Skip to content

Commit 0938195

Browse files
committed
Assert*Rules: Do cheap checks first
1 parent 5c89d74 commit 0938195

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

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

2828
public function processNode(Node $node, Scope $scope): array
2929
{
30-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
30+
if (count($node->getArgs()) < 2) {
3131
return [];
3232
}
33-
3433
if ($node->isFirstClassCallable()) {
3534
return [];
3635
}
3736

38-
if (count($node->getArgs()) < 2) {
39-
return [];
40-
}
4137
if (
4238
!$node->name instanceof Node\Identifier
4339
|| !in_array(strtolower($node->name->name), ['assertequals', 'assertnotequals'], true)
4440
) {
4541
return [];
4642
}
4743

44+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
45+
return [];
46+
}
47+
4848
$leftType = TypeCombinator::removeNull($scope->getType($node->getArgs()[0]->value));
4949
$rightType = TypeCombinator::removeNull($scope->getType($node->getArgs()[1]->value));
5050

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

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

2424
public function processNode(Node $node, Scope $scope): array
2525
{
26-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
26+
if (count($node->getArgs()) < 2) {
2727
return [];
2828
}
29-
3029
if ($node->isFirstClassCallable()) {
3130
return [];
3231
}
33-
34-
if (count($node->getArgs()) < 2) {
35-
return [];
36-
}
3732
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
3833
return [];
3934
}
@@ -43,6 +38,10 @@ public function processNode(Node $node, Scope $scope): array
4338
return [];
4439
}
4540

41+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
42+
return [];
43+
}
44+
4645
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4746
return [
4847
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),

src/Rules/PHPUnit/AssertSameNullExpectedRule.php

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

2424
public function processNode(Node $node, Scope $scope): array
2525
{
26-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
26+
if (count($node->getArgs()) < 2) {
2727
return [];
2828
}
29-
3029
if ($node->isFirstClassCallable()) {
3130
return [];
3231
}
33-
34-
if (count($node->getArgs()) < 2) {
32+
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
3533
return [];
3634
}
37-
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
35+
36+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
3837
return [];
3938
}
4039

src/Rules/PHPUnit/AssertSameWithCountRule.php

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

2525
public function processNode(Node $node, Scope $scope): array
2626
{
27-
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
27+
if (count($node->getArgs()) < 2) {
2828
return [];
2929
}
30-
3130
if ($node->isFirstClassCallable()) {
3231
return [];
3332
}
34-
35-
if (count($node->getArgs()) < 2) {
33+
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
3634
return [];
3735
}
38-
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
36+
37+
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
3938
return [];
4039
}
4140

4241
$right = $node->getArgs()[1]->value;
43-
4442
if (
4543
$right instanceof Node\Expr\FuncCall
4644
&& $right->name instanceof Node\Name

0 commit comments

Comments
 (0)