Skip to content

Commit

Permalink
Fix #10880
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Apr 6, 2024
1 parent eaeb979 commit aab329e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Psalm/DocComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ final class DocComment
/**
* Parse a docblock comment into its parts.
*/
public static function parsePreservingLength(Doc $docblock): ParsedDocblock
public static function parsePreservingLength(Doc $docblock, bool $no_psalm_error = false): ParsedDocblock
{
$parsed_docblock = DocblockParser::parse(
$docblock->getText(),
$docblock->getStartFilePos(),
);

if ($no_psalm_error) {
return $parsed_docblock;
}

foreach ($parsed_docblock->tags as $special_key => $_) {
if (strpos($special_key, 'psalm-') === 0) {
$special_key = substr($special_key, 6);
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,14 @@ private function parseStatementDocblock(
$this->parsed_docblock = null;
}

if ($this->parsed_docblock === null) {
try {
$this->parsed_docblock = DocComment::parsePreservingLength($docblock, true);
} catch (DocblockParseException $e) {
// already reported above
}
}

$comments = $this->parsed_docblock;

if (isset($comments->tags['psalm-scope-this'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static function parse(
CodeLocation $code_location,
string $cased_function_id
): FunctionDocblockComment {
$parsed_docblock = DocComment::parsePreservingLength($comment);
// invalid @psalm annotations are already reported by the StatementsAnalyzer
$parsed_docblock = DocComment::parsePreservingLength($comment, true);

$comment_text = $comment->getText();

Expand Down
39 changes: 39 additions & 0 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,45 @@ public function ret(): array { return []; }
'$_===' => 'list<array{href: string, lang: string}>',
],
],
'invalidPsalmForMethodShouldNotBreakDocblock' => [
'code' => '<?php
class A {
/**
* @psalm-impure
* @param string $arg
* @return non-falsy-string
*/
public function foo($arg) {
return $arg . "bar";
}
}
$a = new A();
$x = $a->foo("hello");
',
'assertions' => [
'$x===' => 'non-falsy-string',
],
'ignored_issues' => ['InvalidDocblock'],
],
'invalidPsalmForFunctionShouldNotBreakDocblock' => [
'code' => '<?php
/**
* @psalm-impure
* @param string $arg
* @return non-falsy-string
*/
function foo($arg) {
return $arg . "bar";
}
$x = foo("hello");
',
'assertions' => [
'$x===' => 'non-falsy-string',
],
'ignored_issues' => ['InvalidDocblock'],
],
'builtInClassInAShape' => [
'code' => '<?php
/**
Expand Down

0 comments on commit aab329e

Please sign in to comment.