-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAssertSameWithCountRule.php
More file actions
166 lines (143 loc) · 4.32 KB
/
AssertSameWithCountRule.php
File metadata and controls
166 lines (143 loc) · 4.32 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use Countable;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function count;
use const COUNT_NORMAL;
/**
* @implements Rule<CallLike>
*/
class AssertSameWithCountRule 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 [];
}
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}
$right = $node->getArgs()[1]->value;
if (self::isCountFunctionCall($right, $scope)) {
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
->identifier('phpunit.assertCount')
->fixNode($node, static function (CallLike $node) use ($scope) {
$newArgs = self::rewriteArgs($node->args, $scope);
if ($newArgs === null) {
return $node;
}
$node->name = new Node\Identifier('assertCount');
$node->args = $newArgs;
return $node;
})
->build(),
];
}
if (self::isCountableMethodCall($right, $scope)) {
return [
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
->identifier('phpunit.assertCount')
->fixNode($node, static function (CallLike $node) use ($scope) {
$newArgs = self::rewriteArgs($node->args, $scope);
if ($newArgs === null) {
return $node;
}
$node->name = new Node\Identifier('assertCount');
$node->args = $newArgs;
return $node;
})
->build(),
];
}
return [];
}
/**
* @phpstan-assert-if-true Node\Expr\FuncCall $expr
*/
private static function isCountFunctionCall(Node\Expr $expr, Scope $scope): bool
{
return $expr instanceof Node\Expr\FuncCall
&& $expr->name instanceof Node\Name
&& $expr->name->toLowerString() === 'count'
&& count($expr->getArgs()) >= 1
&& self::isNormalCount($expr, $scope->getType($expr->getArgs()[0]->value), $scope)->yes();
}
/**
* @phpstan-assert-if-true Node\Expr\MethodCall $expr
*/
private static function isCountableMethodCall(Node\Expr $expr, Scope $scope): bool
{
if (
$expr instanceof Node\Expr\MethodCall
&& $expr->name instanceof Node\Identifier
&& $expr->name->toLowerString() === 'count'
&& count($expr->getArgs()) === 0
) {
$type = $scope->getType($expr->var);
if ((new ObjectType(Countable::class))->isSuperTypeOf($type)->yes()) {
return true;
}
}
return false;
}
private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $countedType, Scope $scope): TrinaryLogic
{
if (count($countFuncCall->getArgs()) === 1) {
$isNormalCount = TrinaryLogic::createYes();
} else {
$mode = $scope->getType($countFuncCall->getArgs()[1]->value);
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->result->or($countedType->getIterableValueType()->isArray()->negate());
}
return $isNormalCount;
}
/**
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
* @return list<Node\Arg|Node\VariadicPlaceholder>
*/
private static function rewriteArgs(array $args, Scope $scope): ?array
{
$newArgs = [];
for ($i = 0; $i < count($args); $i++) {
if (
$args[$i] instanceof Node\Arg
&& $args[$i]->value instanceof CallLike
) {
$value = $args[$i]->value;
if (self::isCountFunctionCall($value, $scope)) {
if (count($value->getArgs()) !== 1) {
return null;
}
$newArgs[] = new Node\Arg($value->getArgs()[0]->value);
continue;
} elseif (self::isCountableMethodCall($value, $scope)) {
$newArgs[] = new Node\Arg($value->var);
continue;
}
}
$newArgs[] = $args[$i];
}
return $newArgs;
}
}