Skip to content

Commit bc1fc69

Browse files
authored
[perf] Speed up CodingStyle rules with cheap guards before type lookup, reflection and regex (#8669)
1 parent 76eda1a commit bc1fc69

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

‎rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ public function refactor(Node $node): ?Node
7878
return null;
7979
}
8080

81-
$firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value);
8281
$secondArgType = $this->nodeTypeResolver->getNativeType($args[1]->value);
8382

8483
if (! $secondArgType->isArray()->yes()) {
8584
return null;
8685
}
8786

87+
$firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value);
88+
8889
if ($this->typeComparator->isSubtype($secondArgType->getIterableValueType(), $firstArgType)) {
8990
$node->args[] = new Arg($this->nodeFactory->createTrue());
9091
return $node;

‎rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ public function refactor(Node $node): ?String_
8383
{
8484
$this->hasChanged = false;
8585

86+
$doubleQuoteCount = substr_count($node->value, '"');
87+
$singleQuoteCount = substr_count($node->value, "'");
88+
89+
// no quotes to simplify
90+
if ($doubleQuoteCount === 0 && $singleQuoteCount === 0) {
91+
return null;
92+
}
93+
8694
if ($this->hasNonPrintableChars($node->value)) {
8795
return null;
8896
}
8997

90-
$doubleQuoteCount = substr_count($node->value, '"');
91-
$singleQuoteCount = substr_count($node->value, "'");
9298
$kind = $node->getAttribute(AttributeKey::KIND);
9399

94100
if ($kind === String_::KIND_SINGLE_QUOTED) {

‎rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public function refactor(Node $node): ?Node
6767
}
6868

6969
$classNames = $this->getExistingClasses($node);
70-
$classNames = $this->filterOurShortClasses($classNames);
7170

7271
if ($classNames === []) {
7372
return null;
@@ -111,6 +110,11 @@ private function getExistingClasses(String_ $string): array
111110
$classNames = [];
112111

113112
foreach ($matches['class_name'] as $matchedClassName) {
113+
// short, non-namespaced names are not resolved to a ::class constant
114+
if (! str_contains($matchedClassName, '\\')) {
115+
continue;
116+
}
117+
114118
if (! $this->reflectionProvider->hasClass($matchedClassName)) {
115119
continue;
116120
}
@@ -138,13 +142,4 @@ private function createExpressionsToConcat(array $parts): array
138142

139143
return $exprsToConcat;
140144
}
141-
142-
/**
143-
* @param string[] $classNames
144-
* @return string[]
145-
*/
146-
private function filterOurShortClasses(array $classNames): array
147-
{
148-
return array_filter($classNames, static fn (string $className): bool => str_contains($className, '\\'));
149-
}
150145
}

0 commit comments

Comments
 (0)