Skip to content

Commit

Permalink
Merge pull request #7157 from nowaja/master
Browse files Browse the repository at this point in the history
ISSUE-5962 Fixed wrong line number for @method annotations
  • Loading branch information
orklah committed Dec 15, 2021
2 parents 2dfe45a + 14c181f commit 16c0496
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
Expand Up @@ -26,6 +26,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 @@ -185,8 +186,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 @@ -440,7 +440,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 @@ -548,4 +550,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 @@ -165,6 +165,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 16c0496

Please sign in to comment.