Skip to content

Commit 7d6c88e

Browse files
authored
Merge pull request #10885 from kkmuffme/unknown-psalm-annotation-should-not-make-docblock-invalid
Unknown @psalm annotation should not make whole docblock invalid
2 parents eaeb979 + db7bdd8 commit 7d6c88e

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/Psalm/DocComment.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ final class DocComment
4040
/**
4141
* Parse a docblock comment into its parts.
4242
*/
43-
public static function parsePreservingLength(Doc $docblock): ParsedDocblock
43+
public static function parsePreservingLength(Doc $docblock, bool $no_psalm_error = false): ParsedDocblock
4444
{
4545
$parsed_docblock = DocblockParser::parse(
4646
$docblock->getText(),
4747
$docblock->getStartFilePos(),
4848
);
4949

50+
if ($no_psalm_error) {
51+
return $parsed_docblock;
52+
}
53+
5054
foreach ($parsed_docblock->tags as $special_key => $_) {
5155
if (strpos($special_key, 'psalm-') === 0) {
5256
$special_key = substr($special_key, 6);

src/Psalm/Internal/Analyzer/StatementsAnalyzer.php

+8
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,14 @@ private function parseStatementDocblock(
802802
$this->parsed_docblock = null;
803803
}
804804

805+
if ($this->parsed_docblock === null) {
806+
try {
807+
$this->parsed_docblock = DocComment::parsePreservingLength($docblock, true);
808+
} catch (DocblockParseException $e) {
809+
// already reported above
810+
}
811+
}
812+
805813
$comments = $this->parsed_docblock;
806814

807815
if (isset($comments->tags['psalm-scope-this'])) {

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockParser.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static function parse(
5353
CodeLocation $code_location,
5454
string $cased_function_id
5555
): FunctionDocblockComment {
56-
$parsed_docblock = DocComment::parsePreservingLength($comment);
56+
// invalid @psalm annotations are already reported by the StatementsAnalyzer
57+
$parsed_docblock = DocComment::parsePreservingLength($comment, true);
5758

5859
$comment_text = $comment->getText();
5960

tests/AnnotationTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,45 @@ public function ret(): array { return []; }
535535
'$_===' => 'list<array{href: string, lang: string}>',
536536
],
537537
],
538+
'invalidPsalmForMethodShouldNotBreakDocblock' => [
539+
'code' => '<?php
540+
class A {
541+
/**
542+
* @psalm-impure
543+
* @param string $arg
544+
* @return non-falsy-string
545+
*/
546+
public function foo($arg) {
547+
return $arg . "bar";
548+
}
549+
}
550+
551+
$a = new A();
552+
$_ = $a->foo("hello");
553+
',
554+
'assertions' => [
555+
'$_===' => 'non-falsy-string',
556+
],
557+
'ignored_issues' => ['InvalidDocblock'],
558+
],
559+
'invalidPsalmForFunctionShouldNotBreakDocblock' => [
560+
'code' => '<?php
561+
/**
562+
* @psalm-impure
563+
* @param string $arg
564+
* @return non-falsy-string
565+
*/
566+
function foo($arg) {
567+
return $arg . "bar";
568+
}
569+
570+
$_ = foo("hello");
571+
',
572+
'assertions' => [
573+
'$_===' => 'non-falsy-string',
574+
],
575+
'ignored_issues' => ['InvalidDocblock'],
576+
],
538577
'builtInClassInAShape' => [
539578
'code' => '<?php
540579
/**

0 commit comments

Comments
 (0)