Skip to content

Commit

Permalink
ISSUE-5962 Fixed wrong line number for @method annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nowaja committed Dec 14, 2021
1 parent fb07d58 commit 14c181f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
Expand Up @@ -23,6 +23,7 @@
use function array_key_first;
use function array_shift;
use function count;
use function explode;
use function implode;
use function in_array;
use function preg_match;
Expand Down Expand Up @@ -182,8 +183,7 @@ public static function parse(
}
}

if (isset($parsed_docblock->tags['psalm-yield'])
) {
if (isset($parsed_docblock->tags['psalm-yield'])) {
$yield = reset($parsed_docblock->tags['psalm-yield']);

$info->yield = trim(preg_replace('@^[ \t]*\*@m', '', $yield));
Expand Down Expand Up @@ -437,7 +437,9 @@ public static function parse(
/** @var Doc */
$node_doc_comment = $node->getDocComment();

$statements[0]->stmts[0]->setAttribute('startLine', $node_doc_comment->getStartLine());
$method_offset = self::getMethodOffset($comment, $method_entry);

$statements[0]->stmts[0]->setAttribute('startLine', $node_doc_comment->getStartLine() + $method_offset);
$statements[0]->stmts[0]->setAttribute('startFilePos', $node_doc_comment->getStartFilePos());
$statements[0]->stmts[0]->setAttribute('endFilePos', $node->getAttribute('startFilePos'));

Expand Down Expand Up @@ -545,4 +547,19 @@ protected static function addMagicPropertyToInfo(
}
}
}

private static function getMethodOffset(Doc $comment, string $method_entry): int
{
$lines = explode("\n", $comment->getText());
$method_offset = 0;

foreach ($lines as $i => $line) {
if (strpos($line, $method_entry) !== false) {
$method_offset = $i;
break;
}
}

return $method_offset;
}
}
71 changes: 71 additions & 0 deletions tests/AnnotationTest.php
Expand Up @@ -164,6 +164,77 @@ function takesArrayIteratorOfString(ArrayIterator $i): void {
$this->analyzeFile('somefile.php', new Context());
}

public function testLessSpecificImplementedReturnTypeWithDocblockOnMultipleLines(): void
{
$this->expectException(CodeException::class);
$this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:5:');

$this->addFile(
'somefile.php',
'<?php
/**
* @method int test()
* @method \DateTime modify($modify)
*/
class WTF extends \DateTime { }'
);

$this->analyzeFile('somefile.php', new Context());
}

public function testLessSpecificImplementedReturnTypeWithDocblockOnMultipleLinesWithMultipleClasses(): void
{
$this->expectException(CodeException::class);
$this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:15:');

$this->addFile(
'somefile.php',
'<?php
class ParentClass
{
/**
* @return $this
*/
public function execute()
{
return $this;
}
}
/**
* @method self execute()
*/
class BreakingThings extends ParentClass
{
}'
);

$this->analyzeFile('somefile.php', new Context());
}

public function testLessSpecificImplementedReturnTypeWithDescription(): void
{
$this->expectException(CodeException::class);
$this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:7:');

$this->addFile(
'somefile.php',
'<?php
/**
* test test test
* test rambling text
* test test text
*
* @method \DateTime modify($modify)
*/
class WTF extends \DateTime { }'
);

$this->analyzeFile('somefile.php', new Context());
}

public function testPhpStormGenericsNoTypehint(): void
{
$this->expectException(CodeException::class);
Expand Down

0 comments on commit 14c181f

Please sign in to comment.