Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-5962 Fixed wrong line number for @method annotations #7157

Merged
merged 1 commit into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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