Skip to content
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

get_defined_vars() return type contains know variables #3624

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\GetDefinedVarsFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\GetParentClassDynamicFunctionReturnTypeExtension
tags:
Expand Down
46 changes: 46 additions & 0 deletions src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php
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();
}

}
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/get-defined-vars.php
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) {
MartinMystikJonas marked this conversation as resolved.
Show resolved Hide resolved
$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
Copy link
Contributor

@ruudk ruudk Nov 13, 2024

Choose a reason for hiding this comment

The 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.

  • easier on the eyes
  • easier to copy and paste
  • easier to search for the unescaped string in the codebase
Suggested change
assertType('array{param: int, local: \'foo\'}', get_defined_vars());
assertType('array{\'param\', \'local\'}', array_keys(get_defined_vars()));
assertType("array{param: int, local: 'foo'}", get_defined_vars());
assertType("array{'param', 'local'}", array_keys(get_defined_vars()));

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@herndlm herndlm Nov 13, 2024

Choose a reason for hiding this comment

The 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 😊
UPDATE: on the other hand - in those tests files it wouldn't be enforced anyway I guess..

Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
Loading