Skip to content

Commit fefaf56

Browse files
committed
[TypeDeclaration] Make configurable on NarrowObjectReturnTypeRector to allow to not make change return on abstract/interface return
1 parent 31adca8 commit fefaf56

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DisableReturnAbstractOrInterfaceTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureSkipReturnAbstractOrInterface');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/disable_return_abstract_or_interface_configured_rule.php';
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\FixtureSkipReturnAbstractOrInterface;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\AbstractTalk;
6+
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\ConcreteConferenceTalk;
7+
8+
final class SkipReturnAbstractClass
9+
{
10+
public function create(): AbstractTalk
11+
{
12+
return new ConcreteConferenceTalk();
13+
}
14+
}
15+
16+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\FixtureSkipReturnAbstractOrInterface;
4+
5+
use Illuminate\Container\Container;
6+
use Psr\Container\ContainerInterface;
7+
8+
final class SkipReturnInterface
9+
{
10+
public function create(): ContainerInterface
11+
{
12+
return new Container();
13+
}
14+
}
15+
16+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->ruleWithConfiguration(NarrowObjectReturnTypeRector::class, [
10+
NarrowObjectReturnTypeRector::IS_ALLOW_ABSTRACT_AND_INTERFACE => false,
11+
]);
12+
};

rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@
1818
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1919
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
2020
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
21+
use Rector\Contract\Rector\ConfigurableRectorInterface;
2122
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
2223
use Rector\PhpParser\AstResolver;
2324
use Rector\PhpParser\Node\BetterNodeFinder;
2425
use Rector\Rector\AbstractRector;
2526
use Rector\Reflection\ReflectionResolver;
2627
use Rector\StaticTypeMapper\StaticTypeMapper;
27-
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
28+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2829
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2930

3031
/**
3132
* Narrows return type from generic object or parent class to specific class in final classes/methods.
3233
*
3334
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\NarrowObjectReturnTypeRectorTest
3435
*/
35-
final class NarrowObjectReturnTypeRector extends AbstractRector
36+
final class NarrowObjectReturnTypeRector extends AbstractRector implements ConfigurableRectorInterface
3637
{
38+
/**
39+
* @var string
40+
*/
41+
public const IS_ALLOW_ABSTRACT_AND_INTERFACE = 'is_allow_abstract_and_interface';
42+
43+
private bool $isAllowAbstractAndInterface = true;
44+
3745
public function __construct(
3846
private readonly BetterNodeFinder $betterNodeFinder,
3947
private readonly ReflectionResolver $reflectionResolver,
@@ -50,7 +58,7 @@ public function getRuleDefinition(): RuleDefinition
5058
return new RuleDefinition(
5159
'Narrows return type from generic object or parent class to specific class in final classes/methods',
5260
[
53-
new CodeSample(
61+
new ConfiguredCodeSample(
5462
<<<'CODE_SAMPLE'
5563
final class TalkFactory extends AbstractFactory
5664
{
@@ -70,8 +78,12 @@ protected function build(): ConferenceTalk
7078
}
7179
}
7280
CODE_SAMPLE
81+
,
82+
[
83+
self::IS_ALLOW_ABSTRACT_AND_INTERFACE => true,
84+
]
7385
),
74-
new CodeSample(
86+
new ConfiguredCodeSample(
7587
<<<'CODE_SAMPLE'
7688
final class TalkFactory
7789
{
@@ -91,6 +103,10 @@ public function createConferenceTalk(): ConferenceTalk
91103
}
92104
}
93105
CODE_SAMPLE
106+
,
107+
[
108+
self::IS_ALLOW_ABSTRACT_AND_INTERFACE => true,
109+
]
94110
),
95111
],
96112
);
@@ -120,6 +136,10 @@ public function refactor(Node $node): ?Node
120136
return null;
121137
}
122138

139+
if (! $this->isAllowedReturnsAbstractOrInterface($returnType->toString())) {
140+
return null;
141+
}
142+
123143
if (! $classReflection->isFinalByKeyword() && ! $node->isFinal()) {
124144
return null;
125145
}
@@ -159,6 +179,34 @@ public function refactor(Node $node): ?Node
159179
return $node;
160180
}
161181

182+
/**
183+
* @param array<string, bool> $configuration
184+
*/
185+
public function configure(array $configuration): void
186+
{
187+
$this->isAllowAbstractAndInterface = $configuration[self::IS_ALLOW_ABSTRACT_AND_INTERFACE] ?? true;
188+
}
189+
190+
private function isAllowedReturnsAbstractOrInterface(string $actualReturnClass): bool
191+
{
192+
if ($this->isAllowAbstractAndInterface) {
193+
return true;
194+
}
195+
196+
$actualObjectType = new ObjectType($actualReturnClass);
197+
$classReflection = $actualObjectType->getClassReflection();
198+
199+
if (! $classReflection instanceof ClassReflection) {
200+
return false;
201+
}
202+
203+
if ($classReflection->isInterface()) {
204+
return false;
205+
}
206+
207+
return ! $classReflection->isAbstract();
208+
}
209+
162210
private function updateDocblock(ClassMethod $classMethod, string $actualReturnClass): void
163211
{
164212
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);

0 commit comments

Comments
 (0)