forked from phpstan/phpstan-strict-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperandsInArithmeticAddition.php
39 lines (29 loc) · 1003 Bytes
/
OperandsInArithmeticAddition.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
class OperandsInArithmeticAddition implements \PHPStan\Rules\Rule
{
public function getNodeType(): string
{
return \PhpParser\Node\Expr\BinaryOp\Plus::class;
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);
if (OperatorRuleHelper::isValidForArithmeticOperation($leftType, $rightType)) {
return [];
}
$mixedArrayType = new ArrayType(new MixedType(), new MixedType());
if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) {
return [];
}
return ['Only numeric types or arrays are allowed in arithmetic addition.'];
}
}