Skip to content

Commit e26a653

Browse files
authored
Merge pull request #46 from johnbillion/fix/get-taxonomies-return-type
2 parents 727ff16 + fe6e117 commit e26a653

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
//phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
4+
35
/**
46
* Set return type of get_taxonomies().
57
*/
@@ -11,13 +13,12 @@
1113
use PhpParser\Node\Expr\FuncCall;
1214
use PHPStan\Analyser\Scope;
1315
use PHPStan\Reflection\FunctionReflection;
14-
use PHPStan\Reflection\ParametersAcceptorSelector;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\ArrayType;
17-
use PHPStan\Type\IntegerType;
1818
use PHPStan\Type\ObjectType;
1919
use PHPStan\Type\StringType;
2020
use PHPStan\Type\Constant\ConstantStringType;
21+
use PHPStan\Type\TypeCombinator;
2122

2223
class GetTaxonomiesDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2324
{
@@ -31,29 +32,32 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3132
*/
3233
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
3334
{
35+
$objectsReturnType = new ArrayType(new StringType(), new ObjectType('WP_Taxonomy'));
36+
$namesReturnType = new ArrayType(new StringType(), new StringType());
37+
$indeterminateReturnType = TypeCombinator::union(
38+
$objectsReturnType,
39+
$namesReturnType
40+
);
41+
3442
// Called without second $output arguments
3543
if (count($functionCall->args) <= 1) {
36-
return new ArrayType(new IntegerType(), new StringType());
44+
return $namesReturnType;
3745
}
3846

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

4149
// When called with a non-string $output, return default return type
4250
if (! $argumentType instanceof ConstantStringType) {
43-
return ParametersAcceptorSelector::selectFromArgs(
44-
$scope,
45-
$functionCall->args,
46-
$functionReflection->getVariants()
47-
)->getReturnType();
51+
return $indeterminateReturnType;
4852
}
4953

5054
// Called with a string $output
5155
switch ($argumentType->getValue()) {
5256
case 'objects':
53-
return new ArrayType(new IntegerType(), new ObjectType('WP_Taxonomy'));
57+
return $objectsReturnType;
5458
case 'names':
5559
default:
56-
return new ArrayType(new IntegerType(), new StringType());
60+
return $namesReturnType;
5761
}
5862
}
5963
}

0 commit comments

Comments
 (0)