Skip to content

Commit dd87f2e

Browse files
authored
Assert*Rules: Do cheap checks first (#247)
1 parent 5c89d74 commit dd87f2e

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,27 @@ public function getNodeType(): string
2727

2828
public function processNode(Node $node, Scope $scope): array
2929
{
30-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
30+
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
3131
return [];
3232
}
33-
34-
if ($node->isFirstClassCallable()) {
33+
if (count($node->getArgs()) < 2) {
3534
return [];
3635
}
37-
38-
if (count($node->getArgs()) < 2) {
36+
if ($node->isFirstClassCallable()) {
3937
return [];
4038
}
39+
4140
if (
4241
!$node->name instanceof Node\Identifier
4342
|| !in_array(strtolower($node->name->name), ['assertequals', 'assertnotequals'], true)
4443
) {
4544
return [];
4645
}
4746

47+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
48+
return [];
49+
}
50+
4851
$leftType = TypeCombinator::removeNull($scope->getType($node->getArgs()[0]->value));
4952
$rightType = TypeCombinator::removeNull($scope->getType($node->getArgs()[1]->value));
5053

src/Rules/PHPUnit/AssertRuleHelper.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
class AssertRuleHelper
1212
{
1313

14-
/**
15-
* @phpstan-assert-if-true Node\Expr\MethodCall|Node\Expr\StaticCall $node
16-
*/
1714
public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): bool
1815
{
1916
if ($node instanceof Node\Expr\MethodCall) {

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ public function getNodeType(): string
2323

2424
public function processNode(Node $node, Scope $scope): array
2525
{
26-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
26+
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
2727
return [];
2828
}
29-
30-
if ($node->isFirstClassCallable()) {
29+
if (count($node->getArgs()) < 2) {
3130
return [];
3231
}
33-
34-
if (count($node->getArgs()) < 2) {
32+
if ($node->isFirstClassCallable()) {
3533
return [];
3634
}
3735
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
@@ -43,6 +41,10 @@ public function processNode(Node $node, Scope $scope): array
4341
return [];
4442
}
4543

44+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
45+
return [];
46+
}
47+
4648
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4749
return [
4850
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),

src/Rules/PHPUnit/AssertSameNullExpectedRule.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ public function getNodeType(): string
2323

2424
public function processNode(Node $node, Scope $scope): array
2525
{
26-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
26+
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
2727
return [];
2828
}
29-
30-
if ($node->isFirstClassCallable()) {
29+
if (count($node->getArgs()) < 2) {
3130
return [];
3231
}
33-
34-
if (count($node->getArgs()) < 2) {
32+
if ($node->isFirstClassCallable()) {
3533
return [];
3634
}
3735
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
3836
return [];
3937
}
4038

39+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
40+
return [];
41+
}
42+
4143
$expectedArgumentValue = $node->getArgs()[0]->value;
4244
if (!($expectedArgumentValue instanceof ConstFetch)) {
4345
return [];

src/Rules/PHPUnit/AssertSameWithCountRule.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,24 @@ public function getNodeType(): string
2424

2525
public function processNode(Node $node, Scope $scope): array
2626
{
27-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
27+
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
28+
return [];
29+
}
30+
if (count($node->getArgs()) < 2) {
2831
return [];
2932
}
30-
3133
if ($node->isFirstClassCallable()) {
3234
return [];
3335
}
34-
35-
if (count($node->getArgs()) < 2) {
36+
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
3637
return [];
3738
}
38-
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
39+
40+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
3941
return [];
4042
}
4143

4244
$right = $node->getArgs()[1]->value;
43-
4445
if (
4546
$right instanceof Node\Expr\FuncCall
4647
&& $right->name instanceof Node\Name

0 commit comments

Comments
 (0)