Skip to content

Commit 8e02273

Browse files
committed
add same support to AddReturnDocblockForCommonObjectDenominatorRector
1 parent d5d631c commit 8e02273

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension;
6+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension;
7+
8+
final class OverrideDummyArray
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function getExtensions(): array
14+
{
15+
return [
16+
new FirstExtension(),
17+
new SecondExtension()
18+
];
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Fixture;
27+
28+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\FirstExtension;
29+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\SecondExtension;
30+
31+
final class OverrideDummyArray
32+
{
33+
/**
34+
* @return \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector\Source\Contract\ExtensionInterface[]
35+
*/
36+
public function getExtensions(): array
37+
{
38+
return [
39+
new FirstExtension(),
40+
new SecondExtension()
41+
];
42+
}
43+
}
44+
45+
?>

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Rector\Rector\AbstractRector;
2323
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2424
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
25+
use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer;
2526
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2627
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2728

@@ -34,7 +35,8 @@ public function __construct(
3435
private readonly PhpDocInfoFactory $phpDocInfoFactory,
3536
private readonly ReturnNodeFinder $returnNodeFinder,
3637
private readonly ReflectionProvider $reflectionProvider,
37-
private readonly PhpDocTypeChanger $phpDocTypeChanger
38+
private readonly PhpDocTypeChanger $phpDocTypeChanger,
39+
private UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer
3840
) {
3941
}
4042

@@ -110,8 +112,10 @@ public function refactor(Node $node): ?Node
110112
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
111113
$returnType = $phpDocInfo->getReturnType();
112114

113-
if (! $returnType instanceof MixedType || $returnType->isExplicitMixed()) {
114-
return null;
115+
if ($returnType instanceof ArrayType) {
116+
if (! $returnType->getItemType() instanceof MixedType) {
117+
return null;
118+
}
115119
}
116120

117121
// definitely not an array return
@@ -175,6 +179,7 @@ public function refactor(Node $node): ?Node
175179
$this->resolveKeyType($returnedType),
176180
new FullyQualifiedObjectType($firstSharedType)
177181
);
182+
178183
$hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $objectTypeArrayType);
179184
if (! $hasChanged) {
180185
return null;

0 commit comments

Comments
 (0)