Skip to content

Commit e800e8f

Browse files
scope return and allow final methods
1 parent faea431 commit e800e8f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Fixture;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\ConferenceTalk;
6+
7+
class NonFinalClassWithFinalMethod
8+
{
9+
final public function create(): object
10+
{
11+
return new ConferenceTalk();
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Fixture;
20+
21+
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\ConferenceTalk;
22+
23+
class NonFinalClassWithFinalMethod
24+
{
25+
final public function create(): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\NarrowObjectReturnTypeRector\Source\ConferenceTalk
26+
{
27+
return new ConferenceTalk();
28+
}
29+
}
30+
31+
?>

rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Name\FullyQualified;
1010
use PhpParser\Node\Stmt\ClassMethod;
11-
use PhpParser\Node\Stmt\Return_;
1211
use PHPStan\Reflection\ClassReflection;
1312
use PHPStan\Type\ObjectType;
1413
use Rector\PhpParser\Node\BetterNodeFinder;
@@ -109,7 +108,7 @@ public function refactor(Node $node): ?Node
109108
return null;
110109
}
111110

112-
if (! $classReflection->isFinal()) {
111+
if (! $classReflection->isFinal() && ! $node->isFinal()) {
113112
return null;
114113
}
115114

@@ -226,7 +225,7 @@ private function hasParentMethodWithNonObjectReturn(ClassMethod $classMethod): b
226225

227226
private function getActualReturnClass(ClassMethod $node): ?string
228227
{
229-
$returnStatements = $this->betterNodeFinder->findInstanceOf($node, Return_::class);
228+
$returnStatements = $this->betterNodeFinder->findReturnsScoped($node);
230229

231230
if ($returnStatements === []) {
232231
return null;

0 commit comments

Comments
 (0)