1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix is_bool checks on true vars

This commit is contained in:
Matthew Brown 2017-12-09 16:05:31 -05:00
parent 42dcd136ca
commit a345ec49a1
2 changed files with 81 additions and 0 deletions

View File

@ -278,6 +278,41 @@ class TypeChecker
return Type::getMixed(); return Type::getMixed();
} }
if ($new_var_type === '!bool' && !$existing_var_type->isMixed()) {
$non_bool_types = [];
$did_remove_type = false;
foreach ($existing_var_type->types as $type) {
if (!$type instanceof TBool) {
$non_bool_types[] = $type;
} else {
$did_remove_type = true;
}
}
if ((!$did_remove_type || !$non_bool_types) && !$existing_var_type->from_docblock) {
if ($key && $code_location) {
if (IssueBuffer::accepts(
new RedundantCondition(
'Found a redundant condition when evaluating ' . $key,
$code_location
),
$suppressed_issues
)) {
// fall through
}
}
}
if ($non_bool_types) {
return new Type\Union($non_bool_types);
}
$failed_reconciliation = true;
return Type::getMixed();
}
if ($new_var_type === '!numeric' && !$existing_var_type->isMixed()) { if ($new_var_type === '!numeric' && !$existing_var_type->isMixed()) {
$non_numeric_types = []; $non_numeric_types = [];
$did_remove_type = $existing_var_type->hasString(); $did_remove_type = $existing_var_type->hasString();
@ -648,6 +683,41 @@ class TypeChecker
return Type::getMixed(); return Type::getMixed();
} }
if ($new_var_type === 'bool' && !$existing_var_type->isMixed()) {
$bool_types = [];
$did_remove_type = false;
foreach ($existing_var_type->types as $type) {
if ($type instanceof TBool) {
$bool_types[] = $type;
} else {
$did_remove_type = true;
}
}
if ((!$did_remove_type || !$bool_types) && !$existing_var_type->from_docblock) {
if ($key && $code_location) {
if (IssueBuffer::accepts(
new RedundantCondition(
'Found a redundant condition when evaluating ' . $key,
$code_location
),
$suppressed_issues
)) {
// fall through
}
}
}
if ($bool_types) {
return new Type\Union($bool_types);
}
$failed_reconciliation = true;
return Type::getMixed();
}
$new_type = Type::parseString($new_var_type); $new_type = Type::parseString($new_var_type);
if ($existing_var_type->isMixed()) { if ($existing_var_type->isMixed()) {

View File

@ -425,6 +425,17 @@ class TypeReconciliationTest extends TestCase
'$a' => 'string', '$a' => 'string',
], ],
], ],
'typeRefinementWithStringOrTrue' => [
'<?php
$a = rand(0, 5) > 4 ? "hello" : true;
if (is_bool($a)) {
exit;
}',
'assertions' => [
'$a' => 'string',
],
],
'updateMultipleIssetVars' => [ 'updateMultipleIssetVars' => [
'<?php '<?php
/** @return void **/ /** @return void **/