diff --git a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php index b03f0830..c4c23887 100644 --- a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php +++ b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php @@ -27,17 +27,16 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { return []; } - - if ($node->isFirstClassCallable()) { + if (count($node->getArgs()) < 2) { return []; } - - if (count($node->getArgs()) < 2) { + if ($node->isFirstClassCallable()) { return []; } + if ( !$node->name instanceof Node\Identifier || !in_array(strtolower($node->name->name), ['assertequals', 'assertnotequals'], true) @@ -45,6 +44,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/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 9abbd75a..fd76f7c4 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -23,15 +23,13 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { return []; } - - if ($node->isFirstClassCallable()) { + if (count($node->getArgs()) < 2) { return []; } - - if (count($node->getArgs()) < 2) { + if ($node->isFirstClassCallable()) { return []; } if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { @@ -43,6 +41,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..3362f443 100644 --- a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php @@ -23,21 +23,23 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { return []; } - - if ($node->isFirstClassCallable()) { + if (count($node->getArgs()) < 2) { return []; } - - if (count($node->getArgs()) < 2) { + if ($node->isFirstClassCallable()) { return []; } if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') { return []; } + if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + return []; + } + $expectedArgumentValue = $node->getArgs()[0]->value; if (!($expectedArgumentValue instanceof ConstFetch)) { return []; diff --git a/src/Rules/PHPUnit/AssertSameWithCountRule.php b/src/Rules/PHPUnit/AssertSameWithCountRule.php index 9e051cc8..0e6d83cf 100644 --- a/src/Rules/PHPUnit/AssertSameWithCountRule.php +++ b/src/Rules/PHPUnit/AssertSameWithCountRule.php @@ -24,23 +24,24 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { + if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) { + return []; + } + 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