Skip to content

Commit

Permalink
Merge pull request #9390 from weirdan/proper-fqcn-resolution-for-psal…
Browse files Browse the repository at this point in the history
…m-scope-this
  • Loading branch information
weirdan committed Feb 24, 2023
2 parents f4c14a4 + 3f52ded commit c0d5d21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit c0d5d21

Please sign in to comment.