Skip to content

Commit d5e6263

Browse files
Add tests (#58)
Co-authored-by: Viktor Szépe <viktor@szepe.net>
1 parent e26a653 commit d5e6263

14 files changed

+156
-7
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trim_trailing_whitespace = true
1010
indent_style = space
1111
indent_size = 4
1212

13-
[*.xml]
13+
[*.{xml,xml.dist}]
1414
indent_style = tab
1515
indent_size = 4
1616

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
/.phpunit.result.cache
12
/composer.lock
23
/vendor/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ install:
2323

2424
script:
2525
- "composer test:syntax -- --no-progress"
26+
- "composer test:phpunit"
2627
- "composer test:cs -- -s"
2728
- "composer test:phpstan -- --ansi --memory-limit=1G --no-progress"

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
"require-dev": {
2020
"composer/composer": "^1.10.23",
2121
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
22+
"nikic/php-parser": "< 4.13.0",
2223
"php-parallel-lint/php-parallel-lint": "^1.1",
2324
"phpstan/phpstan-strict-rules": "^0.12",
25+
"phpunit/phpunit": "^7 || ^9",
2426
"szepeviktor/phpcs-psr-12-neutron-hybrid-ruleset": "^0.6"
2527
},
2628
"extra": {
@@ -38,9 +40,11 @@
3840
"scripts": {
3941
"test": [
4042
"@test:syntax",
43+
"@test:phpunit",
4144
"@test:cs",
4245
"@test:phpstan"
4346
],
47+
"test:phpunit": "phpunit",
4448
"test:cs": "phpcs --standard=PSR12NeutronRuleset --exclude=PEAR.Commenting.ClassComment,PEAR.Commenting.FileComment,Generic.Files.LineLength bootstrap.php src/",
4549
"test:phpstan": "phpstan analyze",
4650
"test:syntax": "parallel-lint bootstrap.php src/"

extension.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ services:
4545
- phpstan.broker.dynamicFunctionReturnTypeExtension
4646
parameters:
4747
bootstrapFiles:
48-
- ../../php-stubs/wordpress-stubs/wordpress-stubs.php
48+
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
4949
- bootstrap.php
5050
dynamicConstantNames:
5151
- WP_DEBUG

phpunit.xml.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<phpunit
2+
colors="true"
3+
failOnRisky="true"
4+
failOnWarning="true"
5+
>
6+
<testsuites>
7+
<testsuite name="main">
8+
<directory suffix="Test.php">./tests/</directory>
9+
</testsuite>
10+
</testsuites>
11+
</phpunit>

src/GetCommentDynamicFunctionReturnTypeExtension.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Expr\FuncCall;
1313
use PHPStan\Analyser\Scope;
1414
use PHPStan\Reflection\FunctionReflection;
15+
use PHPStan\Reflection\ParametersAcceptorSelector;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\ArrayType;
1718
use PHPStan\Type\StringType;
@@ -20,6 +21,7 @@
2021
use PHPStan\Type\ObjectType;
2122
use PHPStan\Type\NullType;
2223
use PHPStan\Type\TypeCombinator;
24+
use PHPStan\Type\Constant\ConstantStringType;
2325

2426
class GetCommentDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2527
{
@@ -32,6 +34,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3234
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3335
{
3436
$output = 'OBJECT';
37+
38+
if (count($functionCall->args) >= 2) {
39+
$argumentType = $scope->getType($functionCall->args[1]->value);
40+
41+
// When called with an $output that isn't a constant string, return default return type
42+
if (! $argumentType instanceof ConstantStringType) {
43+
return ParametersAcceptorSelector::selectFromArgs(
44+
$scope,
45+
$functionCall->args,
46+
$functionReflection->getVariants()
47+
)->getReturnType();
48+
}
49+
}
50+
3551
if (count($functionCall->args) >= 2 && $functionCall->args[1]->value instanceof ConstFetch) {
3652
$output = $functionCall->args[1]->value->name->getLast();
3753
}

src/GetObjectTaxonomiesDynamicFunctionReturnTypeExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Expr\FuncCall;
1414
use PHPStan\Analyser\Scope;
1515
use PHPStan\Reflection\FunctionReflection;
16+
use PHPStan\Reflection\ParametersAcceptorSelector;
1617
use PHPStan\Type\Type;
1718
use PHPStan\Type\ArrayType;
1819
use PHPStan\Type\IntegerType;
@@ -39,12 +40,16 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3940

4041
$argumentType = $scope->getType($functionCall->args[1]->value);
4142

42-
// When called with a non-string $output, return default return type
43+
// When called with an $output that isn't a constant string, return default return type
4344
if (! $argumentType instanceof ConstantStringType) {
44-
return new ArrayType(new IntegerType(), new StringType());
45+
return ParametersAcceptorSelector::selectFromArgs(
46+
$scope,
47+
$functionCall->args,
48+
$functionReflection->getVariants()
49+
)->getReturnType();
4550
}
4651

47-
// Called with a string $output
52+
// Called with a constant string $output
4853
switch ($argumentType->getValue()) {
4954
case 'objects':
5055
return new ArrayType(new StringType(), new ObjectType('WP_Taxonomy'));

src/MySQL2DateDynamicFunctionReturnTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3333
{
3434
$argumentType = $scope->getType($functionCall->args[0]->value);
3535

36-
// When called with a non-string $format, return default return type
36+
// When called with a $format that isn't a constant string, return default return type
3737
if (! $argumentType instanceof ConstantStringType) {
3838
return ParametersAcceptorSelector::selectFromArgs(
3939
$scope,
@@ -42,7 +42,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4242
)->getReturnType();
4343
}
4444

45-
// Called with a string $format
45+
// Called with a constant string $format
4646
switch ($argumentType->getValue()) {
4747
case 'G':
4848
case 'U':

tests/bootstrap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
error_reporting(E_ALL);
6+
7+
require_once __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)