-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get_defined_vars() return type contains know variables
- Loading branch information
1 parent
71d01d6
commit e9ee834
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use function array_filter; | ||
use function array_map; | ||
use function array_merge; | ||
use function array_values; | ||
use function count; | ||
use function range; | ||
|
||
// based on code by @ruudk | ||
final class GetDefinedVarsFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'get_defined_vars'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
if ($scope->canAnyVariableExist()) { | ||
return new ArrayType( | ||
new StringType(), | ||
new MixedType(), | ||
); | ||
} | ||
|
||
$variables = array_values(array_filter( | ||
$scope->getDefinedVariables(), | ||
static fn ($variable) => $variable !== 'this', | ||
)); | ||
|
||
$maybeVariables = array_values( | ||
$scope->getMaybeDefinedVariables(), | ||
); | ||
|
||
$keys = array_map( | ||
static fn ($variable) => new ConstantStringType($variable), | ||
array_merge($variables, $maybeVariables), | ||
); | ||
|
||
$values = array_map( | ||
static fn ($variable) => $scope->getVariableType($variable), | ||
array_merge($variables, $maybeVariables), | ||
); | ||
|
||
if ($maybeVariables !== []) { | ||
$maybeIndexes = range(count($variables), count($variables) + count($maybeVariables)); | ||
} else { | ||
$maybeIndexes = []; | ||
} | ||
|
||
return new ConstantArrayType($keys, $values, [0], $maybeIndexes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace GetDefinedVars; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function doFoo(int $param) { | ||
$local = "foo"; | ||
assertType('array{param: int, local: \'foo\'}', get_defined_vars()); | ||
assertType('array{\'param\', \'local\'}', array_keys(get_defined_vars())); | ||
} | ||
|
||
function doBar(int $param) { | ||
$local = "foo"; | ||
if(true) { | ||
$conditional = "bar"; | ||
assertType('array{param: int, local: \'foo\', conditional: \'bar\'}', get_defined_vars()); | ||
} else { | ||
$other = "baz"; | ||
assertType('array{param: int, local: \'foo\', other: \'baz\'}', get_defined_vars()); | ||
} | ||
assertType('array{param: int, local: \'foo\', conditional: \'bar\'}', get_defined_vars()); | ||
} | ||
|
||
function doBaz(int $param) { | ||
$local = "foo"; | ||
if(rand(0, 1)) { | ||
$random1 = "bar"; | ||
assertType('array{param: int, local: \'foo\', random1: \'bar\'}', get_defined_vars()); | ||
} else { | ||
$random2 = "baz"; | ||
assertType('array{param: int, local: \'foo\', random2: \'baz\'}', get_defined_vars()); | ||
} | ||
assertType('array{param: int, local: \'foo\', random2?: \'baz\', random1?: \'bar\'}', get_defined_vars()); | ||
} |