Skip to content

Commit

Permalink
Replicate behaviour from ThisVariableRule in DefinedVariableRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 20, 2020
1 parent b9f62d6 commit 0613451
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_key_exists;

class MutatingScope implements Scope
{
Expand Down Expand Up @@ -2269,7 +2270,8 @@ public function enterClassMethod(
$isDeprecated,
$isInternal,
$isFinal
)
),
!$classMethod->isStatic()
);
}

Expand Down Expand Up @@ -2354,12 +2356,14 @@ public function enterFunction(
$isDeprecated,
$isInternal,
$isFinal
)
),
false
);
}

private function enterFunctionLike(
PhpFunctionFromParserNodeReflection $functionReflection
PhpFunctionFromParserNodeReflection $functionReflection,
bool $preserveThis
): self
{
$variableTypes = $this->getVariableTypes();
Expand All @@ -2373,6 +2377,10 @@ private function enterFunctionLike(
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $parameter->getNativeType();
}

if (!$preserveThis && array_key_exists('this', $variableTypes)) {
unset($variableTypes['this']);
}

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,4 +744,50 @@ public function testRootScopeMaybeDefined(): void
$this->analyse([__DIR__ . '/data/root-scope-maybe.php'], []);
}

public function testRootScopeMaybeDefinedCheck(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->polluteCatchScopeWithTryAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/root-scope-maybe.php'], [
[
'Variable $maybe might not be defined.',
3,
],
[
'Variable $this might not be defined.',
5,
],
]);
}

public function testFormerThisVariableRule(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->polluteCatchScopeWithTryAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/this.php'], [
[
'Undefined variable: $this',
16,
],
[
'Undefined variable: $this',
20,
],
[
'Undefined variable: $this',
26,
],
[
'Undefined variable: $this',
38,
],
]);
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Variables/data/root-scope-maybe.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php

echo $maybe;

echo $this;
6 changes: 3 additions & 3 deletions tests/PHPStan/Rules/Variables/data/this.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class Foo
public function doFoo()
{
$this->test;
$foo->test;

}

public static function doBar()
{
$this->test;
$foo->test;
$$bar->test;



$this->blabla = 'fooo';
}
Expand Down

0 comments on commit 0613451

Please sign in to comment.