From 356a45a155514cd697aa0823c5c108c4532e1e26 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 16 Feb 2019 18:50:25 -0500 Subject: [PATCH] Improve ergonomics of PHPStorm meta parsing --- psalm.xml.dist | 1 - src/Psalm/Config.php | 6 ----- src/Psalm/Internal/Codebase/Scanner.php | 8 ++++++ .../Internal/Scanner/PhpStormMetaScanner.php | 27 +------------------ .../Internal/Visitor/ReflectorVisitor.php | 17 ++++++++++++ tests/StubTest.php | 12 +++------ tests/{Plugin => stubs}/phpstorm.meta.php | 0 7 files changed, 30 insertions(+), 41 deletions(-) rename tests/{Plugin => stubs}/phpstorm.meta.php (100%) diff --git a/psalm.xml.dist b/psalm.xml.dist index fa68486af..88f1e0983 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -26,7 +26,6 @@ - diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 79dc26d77..8ed5fa6c3 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1213,12 +1213,6 @@ class Config $phpstorm_meta_path = $this->base_dir . DIRECTORY_SEPARATOR . '.phpstorm.meta.php'; if (file_exists($phpstorm_meta_path)) { - $meta_statements = $codebase->statements_provider->getStatementsForFile( - $phpstorm_meta_path - ); - - PhpStormMetaScanner::scan($meta_statements, $codebase); - $stub_files[] = $phpstorm_meta_path; } diff --git a/src/Psalm/Internal/Codebase/Scanner.php b/src/Psalm/Internal/Codebase/Scanner.php index c3543bffe..b8bf0d6e0 100644 --- a/src/Psalm/Internal/Codebase/Scanner.php +++ b/src/Psalm/Internal/Codebase/Scanner.php @@ -498,6 +498,10 @@ class Scanner if (!isset($this->classlike_files[$fq_classlike_name_lc])) { if ($classlikes->doesClassLikeExist($fq_classlike_name_lc)) { + if ($fq_classlike_name_lc === 'self') { + continue; + } + if ($this->debug_output) { echo 'Using reflection to get metadata for ' . $fq_classlike_name . "\n"; } @@ -675,6 +679,10 @@ class Scanner return true; } + if ($fq_class_name === 'self') { + return false; + } + if (isset($this->existing_classlikes_lc[$fq_class_name_lc])) { throw new \InvalidArgumentException('Why are you asking about a builtin class?'); } diff --git a/src/Psalm/Internal/Scanner/PhpStormMetaScanner.php b/src/Psalm/Internal/Scanner/PhpStormMetaScanner.php index 781c2a9b0..1d7d00066 100644 --- a/src/Psalm/Internal/Scanner/PhpStormMetaScanner.php +++ b/src/Psalm/Internal/Scanner/PhpStormMetaScanner.php @@ -17,36 +17,11 @@ use Psalm\Type; */ class PhpStormMetaScanner { - /** - * @param array $stmts - * @return void - */ - public static function scan(array $stmts, Codebase $codebase) - { - foreach ($stmts as $stmt) { - if ($stmt instanceof PhpParser\Node\Stmt\Namespace_ - && $stmt->name - && $stmt->name->parts === ['PHPSTORM_META'] - ) { - foreach ($stmt->stmts as $meta_stmt) { - if ($meta_stmt instanceof PhpParser\Node\Stmt\Expression - && $meta_stmt->expr instanceof PhpParser\Node\Expr\FuncCall - && $meta_stmt->expr->name instanceof PhpParser\Node\Name - && $meta_stmt->expr->name->parts === ['override'] - && count($meta_stmt->expr->args) > 1 - ) { - self::handleOverride($meta_stmt->expr->args, $codebase); - } - } - } - } - } - /** * @param array $args * @return void */ - private static function handleOverride(array $args, Codebase $codebase) + public static function handleOverride(array $args, Codebase $codebase) { $identifier = $args[0]->value; diff --git a/src/Psalm/Internal/Visitor/ReflectorVisitor.php b/src/Psalm/Internal/Visitor/ReflectorVisitor.php index 5dc9573e5..7cc50cb9e 100644 --- a/src/Psalm/Internal/Visitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/Visitor/ReflectorVisitor.php @@ -12,6 +12,7 @@ use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Codebase; use Psalm\Internal\Codebase\CallMap; use Psalm\Internal\Codebase\PropertyMap; +use Psalm\Internal\Scanner\PhpStormMetaScanner; use Psalm\CodeLocation; use Psalm\Config; use Psalm\DocComment; @@ -451,6 +452,22 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse { if ($node instanceof PhpParser\Node\Stmt\Namespace_) { $this->aliases = $this->file_aliases; + + if ($this->codebase->register_stub_files + && $node->name + && $node->name->parts === ['PHPSTORM_META'] + ) { + foreach ($node->stmts as $meta_stmt) { + if ($meta_stmt instanceof PhpParser\Node\Stmt\Expression + && $meta_stmt->expr instanceof PhpParser\Node\Expr\FuncCall + && $meta_stmt->expr->name instanceof PhpParser\Node\Name + && $meta_stmt->expr->name->parts === ['override'] + && count($meta_stmt->expr->args) > 1 + ) { + PhpStormMetaScanner::handleOverride($meta_stmt->expr->args, $this->codebase); + } + } + } } elseif ($node instanceof PhpParser\Node\Stmt\ClassLike) { if (!$this->fq_classlike_names) { throw new \LogicException('$this->fq_classlike_names should not be empty'); diff --git a/tests/StubTest.php b/tests/StubTest.php index 3ee996417..64c0853c9 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -131,20 +131,16 @@ class StubTest extends TestCase + + + + ' ) ); $file_path = getcwd() . '/src/somefile.php'; - $codebase = $this->project_analyzer->getCodebase(); - - $meta_statements = $codebase->statements_provider->getStatementsForFile( - __DIR__ . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . 'phpstorm.meta.php' - ); - - \Psalm\Internal\Scanner\PhpStormMetaScanner::scan($meta_statements, $codebase); - $this->addFile( $file_path, '