1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php

214 lines
6.9 KiB
PHP
Raw Normal View History

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\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2018-01-14 18:09:40 +01:00
use Psalm\CodeLocation;
use Psalm\Context;
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\Reconciler;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class TernaryAnalyzer
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\Ternary $stmt
* @param Context $context
*
* @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\Ternary $stmt,
Context $context
) {
$pre_referenced_var_ids = $context->referenced_var_ids;
$context->referenced_var_ids = [];
$context->inside_conditional = true;
2018-11-11 18:01:14 +01:00
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->cond, $context) === false) {
2018-01-14 18:09:40 +01:00
return false;
}
$new_referenced_var_ids = $context->referenced_var_ids;
$context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
$context->inside_conditional = false;
2018-11-11 18:01:14 +01:00
$codebase = $statements_analyzer->getCodebase();
2018-11-06 03:57:36 +01:00
2018-01-14 18:09:40 +01:00
$t_if_context = clone $context;
2018-05-07 07:26:06 +02:00
$if_clauses = \Psalm\Type\Algebra::getFormula(
2018-01-14 18:09:40 +01:00
$stmt->cond,
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
);
$mixed_var_ids = [];
2018-05-31 02:54:03 +02:00
foreach ($context->vars_in_scope as $var_id => $type) {
if ($type->isMixed()) {
$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 02:54:03 +02:00
*/
2018-11-06 03:57:36 +01:00
function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
2018-05-31 02:54:03 +02:00
$keys = array_keys($c->possibilities);
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 02:54:03 +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(
Algebra::simplifyCNF(
2018-05-06 02:52:10 +02:00
array_merge($context->clauses, $negated_clauses)
)
);
2018-01-14 18:09:40 +01:00
2018-05-07 23:52:18 +02:00
$reconcilable_if_types = Algebra::getTruthsFromFormula($ternary_clauses, $new_referenced_var_ids);
2018-01-14 18:09:40 +01:00
$changed_var_ids = [];
$t_if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$reconcilable_if_types,
$t_if_context->vars_in_scope,
$changed_var_ids,
$new_referenced_var_ids,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
new CodeLocation($statements_analyzer->getSource(), $stmt->cond)
2018-01-14 18:09:40 +01:00
);
$t_if_context->vars_in_scope = $t_if_vars_in_scope_reconciled;
$t_else_context = clone $context;
if ($stmt->if) {
2018-11-11 18:01:14 +01:00
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->if, $t_if_context) === false) {
2018-01-14 18:09:40 +01:00
return false;
}
foreach ($t_if_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], $type);
}
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_if_context->referenced_var_ids
);
$context->unreferenced_vars = array_intersect_key(
$context->unreferenced_vars,
$t_if_context->unreferenced_vars
);
2018-01-14 18:09:40 +01:00
}
if ($negated_if_types) {
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
$negated_if_types,
$t_else_context->vars_in_scope,
$changed_var_ids,
$new_referenced_var_ids,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
new CodeLocation($statements_analyzer->getSource(), $stmt->else)
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->else, $t_else_context) === false) {
2018-01-14 18:09:40 +01:00
return false;
}
foreach ($t_else_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],
$type
);
} elseif (isset($t_if_context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
$t_if_context->vars_in_scope[$var_id],
$type
);
2018-01-14 18:09:40 +01:00
}
}
$context->vars_possibly_in_scope = array_merge(
$context->vars_possibly_in_scope,
$t_if_context->vars_possibly_in_scope,
$t_else_context->vars_possibly_in_scope
);
2018-01-14 18:09:40 +01:00
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_else_context->referenced_var_ids
);
$context->unreferenced_vars = array_intersect_key(
$context->unreferenced_vars,
$t_else_context->unreferenced_vars
);
2018-01-14 18:09:40 +01:00
$lhs_type = null;
if ($stmt->if) {
if (isset($stmt->if->inferredType)) {
$lhs_type = $stmt->if->inferredType;
}
} elseif (isset($stmt->cond->inferredType)) {
$if_return_type_reconciled = Reconciler::reconcileTypes(
'!falsy',
$stmt->cond->inferredType,
'',
2018-11-11 18:01:14 +01:00
$statements_analyzer,
new CodeLocation($statements_analyzer->getSource(), $stmt),
$statements_analyzer->getSuppressedIssues()
2018-01-14 18:09:40 +01:00
);
$lhs_type = $if_return_type_reconciled;
}
if (!$lhs_type || !isset($stmt->else->inferredType)) {
$stmt->inferredType = Type::getMixed();
} else {
$stmt->inferredType = Type::combineUnionTypes($lhs_type, $stmt->else->inferredType);
}
return null;
}
}