mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Improve bool to int casting.
This commit is contained in:
parent
2e2099ebe7
commit
525a86e19d
@ -45,7 +45,6 @@ use Psalm\Type\Union;
|
|||||||
use function array_merge;
|
use function array_merge;
|
||||||
use function array_pop;
|
use function array_pop;
|
||||||
use function array_values;
|
use function array_values;
|
||||||
use function count;
|
|
||||||
use function get_class;
|
use function get_class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,12 +76,26 @@ class CastAnalyzer
|
|||||||
$valid_int_type = Type::getInt(false, (int)$maybe_type->getSingleStringLiteral()->value);
|
$valid_int_type = Type::getInt(false, (int)$maybe_type->getSingleStringLiteral()->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($maybe_type->getAtomicTypes()) === 1
|
if ($maybe_type->hasBool()) {
|
||||||
&& $maybe_type->getSingleAtomic() instanceof Type\Atomic\TBool) {
|
$casted_type = clone $maybe_type;
|
||||||
$valid_int_type = new Union([
|
if (isset($casted_type->getAtomicTypes()['bool'])) {
|
||||||
new TLiteralInt(0),
|
$casted_type->addType(new TLiteralInt(0));
|
||||||
new TLiteralInt(1),
|
$casted_type->addType(new TLiteralInt(1));
|
||||||
]);
|
} else {
|
||||||
|
if (isset($casted_type->getAtomicTypes()['true'])) {
|
||||||
|
$casted_type->addType(new TLiteralInt(1));
|
||||||
|
}
|
||||||
|
if (isset($casted_type->getAtomicTypes()['false'])) {
|
||||||
|
$casted_type->addType(new TLiteralInt(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$casted_type->removeType('bool');
|
||||||
|
$casted_type->removeType('true');
|
||||||
|
$casted_type->removeType('false');
|
||||||
|
|
||||||
|
if ($casted_type->isInt()) {
|
||||||
|
$valid_int_type = $casted_type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
|
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
|
||||||
|
47
tests/CastTest.php
Normal file
47
tests/CastTest.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Psalm\Tests;
|
||||||
|
|
||||||
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
||||||
|
|
||||||
|
class CastTest extends TestCase
|
||||||
|
{
|
||||||
|
use ValidCodeAnalysisTestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>,php_version?:string}>
|
||||||
|
*/
|
||||||
|
public function providerValidCodeParse(): iterable
|
||||||
|
{
|
||||||
|
yield 'castFalseOrIntToInt' => [
|
||||||
|
'code' => '<?php
|
||||||
|
/** @var false|int<10, 20> */
|
||||||
|
$intOrFalse = 10;
|
||||||
|
$int = (int) $intOrFalse;
|
||||||
|
',
|
||||||
|
'assertions' => [
|
||||||
|
'$int===' => '0|int<10, 20>',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
yield 'castTrueOrIntToInt' => [
|
||||||
|
'code' => '<?php
|
||||||
|
/** @var true|int<10, 20> */
|
||||||
|
$intOrTrue = 10;
|
||||||
|
$int = (int) $intOrTrue;
|
||||||
|
',
|
||||||
|
'assertions' => [
|
||||||
|
'$int===' => '1|int<10, 20>',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
yield 'castBoolOrIntToInt' => [
|
||||||
|
'code' => '<?php
|
||||||
|
/** @var bool|int<10, 20> */
|
||||||
|
$intOrBool = 10;
|
||||||
|
$int = (int) $intOrBool;
|
||||||
|
',
|
||||||
|
'assertions' => [
|
||||||
|
'$int===' => '0|1|int<10, 20>',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user