Skip to content

Commit a3c271e

Browse files
committed
Correct the return type of get_post() when the output parameter type isn't known.
1 parent 961f5d2 commit a3c271e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/GetPostDynamicFunctionReturnTypeExtension.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 GetPostDynamicFunctionReturnTypeExtension 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
}

0 commit comments

Comments
 (0)