From 28522c4f9486e79b7094eebcbccb3c6da3b06e32 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 18 Mar 2018 01:07:14 -0400 Subject: [PATCH] Fix #588 - allow anonymous classes to call $this functions --- .../Statements/Expression/Call/MethodCallChecker.php | 2 +- src/Psalm/Checker/StatementsChecker.php | 2 +- tests/Php70Test.php | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php b/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php index 0e1a32f00..105e15ac3 100644 --- a/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php +++ b/src/Psalm/Checker/Statements/Expression/Call/MethodCallChecker.php @@ -59,7 +59,7 @@ class MethodCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker $method_id = null; if ($stmt->var instanceof PhpParser\Node\Expr\Variable) { - if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_checker->getClassName()) { + if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_checker->getFQCLN()) { if (IssueBuffer::accepts( new InvalidScope( 'Use of $this in non-class context', diff --git a/src/Psalm/Checker/StatementsChecker.php b/src/Psalm/Checker/StatementsChecker.php index f7dfbaf80..4060b4d5a 100644 --- a/src/Psalm/Checker/StatementsChecker.php +++ b/src/Psalm/Checker/StatementsChecker.php @@ -438,7 +438,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource } } elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) { try { - $class_checker = (new ClassChecker($stmt, $this->source, $stmt->name)); + $class_checker = new ClassChecker($stmt, $this->source, $stmt->name); $class_checker->analyze(null, $global_context); } catch (\InvalidArgumentException $e) { // disregard this exception, we'll likely see it elsewhere in the form diff --git a/tests/Php70Test.php b/tests/Php70Test.php index fd8441c62..d1edbffac 100644 --- a/tests/Php70Test.php +++ b/tests/Php70Test.php @@ -157,6 +157,18 @@ class Php70Test extends TestCase public function foo2(): void {} // commenting this line out fixes }', ], + 'anonymousClassExtendsWithThis' => [ + 'foo(); + return 42; + } + };', + ], 'returnAnonymousClass' => [ '