Skip to content

Commit 0613451

Browse files
committed
Replicate behaviour from ThisVariableRule in DefinedVariableRule
1 parent b9f62d6 commit 0613451

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

src/Analyser/MutatingScope.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
use PHPStan\Type\UnionType;
8888
use PHPStan\Type\VerbosityLevel;
8989
use PHPStan\Type\VoidType;
90+
use function array_key_exists;
9091

9192
class MutatingScope implements Scope
9293
{
@@ -2269,7 +2270,8 @@ public function enterClassMethod(
22692270
$isDeprecated,
22702271
$isInternal,
22712272
$isFinal
2272-
)
2273+
),
2274+
!$classMethod->isStatic()
22732275
);
22742276
}
22752277

@@ -2354,12 +2356,14 @@ public function enterFunction(
23542356
$isDeprecated,
23552357
$isInternal,
23562358
$isFinal
2357-
)
2359+
),
2360+
false
23582361
);
23592362
}
23602363

23612364
private function enterFunctionLike(
2362-
PhpFunctionFromParserNodeReflection $functionReflection
2365+
PhpFunctionFromParserNodeReflection $functionReflection,
2366+
bool $preserveThis
23632367
): self
23642368
{
23652369
$variableTypes = $this->getVariableTypes();
@@ -2373,6 +2377,10 @@ private function enterFunctionLike(
23732377
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $parameter->getNativeType();
23742378
}
23752379

2380+
if (!$preserveThis && array_key_exists('this', $variableTypes)) {
2381+
unset($variableTypes['this']);
2382+
}
2383+
23762384
return $this->scopeFactory->create(
23772385
$this->context,
23782386
$this->isDeclareStrictTypes(),

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,4 +744,50 @@ public function testRootScopeMaybeDefined(): void
744744
$this->analyse([__DIR__ . '/data/root-scope-maybe.php'], []);
745745
}
746746

747+
public function testRootScopeMaybeDefinedCheck(): void
748+
{
749+
$this->cliArgumentsVariablesRegistered = true;
750+
$this->polluteScopeWithLoopInitialAssignments = false;
751+
$this->polluteCatchScopeWithTryAssignments = false;
752+
$this->checkMaybeUndefinedVariables = true;
753+
$this->polluteScopeWithAlwaysIterableForeach = true;
754+
$this->analyse([__DIR__ . '/data/root-scope-maybe.php'], [
755+
[
756+
'Variable $maybe might not be defined.',
757+
3,
758+
],
759+
[
760+
'Variable $this might not be defined.',
761+
5,
762+
],
763+
]);
764+
}
765+
766+
public function testFormerThisVariableRule(): void
767+
{
768+
$this->cliArgumentsVariablesRegistered = true;
769+
$this->polluteScopeWithLoopInitialAssignments = false;
770+
$this->polluteCatchScopeWithTryAssignments = false;
771+
$this->checkMaybeUndefinedVariables = true;
772+
$this->polluteScopeWithAlwaysIterableForeach = true;
773+
$this->analyse([__DIR__ . '/data/this.php'], [
774+
[
775+
'Undefined variable: $this',
776+
16,
777+
],
778+
[
779+
'Undefined variable: $this',
780+
20,
781+
],
782+
[
783+
'Undefined variable: $this',
784+
26,
785+
],
786+
[
787+
'Undefined variable: $this',
788+
38,
789+
],
790+
]);
791+
}
792+
747793
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<?php
22

33
echo $maybe;
4+
5+
echo $this;

tests/PHPStan/Rules/Variables/data/this.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class Foo
88
public function doFoo()
99
{
1010
$this->test;
11-
$foo->test;
11+
1212
}
1313

1414
public static function doBar()
1515
{
1616
$this->test;
17-
$foo->test;
18-
$$bar->test;
17+
18+
1919

2020
$this->blabla = 'fooo';
2121
}

0 commit comments

Comments
 (0)