-
Notifications
You must be signed in to change notification settings - Fork 542
get_defined_vars() return type contains know variables #3624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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\ConstantArrayTypeBuilder; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
|
||
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(), | ||
); | ||
} | ||
|
||
$typeBuilder = ConstantArrayTypeBuilder::createEmpty(); | ||
|
||
foreach ($scope->getDefinedVariables() as $variable) { | ||
$typeBuilder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), false); | ||
} | ||
|
||
foreach ($scope->getMaybeDefinedVariables() as $variable) { | ||
$typeBuilder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), true); | ||
} | ||
|
||
return $typeBuilder->getArray(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||||
<?php | ||||||||||
|
||||||||||
namespace GetDefinedVars; | ||||||||||
|
||||||||||
use function PHPStan\Testing\assertType; | ||||||||||
|
||||||||||
$global = "foo"; | ||||||||||
|
||||||||||
assertType('array<string, mixed>', get_defined_vars()); // any variable can exist | ||||||||||
|
||||||||||
function doFoo(int $param) { | ||||||||||
$local = "foo"; | ||||||||||
assertType('array{param: int, local: \'foo\'}', get_defined_vars()); | ||||||||||
assertType('array{\'param\', \'local\'}', array_keys(get_defined_vars())); | ||||||||||
MartinMystikJonas marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When escaping single quotes, I always (when possible) switch to double quoted string to prevent that.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do that too but I checked how it is done in other parts of PHPStan and found out that escaping seems to be standard here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also often do the double quote. but I think in general it's very inconsistent :D not sure if such thing exists, but a coding standard rule might be good for this 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
} | ||||||||||
|
||||||||||
function doBar(int $param) { | ||||||||||
global $global; | ||||||||||
$local = "foo"; | ||||||||||
assertType('array{param: int, global: mixed, local: \'foo\'}', get_defined_vars()); | ||||||||||
assertType('array{\'param\', \'global\', \'local\'}', array_keys(get_defined_vars())); | ||||||||||
} | ||||||||||
|
||||||||||
function doConditional(int $param) { | ||||||||||
$local = "foo"; | ||||||||||
if(true) { | ||||||||||
MartinMystikJonas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
$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 doRandom(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()); | ||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.