diff --git a/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php b/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php index e401fd3f31..16c99f1381 100644 --- a/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php +++ b/src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php @@ -35,6 +35,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $typeBuilder = ConstantArrayTypeBuilder::createEmpty(); foreach ($scope->getDefinedVariables() as $variable) { + if ($variable === 'this') { + continue; + } + $typeBuilder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), false); } diff --git a/tests/PHPStan/Analyser/nsrt/get-defined-vars.php b/tests/PHPStan/Analyser/nsrt/get-defined-vars.php index 345d54dbd3..f61a316d88 100644 --- a/tests/PHPStan/Analyser/nsrt/get-defined-vars.php +++ b/tests/PHPStan/Analyser/nsrt/get-defined-vars.php @@ -44,3 +44,13 @@ function doRandom(int $param) { } assertType('array{param: int, local: \'foo\', random2?: \'baz\', random1?: \'bar\'}', get_defined_vars()); } + +class A +{ + public function test(): void + { + $local = 'foo'; + + assertType('array{local: \'foo\'}', get_defined_vars()); + } +} diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 0dbebf0da3..a4d8f415e7 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3666,6 +3666,16 @@ public function testBug3396(): void $this->analyse([__DIR__ . '/data/bug-3396.php'], []); } + #[RequiresPhp('>= 8.0')] + public function testBug13881(): void + { + $this->checkThisOnly = false; + $this->checkNullables = true; + $this->checkUnionTypes = true; + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-13881.php'], []); + } + public function testBug13511(): void { $this->checkThisOnly = false; diff --git a/tests/PHPStan/Rules/Methods/data/bug-13881.php b/tests/PHPStan/Rules/Methods/data/bug-13881.php new file mode 100644 index 0000000000..a618bd5b70 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-13881.php @@ -0,0 +1,34 @@ + "1", + ]; + + $values = get_defined_vars(); + + unset($values["map"]); + unset($values["functionArg"]); + unset($values["localVariable"]); + //unset($values["this"]); + + foreach ($map as $field => $val) { + $values[$field] = $val; + } + + $this->varDump(...$values); + } + + public function varDump(mixed $var): void { + var_dump($var); + } +} + +$a = new A(); +$a->test("a");