|
24 | 24 | use Magento\SemanticVersionChecker\Operation\Visibility\MethodDecreased as VisibilityMethodDecreased;
|
25 | 25 | use Magento\SemanticVersionChecker\Operation\Visibility\MethodIncreased as VisibilityMethodIncreased;
|
26 | 26 | use PhpParser\Node\NullableType;
|
27 |
| -use PhpParser\Node\Stmt; |
| 27 | +use PhpParser\Node\Name; |
| 28 | +use PhpParser\Node\Stmt\Class_; |
28 | 29 | use PhpParser\Node\Stmt\ClassLike;
|
29 | 30 | use PhpParser\Node\Stmt\ClassMethod;
|
30 | 31 | use PHPSemVerChecker\Comparator\Implementation;
|
|
38 | 39 | use PHPSemVerChecker\Operation\ClassMethodParameterTypingRemoved;
|
39 | 40 | use PHPSemVerChecker\Operation\ClassMethodRemoved;
|
40 | 41 | use PHPSemVerChecker\Report\Report;
|
41 |
| -use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
42 |
| -use PHPStan\PhpDocParser\Lexer\Lexer; |
43 |
| -use PHPStan\PhpDocParser\Parser\ConstExprParser; |
44 |
| -use PHPStan\PhpDocParser\Parser\PhpDocParser; |
45 |
| -use PHPStan\PhpDocParser\Parser\TokenIterator; |
46 |
| -use PHPStan\PhpDocParser\Parser\TypeParser; |
47 | 42 |
|
48 | 43 | /**
|
49 | 44 | * Class method analyzer.
|
@@ -422,20 +417,62 @@ private function isReturnsEqualByNullability(ClassMethod $before, ClassMethod $a
|
422 | 417 | */
|
423 | 418 | private function getDocReturnDeclaration(ClassMethod $method)
|
424 | 419 | {
|
425 |
| - if ($method->getDocComment() !== null) { |
426 |
| - $lexer = new Lexer(); |
427 |
| - $typeParser = new TypeParser(); |
428 |
| - $constExprParser = new ConstExprParser(); |
429 |
| - $phpDocParser = new PhpDocParser($typeParser, $constExprParser); |
430 |
| - |
431 |
| - $tokens = $lexer->tokenize((string)$method->getDocComment()); |
432 |
| - $tokenIterator = new TokenIterator($tokens); |
433 |
| - $phpDocNode = $phpDocParser->parse($tokenIterator); |
434 |
| - $tags = $phpDocNode->getTagsByName('@return'); |
435 |
| - /** @var PhpDocTagNode $tag */ |
436 |
| - $tag = array_shift($tags); |
| 420 | + if ( |
| 421 | + ($parsedComment = $method->getAttribute('docCommentParsed')) |
| 422 | + && isset($parsedComment['return']) |
| 423 | + ) { |
| 424 | + $result = implode('|', $parsedComment['return']); |
| 425 | + |
| 426 | + return $result; |
| 427 | + } elseif ($this->dependencyGraph !== null) { |
| 428 | + /** @var Class_ $methodClass */ |
| 429 | + $methodClass = $method->getAttribute('parent'); |
| 430 | + if ($methodClass) { |
| 431 | + $ancestors = []; |
| 432 | + if (!empty($methodClass->extends)) { |
| 433 | + $ancestors = $this->addAncestorsToArray($ancestors, $methodClass->extends); |
| 434 | + } |
| 435 | + if (!empty($methodClass->implements)) { |
| 436 | + $ancestors = $this->addAncestorsToArray($ancestors, $methodClass->implements); |
| 437 | + } |
| 438 | + /** @var Name $ancestor */ |
| 439 | + foreach ($ancestors as $ancestor) { |
| 440 | + $ancestorClass = $this->dependencyGraph->findEntityByName($ancestor->toString()); |
| 441 | + if ($ancestorClass) { |
| 442 | + foreach ($ancestorClass->getMethodList() as $methodItem) { |
| 443 | + if ($method->name->toString() == $methodItem->name->toString()) { |
| 444 | + $result = $this->getDocReturnDeclaration($methodItem); |
| 445 | + if (!empty(trim($result))) { |
| 446 | + return $result; |
| 447 | + } |
| 448 | + } |
| 449 | + } |
| 450 | + } |
| 451 | + } |
| 452 | + } |
437 | 453 | }
|
438 |
| - return isset($tag) ? (string)$tag->value : ' '; |
| 454 | + |
| 455 | + return ' '; |
| 456 | + } |
| 457 | + |
| 458 | + /** |
| 459 | + * Add ancestors to array |
| 460 | + * |
| 461 | + * @param array $ancestors |
| 462 | + * @param array|Name $toAdd |
| 463 | + * @return array |
| 464 | + */ |
| 465 | + private function addAncestorsToArray(array $ancestors, $toAdd) |
| 466 | + { |
| 467 | + if (!empty($toAdd)) { |
| 468 | + if (is_array($toAdd)) { |
| 469 | + $ancestors = array_merge($ancestors, $toAdd); |
| 470 | + } else { |
| 471 | + $ancestors[] = $toAdd; |
| 472 | + } |
| 473 | + } |
| 474 | + |
| 475 | + return $ancestors; |
439 | 476 | }
|
440 | 477 |
|
441 | 478 | /**
|
|
0 commit comments