From 3f52ded9c37a9ab227b8052e132223efd97acc44 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 24 Feb 2023 00:53:36 -0400 Subject: [PATCH] Correctly process use aliases in `@psalm-scope-this` Fixes vimeo/psalm#9386 --- .../Internal/Analyzer/StatementsAnalyzer.php | 15 +++++++-------- tests/TypeReconciliation/ScopeTest.php | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index c13850f0a98..896f5ac3878 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -76,7 +76,6 @@ use function get_class; use function in_array; use function is_string; -use function ltrim; use function preg_split; use function reset; use function round; @@ -794,21 +793,21 @@ private function parseStatementDocblock( if (isset($comments->tags['psalm-scope-this'])) { $trimmed = trim(reset($comments->tags['psalm-scope-this'])); - $trimmed = ltrim($trimmed, '\\'); + $scope_fqcn = Type::getFQCLNFromString($trimmed, $this->getAliases()); - if (!$codebase->classExists($trimmed)) { + if (!$codebase->classExists($scope_fqcn)) { IssueBuffer::maybeAdd( new UndefinedDocblockClass( - 'Scope class ' . $trimmed . ' does not exist', + 'Scope class ' . $scope_fqcn . ' does not exist', new CodeLocation($this->getSource(), $stmt, null, true), - $trimmed, + $scope_fqcn, ), ); } else { - $this_type = Type::parseString($trimmed); - $context->self = $trimmed; + $this_type = Type::parseString($scope_fqcn); + $context->self = $scope_fqcn; $context->vars_in_scope['$this'] = $this_type; - $this->setFQCLN($trimmed); + $this->setFQCLN($scope_fqcn); } } } diff --git a/tests/TypeReconciliation/ScopeTest.php b/tests/TypeReconciliation/ScopeTest.php index 2ebe26ca274..19d2c0b9520 100644 --- a/tests/TypeReconciliation/ScopeTest.php +++ b/tests/TypeReconciliation/ScopeTest.php @@ -215,6 +215,21 @@ function a(): ?int { ?>

getMessage() ?>

', ], + 'psalmScopeWithNamespace' => [ + 'code' => <<<'PHP' + +

f(); ?>

+