-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
AddReturnTypeDeclarationBasedOnParentClassMethodRector.php
160 lines (137 loc) · 4.62 KB
/
AddReturnTypeDeclarationBasedOnParentClassMethodRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/lsp_errors
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\AddReturnTypeDeclarationBasedOnParentClassMethodRectorTest
*/
final class AddReturnTypeDeclarationBasedOnParentClassMethodRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly AstResolver $astResolver,
private readonly PhpVersionProvider $phpVersionProvider,
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::FATAL_ERROR_ON_INCOMPATIBLE_METHOD_SIGNATURE;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add missing return type declaration based on parent class method', [
new CodeSample(
<<<'CODE_SAMPLE'
class A
{
public function execute($foo): int
{
}
}
class B extends A{
public function execute()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class A
{
public function execute($foo): int
{
}
}
class B extends A{
public function execute($foo): int
{
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if ($this->nodeNameResolver->isName($node, MethodName::CONSTRUCT)) {
return null;
}
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($node);
if (! $parentMethodReflection instanceof MethodReflection) {
return null;
}
$parentClassMethod = $this->astResolver->resolveClassMethodFromMethodReflection($parentMethodReflection);
if (! $parentClassMethod instanceof ClassMethod) {
return null;
}
if ($parentClassMethod->isPrivate()) {
return null;
}
$parentClassMethodReturnType = $parentClassMethod->getReturnType();
if ($parentClassMethodReturnType === null) {
return null;
}
$parentClassMethodReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType(
$parentClassMethodReturnType
);
return $this->processClassMethodReturnType($node, $parentClassMethodReturnType);
}
private function processClassMethodReturnType(ClassMethod $classMethod, Type $parentType): ?ClassMethod
{
if ($parentType instanceof MixedType) {
$class = $classMethod->getAttribute(AttributeKey::PARENT_NODE);
if (! $class instanceof Class_) {
return null;
}
$className = (string) $this->nodeNameResolver->getName($class);
$currentObjectType = new ObjectType($className);
if (! $parentType->equals($currentObjectType) && $classMethod->returnType !== null) {
return null;
}
}
// remove it
if ($parentType instanceof MixedType && ! $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::MIXED_TYPE
)) {
$classMethod->returnType = null;
return null;
}
// already set and sub type or equal → no change
if ($this->parentClassMethodTypeOverrideGuard->shouldSkipReturnTypeChange($classMethod, $parentType)) {
return null;
}
$classMethod->returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$parentType,
TypeKind::RETURN
);
return $classMethod;
}
}