Skip to content
Permalink
Browse files

Add support for @psalm-scope-this

Ref #2032
  • Loading branch information...
muglug committed Aug 18, 2019
1 parent ce03be2 commit f5b9dfe5439e4000ec45272a8c659b57fc460a20
Showing with 35 additions and 2 deletions.
  1. +2 −2 src/Psalm/DocComment.php
  2. +27 −0 src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
  3. +6 −0 tests/ScopeTest.php
@@ -150,7 +150,7 @@ public static function parse($docblock, $line_number = null, $preserve_format =
'override-method-visibility', 'seal-properties', 'seal-methods',
'generator-return', 'ignore-falsable-return', 'variadic', 'pure',
'ignore-variable-method', 'ignore-variable-property', 'internal',
'taint-sink', 'taint-source', 'assert-untainted',
'taint-sink', 'taint-source', 'assert-untainted', 'scope-this',
],
true
)) {
@@ -272,7 +272,7 @@ public static function parsePreservingLength(\PhpParser\Comment\Doc $docblock)
'override-method-visibility', 'seal-properties', 'seal-methods',
'generator-return', 'ignore-falsable-return', 'variadic', 'pure',
'ignore-variable-method', 'ignore-variable-property', 'internal',
'taint-sink', 'taint-source', 'assert-untainted',
'taint-sink', 'taint-source', 'assert-untainted', 'scope-this',
],
true
)) {
@@ -136,6 +136,11 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
*/
private $removed_unref_vars = [];
/**
* @var ?string
*/
private $fake_this_class = null;
/**
* @param SourceAnalyzer $source
*/
@@ -268,6 +273,14 @@ public function analyze(
$comments = $this->parsed_docblock;
if (isset($comments['specials']['psalm-scope-this'])) {
$trimmed = trim(reset($comments['specials']['psalm-scope-this']));
$this_type = Type::parseString($trimmed);
$context->self = $trimmed;
$context->vars_in_scope['$this'] = $this_type;
$this->setFQCLN($this_type);
}
if (isset($comments['specials']['psalm-suppress'])) {
$suppressed = array_filter(
array_map(
@@ -2158,4 +2171,18 @@ public function getParsedDocblock() : ?array
{
return $this->parsed_docblock;
}
public function getFQCLN()
{
if ($this->fake_this_class) {
return $this->fake_this_class;
}
return parent::getFQCLN();
}
public function setFQCLN(string $fake_this_class) : void
{
$this->fake_this_class = $fake_this_class;
}
}
@@ -252,6 +252,12 @@ function a(): ?int {
return $foo;
}',
],
'psalmScopeThisInTemplate' => [
'<?php
/** @psalm-scope-this Exception */
?>
<h1><?= $this->getMessage() ?></h1>',
],
];
}

0 comments on commit f5b9dfe

Please sign in to comment.
You can’t perform that action at this time.