1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #588 - allow anonymous classes to call $this functions

This commit is contained in:
Matthew Brown 2018-03-18 01:07:14 -04:00
parent a668cfd78b
commit 28522c4f94
3 changed files with 14 additions and 2 deletions

View File

@ -59,7 +59,7 @@ class MethodCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker
$method_id = null; $method_id = null;
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) { 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( if (IssueBuffer::accepts(
new InvalidScope( new InvalidScope(
'Use of $this in non-class context', 'Use of $this in non-class context',

View File

@ -438,7 +438,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource
} }
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) { } elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) {
try { 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); $class_checker->analyze(null, $global_context);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
// disregard this exception, we'll likely see it elsewhere in the form // disregard this exception, we'll likely see it elsewhere in the form

View File

@ -157,6 +157,18 @@ class Php70Test extends TestCase
public function foo2(): void {} // commenting this line out fixes public function foo2(): void {} // commenting this line out fixes
}', }',
], ],
'anonymousClassExtendsWithThis' => [
'<?php
class A {
public function foo() : void {}
}
$class = new class extends A {
public function f(): int {
$this->foo();
return 42;
}
};',
],
'returnAnonymousClass' => [ 'returnAnonymousClass' => [
'<?php '<?php
/** @return object */ /** @return object */