1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Better assertions about non-empty known numbers

This commit is contained in:
Matthew Brown 2018-11-10 17:56:22 -05:00
parent c136974f4d
commit 2ea58e2c0f
3 changed files with 38 additions and 4 deletions

View File

@ -379,7 +379,7 @@ class Reconciler
return new Type\Union([new Type\Atomic\TEmptyMixed]);
}
$did_remove_type = $existing_var_type->hasDefinitelyNumericType();
$did_remove_type = $existing_var_type->hasDefinitelyNumericType(false);
if ($existing_var_type->hasType('bool')) {
$did_remove_type = true;
@ -1188,7 +1188,7 @@ class Reconciler
return Type::getMixed();
}
$did_remove_type = $existing_var_type->hasDefinitelyNumericType()
$did_remove_type = $existing_var_type->hasDefinitelyNumericType(false)
|| $existing_var_type->isEmpty()
|| $existing_var_type->hasType('bool')
|| $existing_var_type->possibly_undefined

View File

@ -531,12 +531,12 @@ class Union
/**
* @return bool
*/
public function hasDefinitelyNumericType()
public function hasDefinitelyNumericType(bool $include_literal_int = true)
{
return isset($this->types['int'])
|| isset($this->types['float'])
|| isset($this->types['numeric-string'])
|| $this->literal_int_types
|| ($include_literal_int && $this->literal_int_types)
|| $this->literal_float_types;
}

View File

@ -749,6 +749,22 @@ class ForeachTest extends \Psalm\Tests\TestCase
}
}'
],
'ifSpecificMaybeEmptyValues' => [
'<?php
foreach ([0, 1, 2, 3] as $i) {
$a = $i;
}
if ($a) {}',
],
'ifSpecificMaybeEmptyStringValues' => [
'<?php
foreach (["", "1", "2", "3"] as $i) {
$a = $i;
}
if ($a) {}',
],
];
}
@ -935,6 +951,24 @@ class ForeachTest extends \Psalm\Tests\TestCase
}',
'error_message' => 'RawObjectIteration',
],
'ifSpecificNonEmptyValues' => [
'<?php
foreach ([1, 2, 3] as $i) {
$a = $i;
}
if ($a) {}',
'error_message' => 'RedundantCondition',
],
'ifSpecificNonEmptyStringValues' => [
'<?php
foreach (["1", "2", "3"] as $i) {
$a = $i;
}
if ($a) {}',
'error_message' => 'RedundantCondition',
],
];
}
}