mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Improve ergonomics of PHPStorm meta parsing
This commit is contained in:
parent
2ef82d785f
commit
356a45a155
@ -26,7 +26,6 @@
|
||||
<file name="tests/performance/a.test"/>
|
||||
<file name="tests/performance/b.test"/>
|
||||
<file name="tests/ErrorBaselineTest.php"/>
|
||||
<file name="tests/Plugin/phpstorm.meta.php" />
|
||||
</ignoreFiles>
|
||||
</projectFiles>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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?');
|
||||
}
|
||||
|
@ -17,36 +17,11 @@ use Psalm\Type;
|
||||
*/
|
||||
class PhpStormMetaScanner
|
||||
{
|
||||
/**
|
||||
* @param array<PhpParser\Node\Stmt> $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<PhpParser\Node\Arg> $args
|
||||
* @return void
|
||||
*/
|
||||
private static function handleOverride(array $args, Codebase $codebase)
|
||||
public static function handleOverride(array $args, Codebase $codebase)
|
||||
{
|
||||
$identifier = $args[0]->value;
|
||||
|
||||
|
@ -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');
|
||||
|
@ -131,20 +131,16 @@ class StubTest extends TestCase
|
||||
<projectFiles>
|
||||
<directory name="src" />
|
||||
</projectFiles>
|
||||
|
||||
<stubs>
|
||||
<file name="tests/stubs/phpstorm.meta.php" />
|
||||
</stubs>
|
||||
</psalm>'
|
||||
)
|
||||
);
|
||||
|
||||
$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,
|
||||
'<?php
|
||||
|
Loading…
Reference in New Issue
Block a user