Skip to content

Commit 43ebc5e

Browse files
Fix get defined vars return type
1 parent d08d0c2 commit 43ebc5e

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/Type/Php/GetDefinedVarsFunctionReturnTypeExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3535
$typeBuilder = ConstantArrayTypeBuilder::createEmpty();
3636

3737
foreach ($scope->getDefinedVariables() as $variable) {
38+
if ($variable === 'this') {
39+
continue;
40+
}
41+
3842
$typeBuilder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), false);
3943
}
4044

tests/PHPStan/Analyser/nsrt/get-defined-vars.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ function doRandom(int $param) {
4444
}
4545
assertType('array{param: int, local: \'foo\', random2?: \'baz\', random1?: \'bar\'}', get_defined_vars());
4646
}
47+
48+
class A
49+
{
50+
public function test(): void
51+
{
52+
$local = 'foo';
53+
54+
assertType('array{local: \'foo\'}', get_defined_vars());
55+
}
56+
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,16 @@ public function testBug3396(): void
36663666
$this->analyse([__DIR__ . '/data/bug-3396.php'], []);
36673667
}
36683668

3669+
#[RequiresPhp('>= 8.0')]
3670+
public function testBug13881(): void
3671+
{
3672+
$this->checkThisOnly = false;
3673+
$this->checkNullables = true;
3674+
$this->checkUnionTypes = true;
3675+
$this->checkExplicitMixed = true;
3676+
$this->analyse([__DIR__ . '/data/bug-13881.php'], []);
3677+
}
3678+
36693679
public function testBug13511(): void
36703680
{
36713681
$this->checkThisOnly = false;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug13881;
4+
5+
class A {
6+
7+
public function test(string $functionArg): void {
8+
$localVariable = "test";
9+
10+
$map = [
11+
"var" => "1",
12+
];
13+
14+
$values = get_defined_vars();
15+
16+
unset($values["map"]);
17+
unset($values["functionArg"]);
18+
unset($values["localVariable"]);
19+
//unset($values["this"]);
20+
21+
foreach ($map as $field => $val) {
22+
$values[$field] = $val;
23+
}
24+
25+
$this->varDump(...$values);
26+
}
27+
28+
public function varDump(mixed $var): void {
29+
var_dump($var);
30+
}
31+
}
32+
33+
$a = new A();
34+
$a->test("a");

0 commit comments

Comments
 (0)