Skip to content

Commit

Permalink
Updated cast analyzer to handle single string literal int values as l…
Browse files Browse the repository at this point in the history
…iteral ints
  • Loading branch information
ricardoboss committed Dec 5, 2021
1 parent f0b0a99 commit 93dfc60
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php
Expand Up @@ -49,7 +49,6 @@ public static function analyze(
return false;
}

$as_int = true;
$valid_int_type = null;
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);

Expand All @@ -59,37 +58,33 @@ public static function analyze(
if (!$maybe_type->from_calculation) {
self::handleRedundantCast($maybe_type, $statements_analyzer, $stmt);
}

if ($maybe_type->isSingleStringLiteral()) {
$valid_int_type = new Type\Union([
new Type\Atomic\TLiteralInt((int)$maybe_type->getSingleStringLiteral()->value),
]);
}
}

$maybe = $maybe_type->getAtomicTypes();

if (count($maybe) === 1 && current($maybe) instanceof Type\Atomic\TBool) {
$as_int = false;
$type = new Type\Union([
$valid_int_type = new Type\Union([
new Type\Atomic\TLiteralInt(0),
new Type\Atomic\TLiteralInt(1),
]);

if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph
) {
$type->parent_nodes = $maybe_type->parent_nodes;
}

$statements_analyzer->node_data->setType($stmt, $type);
}
}

if ($as_int) {
$type = $valid_int_type ?? Type::getInt();
$type = $valid_int_type ?? Type::getInt();

if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph
) {
$type->parent_nodes = $maybe_type->parent_nodes ?? [];
}

$statements_analyzer->node_data->setType($stmt, $type);
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph
) {
$type->parent_nodes = $maybe_type->parent_nodes ?? [];
}

$statements_analyzer->node_data->setType($stmt, $type);

return true;
}

Expand Down

0 comments on commit 93dfc60

Please sign in to comment.