Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly process use aliases in @psalm-scope-this #9390

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/TypeReconciliation/ScopeTest.php
Expand Up @@ -215,6 +215,21 @@ function a(): ?int {
?>
<h1><?= $this->getMessage() ?></h1>',
],
'psalmScopeWithNamespace' => [
'code' => <<<'PHP'
<?php
namespace A {
class C { public function f(): void {} }
}
namespace B {
use A\C;
/** @psalm-scope-this C */
?>
<h1><?php $this->f(); ?></h1>
<?php
}
PHP,
],
];
}

Expand Down