From 5fad23f63ac6ade95000309cb4df1e41ef65f862 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 29 Aug 2026 19:30:39 +0200 Subject: [PATCH] Run cheap guards before expensive analysis calls Reorder early-return guards in 7 Rector rules so cheap checks (isName/isNames/count/instanceof) bail out before costly type analysis (getType/isObjectType). Fixes new RectorCheaperGuardsFirstRule from symplify/phpstan-rules 14.13.0. Claude-Session: https://claude.ai/code/session_01Na4eYvNqa2pdhZrBXbegrv --- ...ameResponseCodeWithDebugContentsRector.php | 10 ++++----- .../AddViolationToBuildViolationRector.php | 8 +++---- ...oleExceptionToErrorEventConstantRector.php | 21 +++++++++---------- ...ntainerBuilderCompileEnvArgumentRector.php | 8 +++---- .../VarDumperTestTraitMethodArgsRector.php | 8 +++---- ...izationCheckerIsGrantedExtractorRector.php | 8 +++---- ...yOnWindowsOptionToFollowSymlinksRector.php | 4 ++-- 7 files changed, 33 insertions(+), 34 deletions(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php b/rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php index 3c06a147d..175111533 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php @@ -129,17 +129,17 @@ private function matchResponseExpr(Expr $expr): ?Expr return null; } - $varType = $this->nodeTypeResolver->getType($expr->var); - if (! $varType instanceof ObjectType) { + // must be status method call + if (! $this->isName($expr->name, 'getStatusCode')) { return null; } - if (! $varType->isInstanceof(ResponseClass::BASIC)->yes()) { + $varType = $this->nodeTypeResolver->getType($expr->var); + if (! $varType instanceof ObjectType) { return null; } - // must be status method call - if (! $this->isName($expr->name, 'getStatusCode')) { + if (! $varType->isInstanceof(ResponseClass::BASIC)->yes()) { return null; } diff --git a/rules/Symfony25/Rector/MethodCall/AddViolationToBuildViolationRector.php b/rules/Symfony25/Rector/MethodCall/AddViolationToBuildViolationRector.php index 7000451f5..f9356694d 100644 --- a/rules/Symfony25/Rector/MethodCall/AddViolationToBuildViolationRector.php +++ b/rules/Symfony25/Rector/MethodCall/AddViolationToBuildViolationRector.php @@ -66,6 +66,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?MethodCall { + if (! $this->isName($node->name, 'addViolationAt')) { + return null; + } + $objectType = $this->nodeTypeResolver->getType($node->var); if (! $objectType instanceof ObjectType) { return null; @@ -76,10 +80,6 @@ public function refactor(Node $node): ?MethodCall return null; } - if (! $this->isName($node->name, 'addViolationAt')) { - return null; - } - $args = $node->getArgs(); $path = $args[0]; $message = $args[1]; diff --git a/rules/Symfony33/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector.php b/rules/Symfony33/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector.php index 198f97e09..410ff0684 100644 --- a/rules/Symfony33/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector.php +++ b/rules/Symfony33/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector.php @@ -60,22 +60,21 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node instanceof ClassConstFetch && ( + if ($node instanceof String_) { + if ($node->value !== 'console.exception') { + return null; + } + + return $this->nodeFactory->createClassConstFetch($this->consoleEventsObjectType->getClassName(), 'ERROR'); + } + + if ( $this->isObjectType($node->class, $this->consoleEventsObjectType) && $this->isName($node->name, 'EXCEPTION') - ) ) { return $this->nodeFactory->createClassConstFetch($this->consoleEventsObjectType->getClassName(), 'ERROR'); } - if (! $node instanceof String_) { - return null; - } - - if ($node->value !== 'console.exception') { - return null; - } - - return $this->nodeFactory->createClassConstFetch($this->consoleEventsObjectType->getClassName(), 'ERROR'); + return null; } } diff --git a/rules/Symfony40/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector.php b/rules/Symfony40/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector.php index 356e7e6fe..2288dbdf3 100644 --- a/rules/Symfony40/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector.php +++ b/rules/Symfony40/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector.php @@ -64,6 +64,10 @@ public function refactor(Node $node): ?Node return null; } + if (count($node->args) === 1) { + return null; + } + if (! $this->isObjectType( $node->var, new ObjectType('Symfony\Component\DependencyInjection\ContainerBuilder') @@ -71,10 +75,6 @@ public function refactor(Node $node): ?Node return null; } - if (count($node->args) === 1) { - return null; - } - $node->args = $this->nodeFactory->createArgs([$this->nodeFactory->createTrue()]); return $node; diff --git a/rules/Symfony40/Rector/MethodCall/VarDumperTestTraitMethodArgsRector.php b/rules/Symfony40/Rector/MethodCall/VarDumperTestTraitMethodArgsRector.php index 611cfa409..c79e417cd 100644 --- a/rules/Symfony40/Rector/MethodCall/VarDumperTestTraitMethodArgsRector.php +++ b/rules/Symfony40/Rector/MethodCall/VarDumperTestTraitMethodArgsRector.php @@ -60,6 +60,10 @@ public function refactor(Node $node): ?Node return null; } + if (count($node->args) <= 2) { + return null; + } + if (! $this->isObjectType( $node->var, new ObjectType('Symfony\Component\VarDumper\Test\VarDumperTestTrait') @@ -67,10 +71,6 @@ public function refactor(Node $node): ?Node return null; } - if (count($node->args) <= 2) { - return null; - } - $secondArg = $node->args[2]; if (! $secondArg instanceof Arg) { return null; diff --git a/rules/Symfony44/Rector/MethodCall/AuthorizationCheckerIsGrantedExtractorRector.php b/rules/Symfony44/Rector/MethodCall/AuthorizationCheckerIsGrantedExtractorRector.php index 1746ca65a..aca2a043c 100644 --- a/rules/Symfony44/Rector/MethodCall/AuthorizationCheckerIsGrantedExtractorRector.php +++ b/rules/Symfony44/Rector/MethodCall/AuthorizationCheckerIsGrantedExtractorRector.php @@ -113,6 +113,10 @@ public function refactor(Node $node): MethodCall|BooleanOr|null return null; } + if (! $this->isName($node->name, 'isGranted')) { + return null; + } + $objectType = $this->nodeTypeResolver->getType($node->var); if (! $objectType instanceof ObjectType) { return null; @@ -123,10 +127,6 @@ public function refactor(Node $node): MethodCall|BooleanOr|null return null; } - if (! $this->isName($node->name, 'isGranted')) { - return null; - } - return $this->handleIsGranted($node); } diff --git a/rules/Symfony81/Rector/MethodCall/RenameCopyOnWindowsOptionToFollowSymlinksRector.php b/rules/Symfony81/Rector/MethodCall/RenameCopyOnWindowsOptionToFollowSymlinksRector.php index 24e8175b4..f25cb8621 100644 --- a/rules/Symfony81/Rector/MethodCall/RenameCopyOnWindowsOptionToFollowSymlinksRector.php +++ b/rules/Symfony81/Rector/MethodCall/RenameCopyOnWindowsOptionToFollowSymlinksRector.php @@ -83,11 +83,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $this->isObjectType($node->var, new ObjectType('Symfony\Component\Filesystem\Filesystem'))) { + if (! $this->isName($node->name, 'mirror')) { return null; } - if (! $this->isName($node->name, 'mirror')) { + if (! $this->isObjectType($node->var, new ObjectType('Symfony\Component\Filesystem\Filesystem'))) { return null; }