1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix bitwise operations on strings

This commit is contained in:
Matt Brown 2018-04-16 18:19:18 -04:00
parent 55153f9542
commit b28cc19fd2
2 changed files with 24 additions and 11 deletions

View File

@ -397,16 +397,27 @@ class BinaryOpChecker
// let's do some fun type assignment
if (isset($stmt->left->inferredType) && isset($stmt->right->inferredType)) {
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft ||
$stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight
if ($stmt->left->inferredType->hasString()
&& $stmt->right->inferredType->hasString()
&& ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
)
) {
$stmt->inferredType = Type::getString();
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow
|| (($stmt->left->inferredType->hasInt() || $stmt->right->inferredType->hasInt())
&& ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight
)
)
) {
self::analyzeNonDivArithmenticOp(
$statements_checker,

View File

@ -50,13 +50,15 @@ class BinaryOperationTest extends TestCase
$b = 2 | 3;
$c = 4 ^ 3;
$d = 1 << 2;
$e = 15 >> 2;',
$e = 15 >> 2;
$f = "a" & "b";',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$c' => 'int',
'$d' => 'int',
'$e' => 'int',
'$f' => 'string',
],
],
];