-
Notifications
You must be signed in to change notification settings - Fork 468
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 5eea5cb
Showing
3 changed files
with
71 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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
<?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_values; | ||
|
||
// 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', | ||
)); | ||
|
||
$keys = array_map( | ||
static fn ($variable) => new ConstantStringType($variable), | ||
$variables, | ||
); | ||
|
||
$values = array_map( | ||
static fn ($variable) => $scope->getVariableType($variable), | ||
$variables, | ||
); | ||
|
||
return new ConstantArrayType($keys, $values); | ||
} | ||
|
||
} |
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,11 @@ | ||
<?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())); | ||
} |