Skip to content

Commit 27c0087

Browse files
authored
[TypeDeclarationDocblocks] Skip overridden methods in DocblockReturnArrayFromDirectArrayInstanceRector (#7995)
Skip @return docblock generation when the ClassMethod overrides a parent/interface method, to avoid conflicting with the existing parent docblock contract. Fixes rectorphp/rector#9599
1 parent 4a063f8 commit 27c0087

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
interface SomeOverriddenInterface
6+
{
7+
/**
8+
* @return int[]
9+
*/
10+
public function getCodes(): iterable;
11+
}
12+
13+
abstract class SomeOverriddenBase
14+
{
15+
/**
16+
* @return string[]
17+
*/
18+
abstract public function getMethods(): iterable;
19+
}
20+
21+
final class SomeOverriddenChild extends SomeOverriddenBase implements SomeOverriddenInterface
22+
{
23+
public function getCodes(): iterable
24+
{
25+
return [
26+
200,
27+
201,
28+
];
29+
}
30+
31+
public function getMethods(): iterable
32+
{
33+
return [
34+
'GET',
35+
'POST',
36+
];
37+
}
38+
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
2020
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
2121
use Rector\TypeDeclarationDocblocks\TypeResolver\ConstantArrayTypeGeneralizer;
22+
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
2223
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2324
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2425

@@ -32,7 +33,8 @@ public function __construct(
3233
private readonly PhpDocTypeChanger $phpDocTypeChanger,
3334
private readonly ConstantArrayTypeGeneralizer $constantArrayTypeGeneralizer,
3435
private readonly ReturnNodeFinder $returnNodeFinder,
35-
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer
36+
private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer,
37+
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
3638
) {
3739
}
3840

@@ -88,6 +90,11 @@ public function refactor(Node $node): ?Node
8890
return null;
8991
}
9092

93+
// skip overridden methods to not conflict with parent/interface @return docblock
94+
if ($node instanceof ClassMethod && $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($node)) {
95+
return null;
96+
}
97+
9198
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
9299

93100
if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) {

0 commit comments

Comments
 (0)