Skip to content

Commit de0b01d

Browse files
[perf] Speed up type declaration rules with cheap structural and name guards before reflection (#8661)
* [perf] Speed up type declaration rules with cheap structural and name guards before reflection * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 07055c6 commit de0b01d

7 files changed

Lines changed: 40 additions & 36 deletions

‎rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public function refactor(Node $node): ?Node
8787
{
8888
$this->hasChanged = false;
8989

90+
// skip class without parents/interfaces, there is no param type to mimic
91+
if ($node instanceof Class_ && !$node->extends instanceof Name && $node->implements === []) {
92+
return null;
93+
}
94+
9095
// skip guarded classes, where adding a param type would break child classes
9196
if ($this->parentClassMethodTypeOverrideGuard->isTypeGuardedClass($node)) {
9297
return null;

‎rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,24 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
131131

132132
private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool
133133
{
134-
if ($this->classModifierChecker->isInsideFinalClass($classMethod)) {
134+
if (count((array) $classMethod->stmts) !== 1) {
135135
return false;
136136
}
137137

138-
if (count((array) $classMethod->stmts) !== 1) {
138+
$onlyStmt = $classMethod->stmts[0] ?? null;
139+
if (! $onlyStmt instanceof Expression || ! $onlyStmt->expr instanceof Throw_) {
139140
return false;
140141
}
141142

142-
$onlyStmt = $classMethod->stmts[0] ?? null;
143-
return $onlyStmt instanceof Expression && $onlyStmt->expr instanceof Throw_;
143+
return ! $this->classModifierChecker->isInsideFinalClass($classMethod);
144144
}
145145

146146
private function isNotFinalAndEmpty(ClassMethod $classMethod): bool
147147
{
148-
if ($this->classModifierChecker->isInsideFinalClass($classMethod)) {
148+
if ($classMethod->stmts !== []) {
149149
return false;
150150
}
151151

152-
return $classMethod->stmts === [];
152+
return ! $this->classModifierChecker->isInsideFinalClass($classMethod);
153153
}
154154
}

‎rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ public function refactor(Node $node): ?Node
7979
continue;
8080
}
8181

82+
if (! $this->isNames($classMethod, [
83+
MethodName::CALL,
84+
MethodName::CALL_STATIC,
85+
MethodName::__SET,
86+
MethodName::__GET,
87+
MethodName::ISSET,
88+
MethodName::UNSET,
89+
])) {
90+
continue;
91+
}
92+
8293
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
8394
continue;
8495
}
@@ -91,23 +102,11 @@ public function refactor(Node $node): ?Node
91102
}
92103
}
93104

94-
// first arg string
95-
if ($this->isNames(
96-
$classMethod,
97-
[
98-
MethodName::CALL,
99-
MethodName::CALL_STATIC,
100-
MethodName::__SET,
101-
MethodName::__GET,
102-
MethodName::ISSET,
103-
MethodName::UNSET,
104-
]
105-
)) {
106-
$firstParam = $classMethod->getParams()[0];
107-
if (! $firstParam->type instanceof Node) {
108-
$firstParam->type = new Identifier('string');
109-
$hasChanged = true;
110-
}
105+
// first arg string, shared by all handled magic methods
106+
$firstParam = $classMethod->getParams()[0];
107+
if (! $firstParam->type instanceof Node) {
108+
$firstParam->type = new Identifier('string');
109+
$hasChanged = true;
111110
}
112111

113112
if ($this->isName($classMethod, MethodName::__GET) && $this->phpVersionProvider->isAtLeastPhpVersion(

‎rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ private function isTestClassMethodWithFilledReturnType(ClassMethod|Function_ $ca
9191
return false;
9292
}
9393

94-
if (! $this->testsNodeAnalyzer->isInTestClass($callLike)) {
94+
if (! $callLike->returnType instanceof Node) {
9595
return false;
9696
}
9797

98-
return $callLike->returnType instanceof Node;
98+
return $this->testsNodeAnalyzer->isInTestClass($callLike);
9999
}
100100
}

‎rules/TypeDeclaration/Rector/Class_/TypedPropertyFromDocblockSetUpDefinedRector.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public function getNodeTypes(): array
9292
*/
9393
public function refactor(Node $node): ?Node
9494
{
95-
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
96-
return null;
97-
}
98-
9995
// nothing useful here
10096
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
10197
if (! $setUpClassMethod instanceof ClassMethod) {
10298
return null;
10399
}
104100

101+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
102+
return null;
103+
}
104+
105105
$hasChanged = false;
106106

107107
foreach ($node->getProperties() as $property) {

‎rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ public function getNodeTypes(): array
7575
*/
7676
public function refactor(Node $node): ?Node
7777
{
78-
$yieldNodes = $this->yieldNodeFinder->find($node);
79-
if ($yieldNodes === []) {
80-
return null;
81-
}
82-
8378
// skip already filled type
8479
if ($node->returnType instanceof Node && $this->isNames(
8580
$node->returnType,
@@ -88,6 +83,11 @@ public function refactor(Node $node): ?Node
8883
return null;
8984
}
9085

86+
$yieldNodes = $this->yieldNodeFinder->find($node);
87+
if ($yieldNodes === []) {
88+
return null;
89+
}
90+
9191
$scope = ScopeFetcher::fetch($node);
9292
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod(
9393
$node,

‎rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public function refactor(Node $node): ?Node
8383
}
8484

8585
foreach ($this->addPropertyTypeDeclarations as $addPropertyTypeDeclaration) {
86-
if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) {
86+
if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) {
8787
continue;
8888
}
8989

90-
if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) {
90+
if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) {
9191
continue;
9292
}
9393

0 commit comments

Comments
 (0)