Skip to content

Commit c5952d7

Browse files
committed
add deprecation reference to NarrowTooWideReturnTypeRector
1 parent 1dfe1b2 commit c5952d7

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\FunctionLike;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @deprecated as renamed to \Rector\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRector
16+
*/
17+
final class NarrowTooWideReturnTypeRector extends AbstractRector implements DeprecatedInterface
18+
{
19+
public function getRuleDefinition(): RuleDefinition
20+
{
21+
return new RuleDefinition('Deprecated', []);
22+
}
23+
24+
public function getNodeTypes(): array
25+
{
26+
return [ClassMethod::class];
27+
}
28+
29+
public function refactor(Node $node): never
30+
{
31+
throw new ShouldNotHappenException(sprintf(
32+
'Class "%s" is deprecated and renamed to "%s". Use the new class instead.',
33+
self::class,
34+
NarrowWideUnionReturnTypeRector::class
35+
));
36+
}
37+
}

rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ public function getNodeTypes(): array
9595
*/
9696
public function refactor(Node $node): ?Node
9797
{
98-
if (! $node instanceof ClassMethod) {
99-
return null;
100-
}
101-
10298
$returnType = $node->returnType;
10399

104100
if (! $returnType instanceof Identifier && ! $returnType instanceof FullyQualified) {
@@ -156,7 +152,7 @@ private function isDeclaredTypeFinal(string $declaredType): bool
156152
$declaredObjectType = new ObjectType($declaredType);
157153
$classReflection = $declaredObjectType->getClassReflection();
158154

159-
if ($classReflection === null) {
155+
if (! $classReflection instanceof ClassReflection) {
160156
return false;
161157
}
162158

@@ -168,7 +164,7 @@ private function isActualTypeAnonymous(string $actualType): bool
168164
$actualObjectType = new ObjectType($actualType);
169165
$classReflection = $actualObjectType->getClassReflection();
170166

171-
if ($classReflection === null) {
167+
if (! $classReflection instanceof ClassReflection) {
172168
return false;
173169
}
174170

@@ -218,13 +214,13 @@ private function hasParentMethodWithNonObjectReturn(ClassMethod $classMethod): b
218214

219215
$parentClassMethod = $this->astResolver->resolveClassMethod($ancestor->getName(), $methodName);
220216

221-
if ($parentClassMethod === null) {
217+
if (! $parentClassMethod instanceof ClassMethod) {
222218
continue;
223219
}
224220

225221
$parentReturnType = $parentClassMethod->returnType;
226222

227-
if ($parentReturnType === null) {
223+
if (! $parentReturnType instanceof Node) {
228224
continue;
229225
}
230226

@@ -238,9 +234,9 @@ private function hasParentMethodWithNonObjectReturn(ClassMethod $classMethod): b
238234
return false;
239235
}
240236

241-
private function getActualReturnClass(ClassMethod $node): ?string
237+
private function getActualReturnClass(ClassMethod $classMethod): ?string
242238
{
243-
$returnStatements = $this->betterNodeFinder->findReturnsScoped($node);
239+
$returnStatements = $this->betterNodeFinder->findReturnsScoped($classMethod);
244240

245241
if ($returnStatements === []) {
246242
return null;

0 commit comments

Comments
 (0)