forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssertSameDifferentTypesRule.php
50 lines (40 loc) · 1.09 KB
/
AssertSameDifferentTypesRule.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
class AssertSameDifferentTypesRule implements \PHPStan\Rules\Rule
{
public function getNodeType(): string
{
return \PhpParser\NodeAbstract::class;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) {
return [];
}
if (count($node->args) < 2) {
return [];
}
if (!is_string($node->name) || strtolower($node->name) !== 'assertsame') {
return [];
}
$leftType = $scope->getType($node->args[0]->value);
$rightType = $scope->getType($node->args[1]->value);
if ($leftType->isSuperTypeOf($rightType)->no()) {
return [
sprintf(
'Call to assertSame() with different types %s and %s will always result in test failure.',
$leftType->describe(),
$rightType->describe()
),
];
}
return [];
}
}