Skip to content

Commit 1f7ebbe

Browse files
get_defined_vars() return type contains know variables
Co-Authored-By: Ruud Kamphuis <[email protected]>
1 parent 71d01d6 commit 1f7ebbe

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

conf/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,11 @@ services:
14271427
tags:
14281428
- phpstan.broker.dynamicFunctionReturnTypeExtension
14291429

1430+
-
1431+
class: PHPStan\Type\Php\GetDefinedVarsFunctionReturnTypeExtension
1432+
tags:
1433+
- phpstan.broker.dynamicFunctionReturnTypeExtension
1434+
14301435
-
14311436
class: PHPStan\Type\Php\GetParentClassDynamicFunctionReturnTypeExtension
14321437
tags:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Type\ArrayType;
9+
use PHPStan\Type\Constant\ConstantArrayType;
10+
use PHPStan\Type\Constant\ConstantStringType;
11+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
12+
use PHPStan\Type\MixedType;
13+
use PHPStan\Type\StringType;
14+
use PHPStan\Type\Type;
15+
use function array_map;
16+
use function array_merge;
17+
use function array_values;
18+
use function count;
19+
use function range;
20+
21+
final class GetDefinedVarsFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
22+
{
23+
24+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
25+
{
26+
return $functionReflection->getName() === 'get_defined_vars';
27+
}
28+
29+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
30+
{
31+
if ($scope->canAnyVariableExist()) {
32+
return new ArrayType(
33+
new StringType(),
34+
new MixedType(),
35+
);
36+
}
37+
38+
$definedVariables = array_values($scope->getDefinedVariables());
39+
$maybeVariables = array_values( $scope->getMaybeDefinedVariables());
40+
$allVariables = array_merge($definedVariables, $maybeVariables);
41+
42+
$keys = array_map(
43+
static fn ($variable) => new ConstantStringType($variable),
44+
$allVariables,
45+
);
46+
47+
$values = array_map(
48+
static fn ($variable) => $scope->getVariableType($variable),
49+
$allVariables,
50+
);
51+
52+
$maybeIndexes = [];
53+
if ($maybeVariables !== []) {
54+
$maybeIndexes = range(count($definedVariables), count($definedVariables) + count($maybeVariables));
55+
}
56+
57+
return new ConstantArrayType($keys, $values, [0], $maybeIndexes);
58+
}
59+
60+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace GetDefinedVars;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
$global = "foo";
8+
9+
assertType('array<string, mixed>', get_defined_vars()); // any variable can exist
10+
11+
function doFoo(int $param) {
12+
$local = "foo";
13+
assertType('array{param: int, local: \'foo\'}', get_defined_vars());
14+
assertType('array{\'param\', \'local\'}', array_keys(get_defined_vars()));
15+
}
16+
17+
function doBar(int $param) {
18+
global $global;
19+
$local = "foo";
20+
assertType('array{param: int, global: mixed, local: \'foo\'}', get_defined_vars());
21+
assertType('array{\'param\', \'global\', \'local\'}', array_keys(get_defined_vars()));
22+
}
23+
24+
function doConditional(int $param) {
25+
$local = "foo";
26+
if(true) {
27+
$conditional = "bar";
28+
assertType('array{param: int, local: \'foo\', conditional: \'bar\'}', get_defined_vars());
29+
} else {
30+
$other = "baz";
31+
assertType('array{param: int, local: \'foo\', other: \'baz\'}', get_defined_vars());
32+
}
33+
assertType('array{param: int, local: \'foo\', conditional: \'bar\'}', get_defined_vars());
34+
}
35+
36+
function doRandom(int $param) {
37+
$local = "foo";
38+
if(rand(0, 1)) {
39+
$random1 = "bar";
40+
assertType('array{param: int, local: \'foo\', random1: \'bar\'}', get_defined_vars());
41+
} else {
42+
$random2 = "baz";
43+
assertType('array{param: int, local: \'foo\', random2: \'baz\'}', get_defined_vars());
44+
}
45+
assertType('array{param: int, local: \'foo\', random2?: \'baz\', random1?: \'bar\'}', get_defined_vars());
46+
}

0 commit comments

Comments
 (0)