|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\Rule; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\BinaryOp\Div; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Minus; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\Mod; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\Mul; |
| 11 | +use PhpParser\Node\Expr\BinaryOp\Plus; |
| 12 | +use PhpParser\Node\Expr\BinaryOp\Pow; |
| 13 | +use PhpParser\Node\Expr\UnaryMinus; |
| 14 | +use PhpParser\Node\Expr\UnaryPlus; |
| 15 | +use PHPStan\Analyser\Scope; |
| 16 | +use PHPStan\Rules\Rule; |
| 17 | +use PHPStan\Rules\RuleError; |
| 18 | +use PHPStan\Rules\RuleErrorBuilder; |
| 19 | +use PHPStan\Type\FloatType; |
| 20 | +use PHPStan\Type\IntegerType; |
| 21 | +use PHPStan\Type\Type; |
| 22 | +use PHPStan\Type\UnionType; |
| 23 | +use PHPStan\Type\VerbosityLevel; |
| 24 | +use function sprintf; |
| 25 | + |
| 26 | +/** |
| 27 | + * @implements Rule<Node> |
| 28 | + */ |
| 29 | +class ForbidArithmeticOperationOnNonNumberRule implements Rule |
| 30 | +{ |
| 31 | + |
| 32 | + private bool $allowNumericString; |
| 33 | + |
| 34 | + public function __construct(bool $allowNumericString) |
| 35 | + { |
| 36 | + $this->allowNumericString = $allowNumericString; |
| 37 | + } |
| 38 | + |
| 39 | + public function getNodeType(): string |
| 40 | + { |
| 41 | + return Node::class; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return list<RuleError> |
| 46 | + */ |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + if ( |
| 50 | + $node instanceof UnaryPlus |
| 51 | + || $node instanceof UnaryMinus |
| 52 | + ) { |
| 53 | + return $this->processUnary($node->expr, $scope, $node instanceof UnaryMinus ? '-' : '+'); |
| 54 | + } |
| 55 | + |
| 56 | + if ( |
| 57 | + $node instanceof Plus |
| 58 | + || $node instanceof Minus |
| 59 | + || $node instanceof Div |
| 60 | + || $node instanceof Mul |
| 61 | + || $node instanceof Mod |
| 62 | + || $node instanceof Pow |
| 63 | + ) { |
| 64 | + return $this->processBinary($node->left, $node->right, $scope, $node->getOperatorSigil()); |
| 65 | + } |
| 66 | + |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return list<RuleError> |
| 72 | + */ |
| 73 | + private function processUnary(Expr $expr, Scope $scope, string $operator): array |
| 74 | + { |
| 75 | + $exprType = $scope->getType($expr); |
| 76 | + |
| 77 | + if (!$this->isNumeric($exprType)) { |
| 78 | + $errorMessage = sprintf( |
| 79 | + 'Using %s over non-number (%s)', |
| 80 | + $operator, |
| 81 | + $exprType->describe(VerbosityLevel::typeOnly()), |
| 82 | + ); |
| 83 | + $error = RuleErrorBuilder::message($errorMessage) |
| 84 | + ->identifier('shipmonk.arithmeticOnNonNumber') |
| 85 | + ->build(); |
| 86 | + return [$error]; |
| 87 | + } |
| 88 | + |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return list<RuleError> |
| 94 | + */ |
| 95 | + private function processBinary(Expr $left, Expr $right, Scope $scope, string $operator): array |
| 96 | + { |
| 97 | + $leftType = $scope->getType($left); |
| 98 | + $rightType = $scope->getType($right); |
| 99 | + |
| 100 | + if ($operator === '+' && $leftType->isArray()->yes() && $rightType->isArray()->yes()) { |
| 101 | + return []; // array merge syntax |
| 102 | + } |
| 103 | + |
| 104 | + if ( |
| 105 | + $operator === '%' && |
| 106 | + (!$leftType->isInteger()->yes() || !$rightType->isInteger()->yes()) |
| 107 | + ) { |
| 108 | + return $this->buildBinaryErrors($operator, 'non-integer', $leftType, $rightType); |
| 109 | + } |
| 110 | + |
| 111 | + if (!$this->isNumeric($leftType) || !$this->isNumeric($rightType)) { |
| 112 | + return $this->buildBinaryErrors($operator, 'non-number', $leftType, $rightType); |
| 113 | + } |
| 114 | + |
| 115 | + return []; |
| 116 | + } |
| 117 | + |
| 118 | + private function isNumeric(Type $type): bool |
| 119 | + { |
| 120 | + $int = new IntegerType(); |
| 121 | + $float = new FloatType(); |
| 122 | + $intOrFloat = new UnionType([$int, $float]); |
| 123 | + |
| 124 | + return $int->isSuperTypeOf($type)->yes() |
| 125 | + || $float->isSuperTypeOf($type)->yes() |
| 126 | + || $intOrFloat->isSuperTypeOf($type)->yes() |
| 127 | + || ($this->allowNumericString && $type->isNumericString()->yes()); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @return list<RuleError> |
| 132 | + */ |
| 133 | + private function buildBinaryErrors(string $operator, string $type, Type $leftType, Type $rightType): array |
| 134 | + { |
| 135 | + $errorMessage = sprintf( |
| 136 | + 'Using %s over %s (%s %s %s)', |
| 137 | + $operator, |
| 138 | + $type, |
| 139 | + $leftType->describe(VerbosityLevel::typeOnly()), |
| 140 | + $operator, |
| 141 | + $rightType->describe(VerbosityLevel::typeOnly()), |
| 142 | + ); |
| 143 | + $error = RuleErrorBuilder::message($errorMessage) |
| 144 | + ->identifier('shipmonk.arithmeticOnNonNumber') |
| 145 | + ->build(); |
| 146 | + |
| 147 | + return [$error]; |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments