mirror of
https://github.com/danog/psalm.git
synced 2025-01-10 15:09:04 +01:00
Daniil Gentili
d0be59e16e
* Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Update
97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
|
|
|
|
use PhpParser;
|
|
use Psalm\Context;
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
use Psalm\Node\Expr\VirtualIsset;
|
|
use Psalm\Node\Expr\VirtualTernary;
|
|
use Psalm\Node\Expr\VirtualVariable;
|
|
use Psalm\Type;
|
|
use Psalm\Type\Atomic\TMixed;
|
|
use Psalm\Type\Union;
|
|
|
|
use function substr;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
class CoalesceAnalyzer
|
|
{
|
|
public static function analyze(
|
|
StatementsAnalyzer $statements_analyzer,
|
|
PhpParser\Node\Expr\BinaryOp\Coalesce $stmt,
|
|
Context $context
|
|
): bool {
|
|
$left_expr = $stmt->left;
|
|
|
|
$root_expr = $left_expr;
|
|
|
|
while ($root_expr instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|| $root_expr instanceof PhpParser\Node\Expr\PropertyFetch
|
|
) {
|
|
$root_expr = $root_expr->var;
|
|
}
|
|
|
|
if ($root_expr instanceof PhpParser\Node\Expr\FuncCall
|
|
|| $root_expr instanceof PhpParser\Node\Expr\MethodCall
|
|
|| $root_expr instanceof PhpParser\Node\Expr\StaticCall
|
|
|| $root_expr instanceof PhpParser\Node\Expr\Cast
|
|
|| $root_expr instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|
|
|| $root_expr instanceof PhpParser\Node\Expr\NullsafeMethodCall
|
|
|| $root_expr instanceof PhpParser\Node\Expr\Ternary
|
|
) {
|
|
$left_var_id = '$<tmp coalesce var>' . (int) $left_expr->getAttribute('startFilePos');
|
|
|
|
$cloned = clone $context;
|
|
$cloned->inside_isset = true;
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $left_expr, $cloned);
|
|
|
|
if ($root_expr !== $left_expr) {
|
|
$condition_type = $statements_analyzer->node_data->getType($left_expr);
|
|
if ($condition_type) {
|
|
$condition_type = $condition_type->setPossiblyUndefined(true);
|
|
} else {
|
|
$condition_type = new Union([new TMixed()], ['possibly_undefined' => true]);
|
|
}
|
|
} else {
|
|
$condition_type = $statements_analyzer->node_data->getType($left_expr) ?? Type::getMixed();
|
|
}
|
|
|
|
$context->vars_in_scope[$left_var_id] = $condition_type;
|
|
|
|
$left_expr = new VirtualVariable(
|
|
substr($left_var_id, 1),
|
|
$left_expr->getAttributes()
|
|
);
|
|
}
|
|
|
|
$ternary = new VirtualTernary(
|
|
new VirtualIsset(
|
|
[$left_expr],
|
|
$stmt->left->getAttributes()
|
|
),
|
|
$left_expr,
|
|
$stmt->right,
|
|
$stmt->getAttributes()
|
|
);
|
|
|
|
$old_node_data = $statements_analyzer->node_data;
|
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $ternary, $context);
|
|
|
|
$ternary_type = $statements_analyzer->node_data->getType($ternary) ?? Type::getMixed();
|
|
|
|
$statements_analyzer->node_data = $old_node_data;
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $ternary_type);
|
|
|
|
return true;
|
|
}
|
|
}
|