1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Track variable usage in bool to int casts (#5349)

Fixes vimeo/psalm#4956
This commit is contained in:
Bruce Weirdan 2021-03-11 07:08:32 +02:00 committed by GitHub
parent 185827a7ab
commit 8be77aaa2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -71,10 +71,18 @@ class CastAnalyzer
if (count($maybe) === 1 && current($maybe) instanceof Type\Atomic\TBool) {
$as_int = false;
$statements_analyzer->node_data->setType($stmt, new Type\Union([
$type = new Type\Union([
new Type\Atomic\TLiteralInt(0),
new Type\Atomic\TLiteralInt(1),
]));
]);
if ($statements_analyzer->data_flow_graph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) {
$type->parent_nodes = $maybe_type->parent_nodes;
}
$statements_analyzer->node_data->setType($stmt, $type);
}
}

View File

@ -2316,6 +2316,18 @@ class UnusedVariableTest extends TestCase
};
}
',
],
'usedInIntCastInAssignment' => [
'<?php
/** @return mixed */
function f() {
$a = random_int(0, 10) >= 5 ? true : false;
$b = (int) $a;
return $b;
}
'
]
];
}