1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 15:09:04 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonComparisonOpAnalyzer.php

167 lines
5.1 KiB
PHP
Raw Normal View History

2020-05-19 00:57:02 +02:00
<?php
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;
/**
* @internal
*/
class NonComparisonOpAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\BinaryOp $stmt,
Context $context
) : void {
$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);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'nondivop'
);
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
|| $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
) {
NonDivArithmeticOpAnalyzer::analyze(
$statements_analyzer,
$statements_analyzer->node_data,
$stmt->left,
$stmt->right,
$stmt,
$result_type,
$context
);
if (!$result_type) {
$result_type = new Type\Union([new Type\Atomic\TInt(), new Type\Atomic\TFloat()]);
2020-05-19 00:57:02 +02:00
}
$statements_analyzer->node_data->setType($stmt, $result_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'nondivop'
);
2020-05-19 00:57:02 +02:00
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor) {
if ($stmt_left_type->hasBool() || $stmt_right_type->hasBool()) {
$statements_analyzer->node_data->setType($stmt, Type::getInt());
}
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'xor'
);
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());
}
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'xor'
);
2020-05-19 00:57:02 +02:00
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
NonDivArithmeticOpAnalyzer::analyze(
$statements_analyzer,
$statements_analyzer->node_data,
$stmt->left,
$stmt->right,
$stmt,
$result_type,
$context
);
if (!$result_type) {
$result_type = new Type\Union([new Type\Atomic\TInt(), new Type\Atomic\TFloat()]);
2020-05-19 00:57:02 +02:00
}
$statements_analyzer->node_data->setType($stmt, $result_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'div'
);
2020-05-19 00:57:02 +02:00
return;
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
NonDivArithmeticOpAnalyzer::analyze(
$statements_analyzer,
$statements_analyzer->node_data,
$stmt->left,
$stmt->right,
$stmt,
$result_type,
$context
);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->left,
$stmt->right,
'or'
);
2020-05-19 00:57:02 +02:00
}
}
}