Skip to content
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
4 changes: 4 additions & 0 deletions src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$typeBuilder = ConstantArrayTypeBuilder::createEmpty();

foreach ($scope->getDefinedVariables() as $variable) {
if ($variable === 'this') {
Copy link
Contributor

@staabm staabm Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just double checked: since regular variables cannot be named '$this' we don't need a inClassScope or similar check

https://3v4l.org/gSRUE#veol


its a open feature request that we don't error about such case atm: phpstan/phpstan#3585

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also though about moving this check directly into $scope->getDefinedVariables() but I would not do that because there might be extensions out there which depend on the behaviour that '$this' is returned atm

continue;
}

$typeBuilder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), false);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/nsrt/get-defined-vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3674,6 +3674,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;
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-13881.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bug13881;

class A {

public function test(string $functionArg): void {
$localVariable = "test";

$map = [
"var" => "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");
Loading