forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertSameBooleanExpectedRule.php
More file actions
85 lines (70 loc) · 1.99 KB
/
AssertSameBooleanExpectedRule.php
File metadata and controls
85 lines (70 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\ConstFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;
/**
* @implements Rule<CallLike>
*/
class AssertSameBooleanExpectedRule implements Rule
{
public function getNodeType(): string
{
return CallLike::class;
}
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') {
return [];
}
$expectedArgumentValue = $node->getArgs()[0]->value;
if (!($expectedArgumentValue instanceof ConstFetch)) {
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')
->fixNode($node, static function (CallLike $node) {
$node->name = new Node\Identifier('assertTrue');
$args = $node->getArgs();
unset($args[0]);
$node->args = $args;
return $node;
})
->build(),
];
}
if ($expectedArgumentValue->name->toLowerString() === 'false') {
return [
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')
->identifier('phpunit.assertFalse')
->fixNode($node, static function (CallLike $node) {
$node->name = new Node\Identifier('assertFalse');
$args = $node->getArgs();
unset($args[0]);
$node->args = $args;
return $node;
})
->build(),
];
}
return [];
}
}