1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
kkmuffme 2024-04-06 09:42:07 +02:00
parent eaeb979222
commit db7bdd81ac
4 changed files with 54 additions and 2 deletions

View File

@ -40,13 +40,17 @@ final class DocComment
/** /**
* Parse a docblock comment into its parts. * 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( $parsed_docblock = DocblockParser::parse(
$docblock->getText(), $docblock->getText(),
$docblock->getStartFilePos(), $docblock->getStartFilePos(),
); );
if ($no_psalm_error) {
return $parsed_docblock;
}
foreach ($parsed_docblock->tags as $special_key => $_) { foreach ($parsed_docblock->tags as $special_key => $_) {
if (strpos($special_key, 'psalm-') === 0) { if (strpos($special_key, 'psalm-') === 0) {
$special_key = substr($special_key, 6); $special_key = substr($special_key, 6);

View File

@ -802,6 +802,14 @@ final class StatementsAnalyzer extends SourceAnalyzer
$this->parsed_docblock = null; $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; $comments = $this->parsed_docblock;
if (isset($comments->tags['psalm-scope-this'])) { if (isset($comments->tags['psalm-scope-this'])) {

View File

@ -53,7 +53,8 @@ final class FunctionLikeDocblockParser
CodeLocation $code_location, CodeLocation $code_location,
string $cased_function_id string $cased_function_id
): FunctionDocblockComment { ): 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(); $comment_text = $comment->getText();

View File

@ -535,6 +535,45 @@ class AnnotationTest extends TestCase
'$_===' => 'list<array{href: string, lang: string}>', '$_===' => '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();
$_ = $a->foo("hello");
',
'assertions' => [
'$_===' => 'non-falsy-string',
],
'ignored_issues' => ['InvalidDocblock'],
],
'invalidPsalmForFunctionShouldNotBreakDocblock' => [
'code' => '<?php
/**
* @psalm-impure
* @param string $arg
* @return non-falsy-string
*/
function foo($arg) {
return $arg . "bar";
}
$_ = foo("hello");
',
'assertions' => [
'$_===' => 'non-falsy-string',
],
'ignored_issues' => ['InvalidDocblock'],
],
'builtInClassInAShape' => [ 'builtInClassInAShape' => [
'code' => '<?php 'code' => '<?php
/** /**