2018-01-14 18:09:40 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\ArrayAssignmentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2018-03-18 21:39:34 +01:00
|
|
|
use Psalm\Issue\FalseOperand;
|
2018-03-18 19:42:23 +01:00
|
|
|
use Psalm\Issue\ImplicitToStringCast;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\InvalidOperand;
|
|
|
|
use Psalm\Issue\MixedOperand;
|
|
|
|
use Psalm\Issue\NullOperand;
|
2018-03-18 21:39:34 +01:00
|
|
|
use Psalm\Issue\PossiblyFalseOperand;
|
|
|
|
use Psalm\Issue\PossiblyInvalidOperand;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Issue\PossiblyNullOperand;
|
2019-09-01 23:49:37 +02:00
|
|
|
use Psalm\Issue\StringIncrement;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Type;
|
2018-05-07 07:26:06 +02:00
|
|
|
use Psalm\Type\Algebra;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
2018-03-08 20:04:00 +01:00
|
|
|
use Psalm\Type\Atomic\TFalse;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Atomic\TFloat;
|
2019-10-09 00:44:46 +02:00
|
|
|
use Psalm\Type\Atomic\TList;
|
2019-02-22 03:40:06 +01:00
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Atomic\TInt;
|
|
|
|
use Psalm\Type\Atomic\TMixed;
|
2018-05-05 18:59:30 +02:00
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Atomic\TNull;
|
2018-02-16 01:50:50 +01:00
|
|
|
use Psalm\Type\Atomic\TNumeric;
|
2018-01-14 18:09:40 +01:00
|
|
|
use Psalm\Type\Reconciler;
|
2019-08-10 19:22:21 +02:00
|
|
|
use Psalm\Internal\Type\AssertionReconciler;
|
2018-11-18 18:41:47 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_merge;
|
|
|
|
use function array_diff_key;
|
|
|
|
use function array_filter;
|
|
|
|
use const ARRAY_FILTER_USE_KEY;
|
|
|
|
use function array_intersect_key;
|
|
|
|
use function array_values;
|
|
|
|
use function array_map;
|
|
|
|
use function array_keys;
|
|
|
|
use function preg_match;
|
|
|
|
use function preg_quote;
|
|
|
|
use function strtolower;
|
|
|
|
use function strlen;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class BinaryOpAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr\BinaryOp $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param int $nesting
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr\BinaryOp $stmt,
|
|
|
|
Context $context,
|
|
|
|
$nesting = 0
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = null;
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat && $nesting > 20) {
|
|
|
|
// ignore deeply-nested string concatenation
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
) {
|
2018-05-07 20:52:45 +02:00
|
|
|
$left_clauses = Algebra::getFormula(
|
2019-12-08 22:35:56 +01:00
|
|
|
\spl_object_id($stmt->left),
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->left,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$pre_referenced_var_ids = $context->referenced_var_ids;
|
2018-02-08 20:46:06 +01:00
|
|
|
$original_vars_in_scope = $context->vars_in_scope;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$pre_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$left_context = clone $context;
|
|
|
|
|
|
|
|
$left_context->referenced_var_ids = [];
|
|
|
|
$left_context->assigned_var_ids = [];
|
|
|
|
|
|
|
|
/** @var list<string> */
|
|
|
|
$left_context->reconciled_expression_clauses = [];
|
|
|
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $left_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
foreach ($left_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($left_context->assigned_var_ids[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 17:58:15 +02:00
|
|
|
/** @var array<string, bool> */
|
2019-12-08 06:49:34 +01:00
|
|
|
$new_referenced_var_ids = $left_context->referenced_var_ids;
|
2018-01-14 18:09:40 +01:00
|
|
|
$context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$new_assigned_var_ids = array_diff_key($left_context->assigned_var_ids, $pre_assigned_var_ids);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$new_referenced_var_ids = array_diff_key($new_referenced_var_ids, $new_assigned_var_ids);
|
|
|
|
|
2018-02-08 20:46:06 +01:00
|
|
|
// remove all newly-asserted var ids too
|
|
|
|
$new_referenced_var_ids = array_filter(
|
|
|
|
$new_referenced_var_ids,
|
|
|
|
/**
|
|
|
|
* @param string $var_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function ($var_id) use ($original_vars_in_scope) {
|
|
|
|
return isset($original_vars_in_scope[$var_id]);
|
|
|
|
},
|
|
|
|
ARRAY_FILTER_USE_KEY
|
|
|
|
);
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$context_clauses = array_merge($left_context->clauses, $left_clauses);
|
|
|
|
|
|
|
|
if ($left_context->reconciled_expression_clauses) {
|
|
|
|
$reconciled_expression_clauses = $left_context->reconciled_expression_clauses;
|
|
|
|
|
|
|
|
$context_clauses = array_values(
|
|
|
|
array_filter(
|
|
|
|
$context_clauses,
|
|
|
|
function ($c) use ($reconciled_expression_clauses) {
|
|
|
|
return !\in_array($c->getHash(), $reconciled_expression_clauses);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$simplified_clauses = Algebra::simplifyCNF($context_clauses);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 22:35:56 +01:00
|
|
|
$active_left_assertions = [];
|
|
|
|
|
|
|
|
$left_type_assertions = Algebra::getTruthsFromFormula(
|
|
|
|
$simplified_clauses,
|
|
|
|
\spl_object_id($stmt->left),
|
|
|
|
$new_referenced_var_ids,
|
|
|
|
$active_left_assertions
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$changed_var_ids = [];
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$right_context = clone $left_context;
|
2018-12-17 21:23:56 +01:00
|
|
|
|
|
|
|
if ($left_type_assertions) {
|
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x && $x->foo())
|
|
|
|
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
|
|
|
|
$left_type_assertions,
|
2019-12-08 22:35:56 +01:00
|
|
|
$active_left_assertions,
|
2018-12-17 21:23:56 +01:00
|
|
|
$context->vars_in_scope,
|
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
|
|
|
$statements_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-12-17 21:23:56 +01:00
|
|
|
$context->inside_loop,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$right_context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
|
|
|
|
if ($context->if_scope) {
|
|
|
|
$context->if_scope->if_cond_changed_var_ids += $changed_var_ids;
|
|
|
|
}
|
2018-12-17 21:23:56 +01:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$partitioned_clauses = Context::removeReconciledClauses($left_clauses, $changed_var_ids);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$right_context->clauses = $partitioned_clauses[0];
|
|
|
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $right_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->referenced_var_ids = array_merge(
|
2019-12-08 06:49:34 +01:00
|
|
|
$right_context->referenced_var_ids,
|
|
|
|
$left_context->referenced_var_ids
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
2019-12-08 06:49:34 +01:00
|
|
|
$context->unreferenced_vars = $right_context->unreferenced_vars;
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($context->inside_conditional) {
|
|
|
|
$context->updateChecks($right_context);
|
|
|
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$right_context->vars_possibly_in_scope,
|
|
|
|
$left_context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
|
|
|
|
$context->assigned_var_ids = array_merge(
|
|
|
|
$left_context->assigned_var_ids,
|
|
|
|
$right_context->assigned_var_ids
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($context->if_context && !$context->inside_negation) {
|
|
|
|
$context->vars_in_scope = $right_context->vars_in_scope;
|
|
|
|
$if_context = $context->if_context;
|
|
|
|
|
|
|
|
foreach ($right_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (!isset($if_context->vars_in_scope[$var_id])) {
|
|
|
|
$if_context->vars_in_scope[$var_id] = $type;
|
|
|
|
} elseif (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$if_context->vars_in_scope[$var_id] = $context->vars_in_scope[$var_id];
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$if_context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$if_context->referenced_var_ids
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$if_context->assigned_var_ids = array_merge(
|
2018-01-14 18:09:40 +01:00
|
|
|
$context->assigned_var_ids,
|
2019-12-08 06:49:34 +01:00
|
|
|
$if_context->assigned_var_ids
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
2019-12-08 06:49:34 +01:00
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
$if_context->unreferenced_vars = $context->unreferenced_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
$if_context->reconciled_expression_clauses = array_merge(
|
2019-12-08 16:17:40 +01:00
|
|
|
$if_context->reconciled_expression_clauses,
|
2019-12-08 06:49:34 +01:00
|
|
|
array_map(
|
|
|
|
function ($c) {
|
|
|
|
return $c->getHash();
|
|
|
|
},
|
|
|
|
$partitioned_clauses[1]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$if_context->updateChecks($context);
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope = $left_context->vars_in_scope;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
|
|
|
) {
|
|
|
|
$pre_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = [];
|
|
|
|
|
|
|
|
$pre_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$left_context = clone $context;
|
|
|
|
$left_context->parent_context = $context;
|
|
|
|
$left_context->assigned_var_ids = [];
|
2018-09-10 06:13:59 +02:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $left_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
foreach ($left_context->vars_in_scope as $var_id => $type) {
|
2018-09-10 06:13:59 +02:00
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
2019-12-08 06:49:34 +01:00
|
|
|
if (isset($left_context->assigned_var_ids[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = clone $type;
|
|
|
|
}
|
2018-09-10 06:13:59 +02:00
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
2019-12-08 06:49:34 +01:00
|
|
|
$type,
|
|
|
|
$codebase
|
2018-09-10 06:13:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($context->collect_references) {
|
|
|
|
$context->unreferenced_vars = $left_context->unreferenced_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_referenced_var_ids = $left_context->referenced_var_ids;
|
|
|
|
$left_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
|
2018-09-10 06:13:59 +02:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$new_assigned_var_ids = array_diff_key($left_context->assigned_var_ids, $pre_assigned_var_ids);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$new_referenced_var_ids = array_diff_key($new_referenced_var_ids, $new_assigned_var_ids);
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$left_clauses = Algebra::getFormula(
|
2019-12-08 22:35:56 +01:00
|
|
|
\spl_object_id($stmt->left),
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->left,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2019-01-08 15:19:33 +01:00
|
|
|
try {
|
|
|
|
$negated_left_clauses = Algebra::negateFormula($left_clauses);
|
|
|
|
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-07 20:52:45 +02:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($left_context->reconciled_expression_clauses) {
|
|
|
|
$reconciled_expression_clauses = $left_context->reconciled_expression_clauses;
|
|
|
|
|
|
|
|
$negated_left_clauses = array_values(
|
|
|
|
array_filter(
|
|
|
|
$negated_left_clauses,
|
|
|
|
function ($c) use ($reconciled_expression_clauses) {
|
|
|
|
return !\in_array($c->getHash(), $reconciled_expression_clauses);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:52:45 +02:00
|
|
|
$clauses_for_right_analysis = Algebra::simplifyCNF(
|
2018-01-14 18:09:40 +01:00
|
|
|
array_merge(
|
2019-12-08 06:49:34 +01:00
|
|
|
$context->clauses,
|
2018-05-07 20:52:45 +02:00
|
|
|
$negated_left_clauses
|
2018-01-14 18:09:40 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-12-08 22:35:56 +01:00
|
|
|
$active_negated_type_assertions = [];
|
|
|
|
|
|
|
|
$negated_type_assertions = Algebra::getTruthsFromFormula(
|
|
|
|
$clauses_for_right_analysis,
|
|
|
|
\spl_object_id($stmt->left),
|
|
|
|
$new_referenced_var_ids,
|
|
|
|
$active_negated_type_assertions
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$changed_var_ids = [];
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$op_context = clone $context;
|
2018-12-17 21:23:56 +01:00
|
|
|
|
|
|
|
if ($negated_type_assertions) {
|
|
|
|
// while in an or, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x === null || $x->foo())
|
|
|
|
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
|
|
|
|
$negated_type_assertions,
|
2019-12-08 22:35:56 +01:00
|
|
|
$active_negated_type_assertions,
|
2019-12-08 06:49:34 +01:00
|
|
|
$op_context->vars_in_scope,
|
2018-12-17 21:23:56 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
|
|
|
$statements_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2019-12-08 06:49:34 +01:00
|
|
|
$left_context->inside_loop,
|
2018-12-17 21:23:56 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
);
|
|
|
|
$op_context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:52:45 +02:00
|
|
|
$op_context->clauses = $clauses_for_right_analysis;
|
2018-12-17 21:23:56 +01:00
|
|
|
|
2018-09-10 06:13:59 +02:00
|
|
|
if ($changed_var_ids) {
|
2019-12-08 06:49:34 +01:00
|
|
|
$partitioned_clauses = Context::removeReconciledClauses($op_context->clauses, $changed_var_ids);
|
|
|
|
$op_context->clauses = $partitioned_clauses[0];
|
|
|
|
$op_context->reconciled_expression_clauses = array_merge(
|
|
|
|
$context->reconciled_expression_clauses,
|
|
|
|
array_map(
|
|
|
|
function ($c) {
|
|
|
|
return $c->getHash();
|
|
|
|
},
|
|
|
|
$partitioned_clauses[1]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$partitioned_clauses = Context::removeReconciledClauses($context->clauses, $changed_var_ids);
|
|
|
|
$context->clauses = $partitioned_clauses[0];
|
|
|
|
$context->reconciled_expression_clauses = array_merge(
|
|
|
|
$context->reconciled_expression_clauses,
|
|
|
|
array_map(
|
|
|
|
function ($c) {
|
|
|
|
return $c->getHash();
|
|
|
|
},
|
|
|
|
$partitioned_clauses[1]
|
|
|
|
)
|
|
|
|
);
|
2018-09-10 06:13:59 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
$op_context->if_context = null;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $op_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($stmt->right instanceof PhpParser\Node\Expr\Exit_)) {
|
|
|
|
foreach ($op_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
2019-12-08 06:49:34 +01:00
|
|
|
$type,
|
|
|
|
$codebase
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt->left instanceof PhpParser\Node\Expr\Assign) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::getVarId($stmt->left->var, $context->self);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($var_id && isset($left_context->vars_in_scope[$var_id])) {
|
2019-08-10 19:22:21 +02:00
|
|
|
$left_inferred_reconciled = AssertionReconciler::reconcile(
|
2018-01-14 18:09:40 +01:00
|
|
|
'!falsy',
|
2019-12-08 06:49:34 +01:00
|
|
|
clone $left_context->vars_in_scope[$var_id],
|
2018-01-14 18:09:40 +01:00
|
|
|
'',
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-12-08 19:18:55 +01:00
|
|
|
$context->inside_loop,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->left),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-02-06 19:40:28 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $left_inferred_reconciled;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->inside_conditional) {
|
|
|
|
$context->updateChecks($op_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$op_context->referenced_var_ids,
|
|
|
|
$context->referenced_var_ids
|
|
|
|
);
|
|
|
|
|
2018-06-07 21:03:50 +02:00
|
|
|
$context->assigned_var_ids = array_merge(
|
|
|
|
$context->assigned_var_ids,
|
2019-08-15 16:07:31 +02:00
|
|
|
$op_context->assigned_var_ids
|
2018-06-07 21:03:50 +02:00
|
|
|
);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
2019-08-15 15:43:43 +02:00
|
|
|
foreach ($op_context->unreferenced_vars as $var_id => $locations) {
|
|
|
|
if (!isset($context->unreferenced_vars[$var_id])) {
|
|
|
|
$context->unreferenced_vars[$var_id] = $locations;
|
|
|
|
} else {
|
|
|
|
$new_locations = array_diff_key(
|
|
|
|
$locations,
|
|
|
|
$context->unreferenced_vars[$var_id]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($new_locations) {
|
|
|
|
$context->unreferenced_vars[$var_id] += $locations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-08 06:49:34 +01:00
|
|
|
if ($context->if_context) {
|
|
|
|
$if_context = $context->if_context;
|
|
|
|
|
|
|
|
foreach ($op_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($if_context->vars_in_scope[$var_id])) {
|
|
|
|
$if_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$if_context->vars_in_scope[$var_id],
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
} elseif (isset($left_context->vars_in_scope[$var_id])) {
|
|
|
|
$if_context->vars_in_scope[$var_id] = $left_context->vars_in_scope[$var_id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$if_context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$if_context->referenced_var_ids
|
|
|
|
);
|
|
|
|
|
|
|
|
$if_context->assigned_var_ids = array_merge(
|
|
|
|
$context->assigned_var_ids,
|
|
|
|
$if_context->assigned_var_ids
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
$if_context->unreferenced_vars = $context->unreferenced_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
$if_context->updateChecks($context);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$op_context->vars_possibly_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = Type::getString();
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-04 16:37:36 +02:00
|
|
|
|
|
|
|
if ($codebase->taint) {
|
|
|
|
$sources = [];
|
|
|
|
$either_tainted = 0;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_left_type = $statements_analyzer->node_data->getType($stmt->left)) {
|
|
|
|
$sources = $stmt_left_type->sources ?: [];
|
|
|
|
$either_tainted = $stmt_left_type->tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_right_type = $statements_analyzer->node_data->getType($stmt->right)) {
|
|
|
|
$sources = array_merge($sources, $stmt_right_type->sources ?: []);
|
|
|
|
$either_tainted = $either_tainted | $stmt_right_type->tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($sources) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->sources = $sources;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($either_tainted) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->tainted = $either_tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Coalesce) {
|
|
|
|
$t_if_context = clone $context;
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$if_clauses = Algebra::getFormula(
|
2019-12-08 22:35:56 +01:00
|
|
|
\spl_object_id($stmt),
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
2018-08-24 22:48:14 +02:00
|
|
|
$mixed_var_ids = [];
|
2018-05-31 00:56:44 +02:00
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var_id => $type) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($type->hasMixed()) {
|
2018-05-31 00:56:44 +02:00
|
|
|
$mixed_var_ids[] = $var_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_possibly_in_scope as $var_id => $_) {
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$mixed_var_ids[] = $var_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$if_clauses = array_values(
|
|
|
|
array_map(
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @return \Psalm\Internal\Clause
|
2018-05-31 00:56:44 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
|
2018-05-31 00:56:44 +02:00
|
|
|
$keys = array_keys($c->possibilities);
|
|
|
|
|
2019-11-12 16:21:27 +01:00
|
|
|
$mixed_var_ids = \array_diff($mixed_var_ids, $keys);
|
2019-11-12 16:12:07 +01:00
|
|
|
|
2018-05-31 00:56:44 +02:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
foreach ($mixed_var_ids as $mixed_var_id) {
|
|
|
|
if (preg_match('/^' . preg_quote($mixed_var_id, '/') . '(\[|-)/', $key)) {
|
2018-11-06 03:57:36 +01:00
|
|
|
return new \Psalm\Internal\Clause([], true);
|
2018-05-31 00:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
},
|
|
|
|
$if_clauses
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$ternary_clauses = Algebra::simplifyCNF(array_merge($context->clauses, $if_clauses));
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$negated_clauses = Algebra::negateFormula($if_clauses);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$negated_if_types = Algebra::getTruthsFromFormula($negated_clauses);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-05-07 07:26:06 +02:00
|
|
|
$reconcilable_if_types = Algebra::getTruthsFromFormula($ternary_clauses);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
|
|
|
$changed_var_ids = [];
|
|
|
|
|
2018-12-17 21:23:56 +01:00
|
|
|
if ($reconcilable_if_types) {
|
|
|
|
$t_if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
|
|
|
$reconcilable_if_types,
|
2019-12-08 22:35:56 +01:00
|
|
|
[],
|
2018-12-17 21:23:56 +01:00
|
|
|
$t_if_context->vars_in_scope,
|
|
|
|
$changed_var_ids,
|
|
|
|
[],
|
|
|
|
$statements_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-12-17 21:23:56 +01:00
|
|
|
$t_if_context->inside_loop,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->left)
|
|
|
|
);
|
|
|
|
|
|
|
|
$t_if_context->vars_in_scope = $t_if_vars_in_scope_reconciled;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-08-20 04:45:24 +02:00
|
|
|
if (!self::hasArrayDimFetch($stmt->left)) {
|
|
|
|
// check first if the variable was good
|
|
|
|
|
|
|
|
IssueBuffer::startRecording();
|
|
|
|
|
2019-08-20 20:26:32 +02:00
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, clone $context);
|
2019-08-20 04:45:24 +02:00
|
|
|
|
|
|
|
IssueBuffer::clearRecordingLevel();
|
|
|
|
IssueBuffer::stopRecording();
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$naive_type = $statements_analyzer->node_data->getType($stmt->left);
|
2019-08-20 04:45:24 +02:00
|
|
|
|
|
|
|
if ($naive_type
|
2019-10-20 23:06:59 +02:00
|
|
|
&& !$naive_type->hasMixed()
|
2019-08-20 04:45:24 +02:00
|
|
|
&& !$naive_type->isNullable()
|
|
|
|
) {
|
|
|
|
$var_id = ExpressionAnalyzer::getVarId($stmt->left, $context->self);
|
|
|
|
|
2019-12-07 07:23:35 +01:00
|
|
|
if (!$var_id || !isset($changed_var_ids[$var_id])) {
|
2019-08-20 04:45:24 +02:00
|
|
|
if ($naive_type->from_docblock) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\DocblockTypeContradiction(
|
|
|
|
$naive_type->getId() . ' does not contain null',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->left)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\TypeDoesNotContainType(
|
|
|
|
$naive_type->getId() . ' is always defined and non-null',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt->left)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:07:03 +02:00
|
|
|
$t_if_context->inside_isset = true;
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $t_if_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:07:03 +02:00
|
|
|
$t_if_context->inside_isset = false;
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
foreach ($t_if_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
2019-12-08 06:49:34 +01:00
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$type,
|
|
|
|
$codebase
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_if_context->referenced_var_ids
|
|
|
|
);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
|
|
|
$context->unreferenced_vars = array_intersect_key(
|
|
|
|
$t_if_context->unreferenced_vars,
|
|
|
|
$context->unreferenced_vars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$t_else_context = clone $context;
|
|
|
|
|
|
|
|
if ($negated_if_types) {
|
|
|
|
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
|
|
|
$negated_if_types,
|
2019-12-08 22:35:56 +01:00
|
|
|
[],
|
2018-01-14 18:09:40 +01:00
|
|
|
$t_else_context->vars_in_scope,
|
|
|
|
$changed_var_ids,
|
|
|
|
[],
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-12-08 19:18:55 +01:00
|
|
|
$t_else_context->inside_loop,
|
2018-11-14 19:44:20 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->right)
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$t_else_context->vars_in_scope = $t_else_vars_in_scope_reconciled;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $t_else_context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_else_context->referenced_var_ids
|
|
|
|
);
|
|
|
|
|
2018-01-25 07:04:26 +01:00
|
|
|
if ($context->collect_references) {
|
|
|
|
$context->unreferenced_vars = array_intersect_key(
|
|
|
|
$t_else_context->unreferenced_vars,
|
|
|
|
$context->unreferenced_vars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$lhs_type = null;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_left_type = $statements_analyzer->node_data->getType($stmt->left)) {
|
2019-08-10 19:22:21 +02:00
|
|
|
$if_return_type_reconciled = AssertionReconciler::reconcile(
|
2018-01-14 18:09:40 +01:00
|
|
|
'!null',
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_left_type,
|
2018-01-14 18:09:40 +01:00
|
|
|
'',
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-12-08 19:18:55 +01:00
|
|
|
$context->inside_loop,
|
2019-01-23 05:42:54 +01:00
|
|
|
[],
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$lhs_type = $if_return_type_reconciled;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_right_type = null;
|
|
|
|
|
|
|
|
if (!$lhs_type || !($stmt_right_type = $statements_analyzer->node_data->getType($stmt->right))) {
|
|
|
|
$stmt_type = Type::getMixed();
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
} else {
|
2019-12-08 06:49:34 +01:00
|
|
|
$stmt_type = Type::combineUnionTypes(
|
|
|
|
$lhs_type,
|
|
|
|
$stmt_right_type,
|
|
|
|
$codebase
|
|
|
|
);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($stmt->left instanceof PhpParser\Node\Expr\BinaryOp) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->left, $context, ++$nesting) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->right instanceof PhpParser\Node\Expr\BinaryOp) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->right, $context, ++$nesting) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $context) === false) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_left_type = $statements_analyzer->node_data->getType($stmt->left);
|
|
|
|
$stmt_right_type = $statements_analyzer->node_data->getType($stmt->right);
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
// let's do some fun type assignment
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_left_type && $stmt_right_type) {
|
|
|
|
if ($stmt_left_type->hasString()
|
|
|
|
&& $stmt_right_type->hasString()
|
2018-04-17 00:19:18 +02:00
|
|
|
&& ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|
|
|
|
)
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = Type::getString();
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-04-17 00:19:18 +02:00
|
|
|
} 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
|
2019-11-25 17:44:54 +01:00
|
|
|
|| (($stmt_left_type->hasInt() || $stmt_right_type->hasInt())
|
2018-04-17 00:19:18 +02: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
|
|
|
|
)
|
|
|
|
)
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
2019-03-25 02:22:52 +01:00
|
|
|
self::analyzeNonDivArithmeticOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $result_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2018-05-28 21:31:20 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_left_type->hasBool() || $stmt_right_type->hasBool())
|
2018-05-28 21:31:20 +02:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt());
|
2018-05-28 21:31:20 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_left_type->hasBool() || $stmt_right_type->hasBool())
|
2018-05-28 21:31:20 +02:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2018-01-14 18:09:40 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
|
2019-03-25 02:22:52 +01:00
|
|
|
self::analyzeNonDivArithmeticOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2018-05-05 19:14:59 +02:00
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
|
|
|
if ($result_type->hasInt()) {
|
|
|
|
$result_type->addType(new TFloat);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $result_type);
|
2018-05-05 19:14:59 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
|
|
|
self::analyzeConcatOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$context,
|
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = $result_type;
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($codebase->taint && $stmt_type) {
|
|
|
|
$sources = $stmt_left_type->sources ?: [];
|
|
|
|
$either_tainted = $stmt_left_type->tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$sources = array_merge($sources, $stmt_right_type->sources ?: []);
|
|
|
|
$either_tainted = $either_tainted | $stmt_right_type->tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
|
|
|
|
if ($sources) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->sources = $sources;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($either_tainted) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->tainted = $either_tainted;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 16:15:15 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
|
2019-03-25 02:22:52 +01:00
|
|
|
self::analyzeNonDivArithmeticOp(
|
2019-01-13 16:15:15 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2019-01-13 16:15:15 +01:00
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotEqual
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Spaceship) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt());
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-20 04:45:24 +02:00
|
|
|
private static function hasArrayDimFetch(PhpParser\Node\Expr $expr) : bool
|
|
|
|
{
|
|
|
|
if ($expr instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expr instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|| $expr instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
) {
|
|
|
|
return self::hasArrayDimFetch($expr->var);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-25 02:22:52 +01:00
|
|
|
public static function analyzeNonDivArithmeticOp(
|
2019-09-01 23:49:37 +02:00
|
|
|
?StatementsSource $statements_source,
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\Internal\Provider\NodeDataProvider $nodes,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr $left,
|
|
|
|
PhpParser\Node\Expr $right,
|
|
|
|
PhpParser\Node $parent,
|
2019-09-01 23:49:37 +02:00
|
|
|
?Type\Union &$result_type = null,
|
|
|
|
?Context $context = null
|
|
|
|
) : void {
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $statements_source ? $statements_source->getCodebase() : null;
|
2018-02-04 00:52:35 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$left_type = $nodes->getType($left);
|
|
|
|
$right_type = $nodes->getType($right);
|
2018-01-14 18:09:40 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if ($left_type && $right_type) {
|
2018-06-18 19:16:51 +02:00
|
|
|
if ($left_type->isNull()) {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Left operand cannot be null',
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-08 20:04:00 +01:00
|
|
|
if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) {
|
2018-01-21 18:44:46 +01:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
2018-01-14 18:09:40 +01:00
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Left operand cannot be nullable, got ' . $left_type,
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-06-18 19:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($right_type->isNull()) {
|
2018-01-21 18:44:46 +01:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
2018-01-14 18:09:40 +01:00
|
|
|
new NullOperand(
|
2018-06-18 19:16:51 +02:00
|
|
|
'Right operand cannot be null',
|
|
|
|
new CodeLocation($statements_source, $right)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 01:52:40 +02:00
|
|
|
if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) {
|
2018-01-21 18:44:46 +01:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
2018-01-14 18:09:40 +01:00
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Right operand cannot be nullable, got ' . $right_type,
|
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-06-18 19:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type->isFalse()) {
|
2018-01-21 18:44:46 +01:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
2018-06-18 19:16:51 +02:00
|
|
|
new FalseOperand(
|
|
|
|
'Left operand cannot be null',
|
|
|
|
new CodeLocation($statements_source, $left)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($left_type->isFalsable() && !$left_type->ignore_falsable_issues) {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new PossiblyFalseOperand(
|
|
|
|
'Left operand cannot be falsable, got ' . $left_type,
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-06-18 19:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($right_type->isFalse()) {
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new FalseOperand(
|
2018-06-18 19:16:51 +02:00
|
|
|
'Right operand cannot be false',
|
|
|
|
new CodeLocation($statements_source, $right)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($right_type->isFalsable() && !$right_type->ignore_falsable_issues) {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new PossiblyFalseOperand(
|
|
|
|
'Right operand cannot be falsable, got ' . $right_type,
|
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_left_messages = [];
|
|
|
|
$invalid_right_messages = [];
|
|
|
|
$has_valid_left_operand = false;
|
|
|
|
$has_valid_right_operand = false;
|
2019-09-01 23:49:37 +02:00
|
|
|
$has_string_increment = false;
|
2018-03-18 21:39:34 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
foreach ($left_type->getTypes() as $left_type_part) {
|
|
|
|
foreach ($right_type->getTypes() as $right_type_part) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$candidate_result_type = self::analyzeNonDivOperands(
|
|
|
|
$statements_source,
|
|
|
|
$codebase,
|
2018-05-07 07:26:06 +02:00
|
|
|
$config,
|
|
|
|
$context,
|
2018-05-06 18:04:38 +02:00
|
|
|
$left,
|
|
|
|
$right,
|
|
|
|
$parent,
|
|
|
|
$left_type_part,
|
|
|
|
$right_type_part,
|
|
|
|
$invalid_left_messages,
|
|
|
|
$invalid_right_messages,
|
|
|
|
$has_valid_left_operand,
|
|
|
|
$has_valid_right_operand,
|
2019-09-01 23:49:37 +02:00
|
|
|
$has_string_increment,
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type
|
|
|
|
);
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-05-06 18:04:38 +02:00
|
|
|
if ($candidate_result_type) {
|
|
|
|
$result_type = $candidate_result_type;
|
2018-01-14 18:09:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 21:39:34 +01:00
|
|
|
|
|
|
|
if ($invalid_left_messages && $statements_source) {
|
|
|
|
$first_left_message = $invalid_left_messages[0];
|
|
|
|
|
|
|
|
if ($has_valid_left_operand) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidOperand(
|
|
|
|
$first_left_message,
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
$first_left_message,
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_right_messages && $statements_source) {
|
2018-03-18 21:46:26 +01:00
|
|
|
$first_right_message = $invalid_right_messages[0];
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($has_valid_right_operand) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidOperand(
|
2018-03-18 21:46:26 +01:00
|
|
|
$first_right_message,
|
2018-03-18 21:39:34 +01:00
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
2018-03-18 21:46:26 +01:00
|
|
|
$first_right_message,
|
2018-03-18 21:39:34 +01:00
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 23:49:37 +02:00
|
|
|
|
|
|
|
if ($has_string_increment && $statements_source) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new StringIncrement(
|
|
|
|
'Possibly unintended string increment',
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:04:38 +02:00
|
|
|
/**
|
|
|
|
* @param string[] &$invalid_left_messages
|
|
|
|
* @param string[] &$invalid_right_messages
|
|
|
|
*
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2019-09-01 23:49:37 +02:00
|
|
|
private static function analyzeNonDivOperands(
|
|
|
|
?StatementsSource $statements_source,
|
|
|
|
?\Psalm\Codebase $codebase,
|
2018-05-07 07:26:06 +02:00
|
|
|
Config $config,
|
2019-09-01 23:49:37 +02:00
|
|
|
?Context $context,
|
2018-05-06 18:04:38 +02:00
|
|
|
PhpParser\Node\Expr $left,
|
|
|
|
PhpParser\Node\Expr $right,
|
|
|
|
PhpParser\Node $parent,
|
|
|
|
Type\Atomic $left_type_part,
|
|
|
|
Type\Atomic $right_type_part,
|
|
|
|
array &$invalid_left_messages,
|
|
|
|
array &$invalid_right_messages,
|
2019-09-01 23:49:37 +02:00
|
|
|
bool &$has_valid_left_operand,
|
|
|
|
bool &$has_valid_right_operand,
|
|
|
|
bool &$has_string_increment,
|
2018-05-06 18:04:38 +02:00
|
|
|
Type\Union &$result_type = null
|
|
|
|
) {
|
|
|
|
if ($left_type_part instanceof TNull || $right_type_part instanceof TNull) {
|
|
|
|
// null case is handled above
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part instanceof TFalse || $right_type_part instanceof TFalse) {
|
|
|
|
// null case is handled above
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-01 23:49:37 +02:00
|
|
|
if ($left_type_part instanceof Type\Atomic\TString
|
|
|
|
&& $right_type_part instanceof TInt
|
|
|
|
&& $parent instanceof PhpParser\Node\Expr\PostInc
|
|
|
|
) {
|
|
|
|
$has_string_increment = true;
|
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = Type::getString();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getString(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:04:38 +02:00
|
|
|
if ($left_type_part instanceof TMixed
|
|
|
|
|| $right_type_part instanceof TMixed
|
2019-02-22 03:40:06 +01:00
|
|
|
|| $left_type_part instanceof TTemplateParam
|
|
|
|
|| $right_type_part instanceof TTemplateParam
|
2018-05-06 18:04:38 +02:00
|
|
|
) {
|
2019-03-23 14:50:47 +01:00
|
|
|
if ($statements_source && $codebase && $context) {
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_source->getFilePath() === $statements_source->getRootFilePath()
|
|
|
|
&& (!(($source = $statements_source->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_source->getFilePath());
|
|
|
|
}
|
2018-05-06 18:04:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:40:06 +01:00
|
|
|
if ($left_type_part instanceof TMixed || $left_type_part instanceof TTemplateParam) {
|
2018-05-06 18:04:38 +02:00
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Left operand cannot be mixed',
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Right operand cannot be mixed',
|
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part instanceof TMixed
|
2018-12-08 19:18:55 +01:00
|
|
|
&& $left_type_part->from_loop_isset
|
2018-05-06 18:04:38 +02:00
|
|
|
&& $parent instanceof PhpParser\Node\Expr\AssignOp\Plus
|
|
|
|
&& !$right_type_part instanceof TMixed
|
|
|
|
) {
|
|
|
|
$result_type_member = new Type\Union([$right_type_part]);
|
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = $result_type_member;
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes($result_type_member, $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
$from_loop_isset = (!($left_type_part instanceof TMixed) || $left_type_part->from_loop_isset)
|
|
|
|
&& (!($right_type_part instanceof TMixed) || $right_type_part->from_loop_isset);
|
2018-05-06 18:04:38 +02:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
$result_type = Type::getMixed($from_loop_isset);
|
2018-05-06 18:04:38 +02:00
|
|
|
|
|
|
|
return $result_type;
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
if ($statements_source && $codebase && $context) {
|
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_source->getFilePath() === $statements_source->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_source->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_source->getFilePath());
|
|
|
|
}
|
2018-05-06 18:04:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part instanceof TArray
|
|
|
|
|| $right_type_part instanceof TArray
|
|
|
|
|| $left_type_part instanceof ObjectLike
|
|
|
|
|| $right_type_part instanceof ObjectLike
|
2019-10-09 00:44:46 +02:00
|
|
|
|| $left_type_part instanceof TList
|
|
|
|
|| $right_type_part instanceof TList
|
2018-05-06 18:04:38 +02:00
|
|
|
) {
|
2019-10-09 00:44:46 +02:00
|
|
|
if ((!$right_type_part instanceof TArray
|
|
|
|
&& !$right_type_part instanceof ObjectLike
|
|
|
|
&& !$right_type_part instanceof TList)
|
|
|
|
|| (!$left_type_part instanceof TArray
|
|
|
|
&& !$left_type_part instanceof ObjectLike
|
|
|
|
&& !$left_type_part instanceof TList)
|
2018-05-06 18:04:38 +02:00
|
|
|
) {
|
2019-10-09 00:44:46 +02:00
|
|
|
if (!$left_type_part instanceof TArray
|
|
|
|
&& !$left_type_part instanceof ObjectLike
|
|
|
|
&& !$left_type_part instanceof TList
|
|
|
|
) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$invalid_left_messages[] = 'Cannot add an array to a non-array ' . $left_type_part;
|
|
|
|
} else {
|
|
|
|
$invalid_right_messages[] = 'Cannot add an array to a non-array ' . $right_type_part;
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:44:46 +02:00
|
|
|
if ($left_type_part instanceof TArray
|
|
|
|
|| $left_type_part instanceof ObjectLike
|
|
|
|
|| $left_type_part instanceof TList
|
|
|
|
) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$has_valid_left_operand = true;
|
2019-10-09 00:44:46 +02:00
|
|
|
} elseif ($right_type_part instanceof TArray
|
|
|
|
|| $right_type_part instanceof ObjectLike
|
|
|
|
|| $right_type_part instanceof TList
|
|
|
|
) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$has_valid_right_operand = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result_type = Type::getArray();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
2019-09-08 16:23:12 +02:00
|
|
|
if ($left_type_part instanceof ObjectLike
|
|
|
|
&& $right_type_part instanceof ObjectLike
|
|
|
|
) {
|
|
|
|
$definitely_existing_mixed_right_properties = array_diff_key(
|
|
|
|
$right_type_part->properties,
|
|
|
|
$left_type_part->properties
|
|
|
|
);
|
|
|
|
|
2018-05-06 18:04:38 +02:00
|
|
|
$properties = $left_type_part->properties + $right_type_part->properties;
|
|
|
|
|
2019-09-08 16:23:12 +02:00
|
|
|
if (!$left_type_part->sealed) {
|
|
|
|
foreach ($definitely_existing_mixed_right_properties as $key => $type) {
|
|
|
|
$properties[$key] = Type::combineUnionTypes(Type::getMixed(), $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type_member = new Type\Union([new ObjectLike($properties)]);
|
|
|
|
} else {
|
2019-01-05 06:15:53 +01:00
|
|
|
$result_type_member = TypeCombination::combineTypes(
|
|
|
|
[$left_type_part, $right_type_part],
|
|
|
|
$codebase,
|
|
|
|
true
|
|
|
|
);
|
2018-05-06 18:04:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = $result_type_member;
|
|
|
|
} else {
|
2019-01-05 06:15:53 +01:00
|
|
|
$result_type = Type::combineUnionTypes($result_type_member, $result_type, $codebase, true);
|
2018-05-06 18:04:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($left instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
&& $context
|
2018-11-06 03:57:36 +01:00
|
|
|
&& $statements_source instanceof StatementsAnalyzer
|
2018-05-06 18:04:38 +02:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ArrayAssignmentAnalyzer::updateArrayType(
|
2018-05-06 18:04:38 +02:00
|
|
|
$statements_source,
|
|
|
|
$left,
|
2019-01-31 18:45:47 +01:00
|
|
|
$right,
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($left_type_part instanceof TNamedObject && strtolower($left_type_part->value) === 'gmp')
|
|
|
|
|| ($right_type_part instanceof TNamedObject && strtolower($right_type_part->value) === 'gmp')
|
|
|
|
) {
|
|
|
|
if ((($left_type_part instanceof TNamedObject
|
|
|
|
&& strtolower($left_type_part->value) === 'gmp')
|
|
|
|
&& (($right_type_part instanceof TNamedObject
|
|
|
|
&& strtolower($right_type_part->value) === 'gmp')
|
|
|
|
|| ($right_type_part->isNumericType() || $right_type_part instanceof TMixed)))
|
|
|
|
|| (($right_type_part instanceof TNamedObject
|
|
|
|
&& strtolower($right_type_part->value) === 'gmp')
|
|
|
|
&& (($left_type_part instanceof TNamedObject
|
|
|
|
&& strtolower($left_type_part->value) === 'gmp')
|
|
|
|
|| ($left_type_part->isNumericType() || $left_type_part instanceof TMixed)))
|
|
|
|
) {
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = new Type\Union([new TNamedObject('GMP')]);
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(
|
|
|
|
new Type\Union([new TNamedObject('GMP')]),
|
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add GMP to non-numeric type',
|
|
|
|
new CodeLocation($statements_source, $parent)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part->isNumericType() || $right_type_part->isNumericType()) {
|
|
|
|
if (($left_type_part instanceof TNumeric || $right_type_part instanceof TNumeric)
|
|
|
|
&& ($left_type_part->isNumericType() && $right_type_part->isNumericType())
|
|
|
|
) {
|
2018-11-07 14:45:26 +01:00
|
|
|
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} elseif (!$result_type) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type = Type::getNumeric();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getNumeric(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part instanceof TInt && $right_type_part instanceof TInt) {
|
2018-11-07 14:45:26 +01:00
|
|
|
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} elseif (!$result_type) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type = Type::getInt(true);
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getInt(true), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part instanceof TFloat && $right_type_part instanceof TFloat) {
|
2018-11-07 14:45:26 +01:00
|
|
|
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} elseif (!$result_type) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($left_type_part instanceof TFloat && $right_type_part instanceof TInt)
|
|
|
|
|| ($left_type_part instanceof TInt && $right_type_part instanceof TFloat)
|
|
|
|
) {
|
|
|
|
if ($config->strict_binary_operands) {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add ints to floats',
|
|
|
|
new CodeLocation($statements_source, $parent)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:45:26 +01:00
|
|
|
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} elseif (!$result_type) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part->isNumericType() && $right_type_part->isNumericType()) {
|
|
|
|
if ($config->strict_binary_operands) {
|
|
|
|
if ($statements_source && IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add numeric types together, please cast explicitly',
|
|
|
|
new CodeLocation($statements_source, $parent)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:45:26 +01:00
|
|
|
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} elseif (!$result_type) {
|
2018-05-06 18:04:38 +02:00
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$left_type_part->isNumericType()) {
|
|
|
|
$invalid_left_messages[] = 'Cannot perform a numeric operation with a non-numeric type '
|
|
|
|
. $left_type_part;
|
|
|
|
$has_valid_right_operand = true;
|
|
|
|
} else {
|
|
|
|
$invalid_right_messages[] = 'Cannot perform a numeric operation with a non-numeric type '
|
|
|
|
. $right_type_part;
|
|
|
|
$has_valid_left_operand = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$invalid_left_messages[] =
|
|
|
|
'Cannot perform a numeric operation with non-numeric types ' . $left_type_part
|
|
|
|
. ' and ' . $right_type_part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr $left
|
|
|
|
* @param PhpParser\Node\Expr $right
|
|
|
|
* @param Type\Union|null &$result_type
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function analyzeConcatOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Expr $left,
|
|
|
|
PhpParser\Node\Expr $right,
|
|
|
|
Context $context,
|
|
|
|
Type\Union &$result_type = null
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$left_type = $statements_analyzer->node_data->getType($left);
|
|
|
|
$right_type = $statements_analyzer->node_data->getType($right);
|
2018-01-14 18:09:40 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if ($left_type && $right_type) {
|
|
|
|
$result_type = Type::getString();
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($left_type->hasMixed() || $right_type->hasMixed()) {
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($left_type->hasMixed()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Left operand cannot be mixed',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Right operand cannot be mixed',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($left_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Cannot concatenate with a ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($right_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Cannot concatenate with a ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($left_type->isFalse()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new FalseOperand(
|
|
|
|
'Cannot concatenate with a ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-03-18 21:39:34 +01:00
|
|
|
|
|
|
|
return;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($right_type->isFalse()) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new FalseOperand(
|
|
|
|
'Cannot concatenate with a ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) {
|
2018-03-18 19:42:23 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Cannot concatenate with a possibly null ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-03-18 19:42:23 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 19:42:23 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) {
|
2018-03-18 19:42:23 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Cannot concatenate with a possibly null ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-03-18 19:42:23 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 19:42:23 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($left_type->isFalsable() && !$left_type->ignore_falsable_issues) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new PossiblyFalseOperand(
|
|
|
|
'Cannot concatenate with a possibly false ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($right_type->isFalsable() && !$right_type->ignore_falsable_issues) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-03-18 21:39:34 +01:00
|
|
|
new PossiblyFalseOperand(
|
|
|
|
'Cannot concatenate with a possibly false ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-01-14 18:09:40 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-14 18:09:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 21:39:34 +01:00
|
|
|
|
|
|
|
$left_type_match = true;
|
|
|
|
$right_type_match = true;
|
|
|
|
|
|
|
|
$has_valid_left_operand = false;
|
|
|
|
$has_valid_right_operand = false;
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
$left_comparison_result = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
$right_comparison_result = new \Psalm\Internal\Analyzer\TypeComparisonResult();
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
foreach ($left_type->getTypes() as $left_type_part) {
|
2019-02-22 03:40:06 +01:00
|
|
|
if ($left_type_part instanceof Type\Atomic\TTemplateParam) {
|
2018-04-20 16:52:23 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Left operand cannot be mixed',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-04-20 16:52:23 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-20 16:52:23 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($left_type_part instanceof Type\Atomic\TNull || $left_type_part instanceof Type\Atomic\TFalse) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$left_type_part_match = TypeAnalyzer::isAtomicContainedBy(
|
|
|
|
$codebase,
|
2018-03-18 21:39:34 +01:00
|
|
|
$left_type_part,
|
|
|
|
new Type\Atomic\TString,
|
2018-09-09 18:20:49 +02:00
|
|
|
false,
|
2018-11-16 17:04:45 +01:00
|
|
|
false,
|
2019-07-10 07:35:57 +02:00
|
|
|
$left_comparison_result
|
2018-03-18 21:39:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$left_type_match = $left_type_match && $left_type_part_match;
|
|
|
|
|
|
|
|
$has_valid_left_operand = $has_valid_left_operand || $left_type_part_match;
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($left_comparison_result->to_string_cast && $config->strict_binary_operands) {
|
2018-03-18 21:39:34 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplicitToStringCast(
|
|
|
|
'Left side of concat op expects string, '
|
|
|
|
. '\'' . $left_type . '\' provided with a __toString method',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($right_type->getTypes() as $right_type_part) {
|
2019-02-22 03:40:06 +01:00
|
|
|
if ($right_type_part instanceof Type\Atomic\TTemplateParam) {
|
2018-04-20 16:52:23 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Right operand cannot be a template param',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-04-20 16:52:23 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-20 16:52:23 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($right_type_part instanceof Type\Atomic\TNull || $right_type_part instanceof Type\Atomic\TFalse) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$right_type_part_match = TypeAnalyzer::isAtomicContainedBy(
|
|
|
|
$codebase,
|
2018-03-18 21:39:34 +01:00
|
|
|
$right_type_part,
|
|
|
|
new Type\Atomic\TString,
|
2018-09-09 18:20:49 +02:00
|
|
|
false,
|
2018-11-16 17:04:45 +01:00
|
|
|
false,
|
2019-07-10 07:35:57 +02:00
|
|
|
$right_comparison_result
|
2018-03-18 21:39:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$right_type_match = $right_type_match && $right_type_part_match;
|
|
|
|
|
|
|
|
$has_valid_right_operand = $has_valid_right_operand || $right_type_part_match;
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($right_comparison_result->to_string_cast && $config->strict_binary_operands) {
|
2018-03-18 21:39:34 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplicitToStringCast(
|
|
|
|
'Right side of concat op expects string, '
|
|
|
|
. '\'' . $right_type . '\' provided with a __toString method',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if (!$left_type_match
|
|
|
|
&& (!$left_comparison_result->scalar_type_match_found || $config->strict_binary_operands)
|
|
|
|
) {
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($has_valid_left_operand) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidOperand(
|
|
|
|
'Cannot concatenate with a ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot concatenate with a ' . $left_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $left)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
if (!$right_type_match
|
|
|
|
&& (!$right_comparison_result->scalar_type_match_found || $config->strict_binary_operands)
|
|
|
|
) {
|
2018-03-18 21:39:34 +01:00
|
|
|
if ($has_valid_right_operand) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidOperand(
|
|
|
|
'Cannot concatenate with a ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot concatenate with a ' . $right_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $right)
|
2018-03-18 21:39:34 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-03-18 21:39:34 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2018-05-09 01:11:18 +02:00
|
|
|
// When concatenating two known string literals (with only one possibility),
|
|
|
|
// put the concatenated string into $result_type
|
|
|
|
if ($left_type && $right_type && $left_type->isSingleStringLiteral() && $right_type->isSingleStringLiteral()) {
|
2018-08-09 03:31:13 +02:00
|
|
|
$literal = $left_type->getSingleStringLiteral()->value . $right_type->getSingleStringLiteral()->value;
|
2019-03-12 17:58:04 +01:00
|
|
|
if (strlen($literal) <= 1000) {
|
2018-05-09 01:11:18 +02:00
|
|
|
// Limit these to 10000 bytes to avoid extremely large union types from repeated concatenations, etc
|
2019-03-12 17:58:04 +01:00
|
|
|
$result_type = Type::getString($literal);
|
2018-05-09 01:11:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|