10000 [TypeDeclaration] Fix crash on already return static on AddReturnType… · rectorphp/rector-src@a144023 · GitHub
[go: up one dir, main page]

Skip to content

Commit a144023

Browse files
authored
[TypeDeclaration] Fix crash on already return static on AddReturnTypeDeclarationRector (#6940)
1 parent 8028130 commit a144023

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Fixture;
4+
5+
final class SkipAlreadyStaticReturn
6+
{
7+
public function transform(): static
8+
{
9+
return $this;
10+
}
11+
}

rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector/config/configured_rule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\StaticTypeMapper\ValueObject\Type\SimpleStaticType;
1313
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Fixture\ReturnOfStatic;
1414
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Fixture\ReturnTheMixed;
15+
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Fixture\SkipAlreadyStaticReturn;
1516
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Source\DataTransformerInterface;
1617
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Source\FileInterface;
1718
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector\Source\FolderInterface;
@@ -36,5 +37,10 @@
3637
new AddReturnTypeDeclaration(DataTransformerInterface::class, 'transform', new MixedType()),
3738
new AddReturnTypeDeclaration(FormTypeInterface::class, 'getParent', $nullableStringType),
3839
new AddReturnTypeDeclaration(FolderInterface::class, 'create', $nullableObjectType),
40+
new AddReturnTypeDeclaration(
41+
SkipAlreadyStaticReturn::class,
42+
'transform',
43+
new SimpleStaticType(SkipAlreadyStaticReturn::class)
44+
),
3945
]);
4046
};

rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\TypeDeclaration\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Name;
89
use PhpParser\Node\Stmt\Class_;
910
use PhpParser\Node\Stmt\ClassMethod;
1011
use PHPStan\Type\ArrayType;
@@ -16,6 +17,7 @@
1617
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1718
use Rector\Rector\AbstractRector;
1819
use Rector\StaticTypeMapper\StaticTypeMapper;
20+
use Rector\StaticTypeMapper\ValueObject\Type\SimpleStaticType;
1921
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
2022
use Rector\ValueObject\PhpVersionFeature;
2123
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
@@ -144,6 +146,10 @@ private function processClassMethodNodeWithTypehints(
144146
return;
145147
}
146148

149+
if ($newType instanceof SimpleStaticType && $classMethod->returnType instanceof Name && $this->isName($classMethod->returnType, 'static')) {
150+
return;
151+
}
152+
147153
// already set and sub type or equal → no change
148154
if ($this->parentClassMethodTypeOverrideGuard->shouldSkipReturnTypeChange($classMethod, $newType)) {
149155
return;

0 commit comments

Comments
 (0)
0