From 093819535fb279b979afe1afb3a7fb7a5efeb429 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 27 Oct 2025 18:46:03 +0100 Subject: [PATCH 1/2] Assert*Rules: Do cheap checks first --- src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php | 10 +++++----- src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php | 11 +++++------ src/Rules/PHPUnit/AssertSameNullExpectedRule.php | 9 ++++----- src/Rules/PHPUnit/AssertSameWithCountRule.php | 10 ++++------ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php index b03f0830..c357aefb 100644 --- a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php +++ b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php @@ -27,17 +27,13 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (count($node->getArgs()) < 2) { return []; } - if ($node->isFirstClassCallable()) { return []; } - if (count($node->getArgs()) < 2) { - return []; - } if ( !$node->name instanceof Node\Identifier || !in_array(strtolower($node->name->name), ['assertequals', 'assertnotequals'], true) @@ -45,6 +41,10 @@ public function processNode(Node $node, Scope $scope): array return []; } + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + return []; + } + $leftType = TypeCombinator::removeNull($scope->getType($node->getArgs()[0]->value)); $rightType = TypeCombinator::removeNull($scope->getType($node->getArgs()[1]->value)); diff --git a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php index 9abbd75a..9acb960e 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -23,17 +23,12 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (count($node->getArgs()) < 2) { return []; } - if ($node->isFirstClassCallable()) { return []; } - - if (count($node->getArgs()) < 2) { - return []; - } if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { return []; } @@ -43,6 +38,10 @@ public function processNode(Node $node, Scope $scope): array return []; } + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + return []; + } + if ($expectedArgumentValue->name->toLowerString() === 'true') { return [ RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(), diff --git a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php index 83807ec9..acfb8e68 100644 --- a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php @@ -23,18 +23,17 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (count($node->getArgs()) < 2) { return []; } - if ($node->isFirstClassCallable()) { return []; } - - if (count($node->getArgs()) < 2) { + if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { return []; } - if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { + + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { return []; } diff --git a/src/Rules/PHPUnit/AssertSameWithCountRule.php b/src/Rules/PHPUnit/AssertSameWithCountRule.php index 9e051cc8..e56ba3df 100644 --- a/src/Rules/PHPUnit/AssertSameWithCountRule.php +++ b/src/Rules/PHPUnit/AssertSameWithCountRule.php @@ -24,23 +24,21 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (count($node->getArgs()) < 2) { return []; } - if ($node->isFirstClassCallable()) { return []; } - - if (count($node->getArgs()) < 2) { + if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { return []; } - if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { + + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { return []; } $right = $node->getArgs()[1]->value; - if ( $right instanceof Node\Expr\FuncCall && $right->name instanceof Node\Name From 23fc42e9bbd39a988e351afa489d5fbe1c2cf280 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 27 Oct 2025 18:52:05 +0100 Subject: [PATCH 2/2] fix --- src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php | 3 +++ src/Rules/PHPUnit/AssertRuleHelper.php | 3 --- src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php | 3 +++ src/Rules/PHPUnit/AssertSameNullExpectedRule.php | 3 +++ src/Rules/PHPUnit/AssertSameWithCountRule.php | 5 ++++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php index c357aefb..c4c23887 100644 --- a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php +++ b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php @@ -27,6 +27,9 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { + return []; + } if (count($node->getArgs()) < 2) { return []; } diff --git a/src/Rules/PHPUnit/AssertRuleHelper.php b/src/Rules/PHPUnit/AssertRuleHelper.php index 442b0655..ecaec91d 100644 --- a/src/Rules/PHPUnit/AssertRuleHelper.php +++ b/src/Rules/PHPUnit/AssertRuleHelper.php @@ -11,9 +11,6 @@ class AssertRuleHelper { - /** - * @phpstan-assert-if-true Node\Expr\MethodCall|Node\Expr\StaticCall $node - */ public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): bool { if ($node instanceof Node\Expr\MethodCall) { diff --git a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php index 9acb960e..fd76f7c4 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -23,6 +23,9 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { + return []; + } if (count($node->getArgs()) < 2) { return []; } diff --git a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php index acfb8e68..3362f443 100644 --- a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php @@ -23,6 +23,9 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { + return []; + } if (count($node->getArgs()) < 2) { return []; } diff --git a/src/Rules/PHPUnit/AssertSameWithCountRule.php b/src/Rules/PHPUnit/AssertSameWithCountRule.php index e56ba3df..0e6d83cf 100644 --- a/src/Rules/PHPUnit/AssertSameWithCountRule.php +++ b/src/Rules/PHPUnit/AssertSameWithCountRule.php @@ -24,13 +24,16 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { + return []; + } if (count($node->getArgs()) < 2) { return []; } if ($node->isFirstClassCallable()) { return []; } - if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { + if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { return []; }