1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add support for anonymous classes

This commit is contained in:
Matthew Brown 2016-10-20 20:54:17 -04:00
parent 6283553d19
commit 6d8e722a77
4 changed files with 20 additions and 2 deletions

View File

@ -31,13 +31,19 @@ class ClassChecker extends ClassLikeChecker
*/ */
protected static $class_extends = []; protected static $class_extends = [];
protected static $anonymous_class_count = 0;
/** /**
* @param PhpParser\Node\Stmt\Class_ $class * @param PhpParser\Node\Stmt\Class_ $class
* @param StatementsSource $source * @param StatementsSource $source
* @param string $absolute_class * @param string|null $absolute_class
*/ */
public function __construct(PhpParser\Node\Stmt\Class_ $class, StatementsSource $source, $absolute_class) public function __construct(PhpParser\Node\Stmt\Class_ $class, StatementsSource $source, $absolute_class)
{ {
if ($absolute_class === null) {
$absolute_class = 'PsalmAnonymousClass' . (self::$anonymous_class_count++);
}
parent::__construct($class, $source, $absolute_class); parent::__construct($class, $source, $absolute_class);
self::$existing_classes[$absolute_class] = true; self::$existing_classes[$absolute_class] = true;
@ -193,6 +199,8 @@ class ClassChecker extends ClassLikeChecker
self::$class_extends = []; self::$class_extends = [];
self::$anonymous_class_count = 0;
MethodChecker::clearCache(); MethodChecker::clearCache();
} }
} }

View File

@ -487,6 +487,10 @@ abstract class ClassLikeChecker implements StatementsSource
} }
} }
} }
if (!$this->class->name) {
$this->class->name = $this->absolute_class;
}
} }
/** /**

View File

@ -2059,6 +2059,13 @@ class StatementsChecker
} }
} }
} }
elseif ($stmt->class instanceof PhpParser\Node\Stmt\Class_) {
$this->check([$stmt->class], $context);
$absolute_class = $stmt->class->name;
}
else {
$this->checkExpression($stmt->class, $context);
}
if ($absolute_class) { if ($absolute_class) {
$stmt->inferredType = new Type\Union([new Type\Atomic($absolute_class)]); $stmt->inferredType = new Type\Union([new Type\Atomic($absolute_class)]);

View File

@ -119,7 +119,6 @@ class Php70Test extends PHPUnit_Framework_TestCase
public function testAnonymousClass() public function testAnonymousClass()
{ {
$this->markTestIncomplete('Not yet supported');
$stmts = self::$_parser->parse('<?php $stmts = self::$_parser->parse('<?php
interface Logger { interface Logger {
public function log(string $msg); public function log(string $msg);