2020-05-19 00:57:02 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-05-19 00:57:02 +02:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Context;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\BinaryOpAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-05-19 00:57:02 +02:00
|
|
|
use Psalm\Type;
|
2021-12-13 04:45:57 +01:00
|
|
|
use Psalm\Type\Atomic\TFloat;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
2021-12-13 16:28:14 +01:00
|
|
|
use Psalm\Type\Union;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class NonComparisonOpAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\BinaryOp $stmt,
|
|
|
|
Context $context
|
2021-12-05 18:51:26 +01:00
|
|
|
): void {
|
2020-05-19 00:57:02 +02:00
|
|
|
$stmt_left_type = $statements_analyzer->node_data->getType($stmt->left);
|
|
|
|
$stmt_right_type = $statements_analyzer->node_data->getType($stmt->right);
|
|
|
|
|
|
|
|
if (!$stmt_left_type || !$stmt_right_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|
|
|
|
)
|
|
|
|
&& $stmt_left_type->hasString()
|
|
|
|
&& $stmt_right_type->hasString()
|
|
|
|
) {
|
|
|
|
$stmt_type = Type::getString();
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2021-07-12 19:09:20 +02:00
|
|
|
BinaryOpAnalyzer::addDataFlow(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
2022-12-18 17:15:15 +01:00
|
|
|
'nondivop',
|
2021-07-12 19:09:20 +02:00
|
|
|
);
|
2020-05-19 00:57:02 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-11-30 03:43:49 +01:00
|
|
|
|| $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
|
2020-05-19 00:57:02 +02:00
|
|
|
) {
|
2021-09-06 23:18:17 +02:00
|
|
|
ArithmeticOpAnalyzer::analyze(
|
2020-05-19 00:57:02 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
2022-12-18 17:15:15 +01:00
|
|
|
$context,
|
2020-05-19 00:57:02 +02:00
|
|
|
);
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
if (!$result_type) {
|
2021-12-13 16:28:14 +01:00
|
|
|
$result_type = new Union([new TInt(), new TFloat()]);
|
2020-05-19 00:57:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $result_type);
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
BinaryOpAnalyzer::addDataFlow(
|
2020-09-28 06:45:02 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
2022-12-18 17:15:15 +01:00
|
|
|
'nondivop',
|
2020-09-28 06:45:02 +02:00
|
|
|
);
|
|
|
|
|
2020-05-19 00:57:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor) {
|
|
|
|
if ($stmt_left_type->hasBool() || $stmt_right_type->hasBool()) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
BinaryOpAnalyzer::addDataFlow(
|
2020-09-28 06:45:02 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
2022-12-18 17:15:15 +01:00
|
|
|
'xor',
|
2020-09-28 06:45:02 +02:00
|
|
|
);
|
|
|
|
|
2020-05-19 00:57:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
|
2021-09-06 23:18:17 +02:00
|
|
|
ArithmeticOpAnalyzer::analyze(
|
2020-05-19 00:57:02 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$statements_analyzer->node_data,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
2022-12-18 17:15:15 +01:00
|
|
|
$context,
|
2020-05-19 00:57:02 +02:00
|
|
|
);
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
if (!$result_type) {
|
2021-12-13 16:28:14 +01:00
|
|
|
$result_type = new Union([new TInt(), new TFloat()]);
|
2020-05-19 00:57:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $result_type);
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
BinaryOpAnalyzer::addDataFlow(
|
2020-09-28 06:45:02 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
2022-12-18 17:15:15 +01:00
|
|
|
'div',
|
2020-09-28 06:45:02 +02:00
|
|
|
);
|
|
|
|
|
2020-05-19 00:57:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|