|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Type\BooleanType; |
| 8 | +use PHPStan\Type\FloatType; |
| 9 | +use PHPStan\Type\IntegerType; |
| 10 | +use PHPStan\Type\StringType; |
| 11 | + |
| 12 | +class AssertEqualsIsDiscouragedRule implements \PHPStan\Rules\Rule |
| 13 | +{ |
| 14 | + |
| 15 | + public function getNodeType(): string |
| 16 | + { |
| 17 | + return \PhpParser\NodeAbstract::class; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $node |
| 22 | + * @param \PHPStan\Analyser\Scope $scope |
| 23 | + * @return string[] errors |
| 24 | + */ |
| 25 | + public function processNode(Node $node, Scope $scope): array |
| 26 | + { |
| 27 | + if (!AssertRuleHelper::isMethodOrStaticCallOnTestCase($node, $scope)) { |
| 28 | + return []; |
| 29 | + } |
| 30 | + |
| 31 | + if (count($node->args) < 2) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + if (!is_string($node->name) || strtolower($node->name) !== 'assertequals') { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + $leftType = $scope->getType($node->args[0]->value); |
| 39 | + $rightType = $scope->getType($node->args[1]->value); |
| 40 | + |
| 41 | + if ( |
| 42 | + ($leftType instanceof BooleanType && $rightType instanceof BooleanType) |
| 43 | + || ($leftType instanceof IntegerType && $rightType instanceof IntegerType) |
| 44 | + || ($leftType instanceof StringType && $rightType instanceof StringType) |
| 45 | + ) { |
| 46 | + $typeDescription = $leftType->describe(); |
| 47 | + if ($leftType instanceof BooleanType) { |
| 48 | + $typeDescription = 'bool'; |
| 49 | + } |
| 50 | + return [ |
| 51 | + sprintf( |
| 52 | + 'You should use assertSame instead of assertEquals, because both values are of the same type "%s"', |
| 53 | + $typeDescription |
| 54 | + ), |
| 55 | + ]; |
| 56 | + } |
| 57 | + if ( |
| 58 | + ($leftType instanceof FloatType && $rightType instanceof FloatType) |
| 59 | + && count($node->args) < 4 // is not using delta for comparing floats |
| 60 | + ) { |
| 61 | + return [ |
| 62 | + 'You should use assertSame instead of assertEquals, because both values are of the same type "float" and you are not using $delta argument', |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + if (!$node->hasAttribute('comments')) { |
| 67 | + return [ |
| 68 | + 'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...', |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + /** @var \PhpParser\Comment[] $comments */ |
| 73 | + $comments = $node->getAttribute('comments'); |
| 74 | + $comment = $comments[count($comments) - 1]; |
| 75 | + |
| 76 | + // the comment should be on the line above the assertEquals() |
| 77 | + if ($comment->getLine() !== ($node->getLine() - 1)) { |
| 78 | + return [ |
| 79 | + 'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (The comment is not directly above the assertEquals)', |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + if (!preg_match('~^//\s+assertEquals because(.*)~', $comment->getReformattedText())) { |
| 84 | + return [ |
| 85 | + 'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)', |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments