From 80b629f7d682de574eb599181eab69de453f3fd8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 15 Oct 2026 21:41:02 +0200 Subject: [PATCH] [perf] Speed up CodingStyle rules with cheap guards before type lookup, reflection and regex --- .../Rector/FuncCall/StrictInArrayRector.php | 3 ++- .../Rector/String_/SimplifyQuoteEscapeRector.php | 10 ++++++++-- ...seClassKeywordForClassNameResolutionRector.php | 15 +++++---------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php b/rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php index 9460174a48d..1516cb0327d 100644 --- a/rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php +++ b/rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php @@ -78,13 +78,14 @@ public function refactor(Node $node): ?Node return null; } - $firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value); $secondArgType = $this->nodeTypeResolver->getNativeType($args[1]->value); if (! $secondArgType->isArray()->yes()) { return null; } + $firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value); + if ($this->typeComparator->isSubtype($secondArgType->getIterableValueType(), $firstArgType)) { $node->args[] = new Arg($this->nodeFactory->createTrue()); return $node; diff --git a/rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php b/rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php index 8a424b2a623..2522ff55a55 100644 --- a/rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php +++ b/rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php @@ -83,12 +83,18 @@ public function refactor(Node $node): ?String_ { $this->hasChanged = false; + $doubleQuoteCount = substr_count($node->value, '"'); + $singleQuoteCount = substr_count($node->value, "'"); + + // no quotes to simplify + if ($doubleQuoteCount === 0 && $singleQuoteCount === 0) { + return null; + } + if ($this->hasNonPrintableChars($node->value)) { return null; } - $doubleQuoteCount = substr_count($node->value, '"'); - $singleQuoteCount = substr_count($node->value, "'"); $kind = $node->getAttribute(AttributeKey::KIND); if ($kind === String_::KIND_SINGLE_QUOTED) { diff --git a/rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php b/rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php index 80e346bfc0f..7efa2fec114 100644 --- a/rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php +++ b/rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php @@ -67,7 +67,6 @@ public function refactor(Node $node): ?Node } $classNames = $this->getExistingClasses($node); - $classNames = $this->filterOurShortClasses($classNames); if ($classNames === []) { return null; @@ -111,6 +110,11 @@ private function getExistingClasses(String_ $string): array $classNames = []; foreach ($matches['class_name'] as $matchedClassName) { + // short, non-namespaced names are not resolved to a ::class constant + if (! str_contains($matchedClassName, '\\')) { + continue; + } + if (! $this->reflectionProvider->hasClass($matchedClassName)) { continue; } @@ -138,13 +142,4 @@ private function createExpressionsToConcat(array $parts): array return $exprsToConcat; } - - /** - * @param string[] $classNames - * @return string[] - */ - private function filterOurShortClasses(array $classNames): array - { - return array_filter($classNames, static fn (string $className): bool => str_contains($className, '\\')); - } }