Skip to content
Permalink
Browse files

Fix #2330 - allow empty checks against numeric

  • Loading branch information
muglug committed Nov 14, 2019
1 parent 09aafa4 commit 8777c952c831e82bccf1b0cfcb704e865f08c70d
Showing with 61 additions and 0 deletions.
  1. +36 −0 src/Psalm/Internal/Type/AssertionReconciler.php
  2. +13 −0 src/Psalm/Type/Atomic/TEmptyNumeric.php
  3. +12 −0 tests/EmptyTest.php
@@ -2001,6 +2001,42 @@ private static function reconcileFalsyOrEmpty(
}
}
if ($existing_var_type->hasNumeric()) {
$existing_int_types = $existing_var_type->getLiteralInts();
if ($existing_int_types) {
foreach ($existing_int_types as $int_key => $literal_type) {
if ($literal_type->value) {
$existing_var_type->removeType($int_key);
}
}
}
$existing_string_types = $existing_var_type->getLiteralStrings();
if ($existing_string_types) {
foreach ($existing_string_types as $string_key => $literal_type) {
if ($literal_type->value) {
$existing_var_type->removeType($string_key);
}
}
}
$existing_float_types = $existing_var_type->getLiteralFloats();
if ($existing_float_types) {
foreach ($existing_float_types as $float_key => $literal_type) {
if ($literal_type->value) {
$existing_var_type->removeType($float_key);
}
}
}
$did_remove_type = true;
$existing_var_type->removeType('numeric');
$existing_var_type->addType(new Type\Atomic\TEmptyNumeric);
}
if (isset($existing_var_atomic_types['array'])) {
$array_atomic_type = $existing_var_atomic_types['array'];
@@ -0,0 +1,13 @@
<?php
namespace Psalm\Type\Atomic;
class TEmptyNumeric extends TNumeric
{
/**
* @return string
*/
public function getId()
{
return 'empty-numeric';
}
}
@@ -353,6 +353,18 @@ function foo(?A $a) : void {
if (!empty($a->b)) {}
}',
],
'allowNumericEmpty' => [
'<?php
/**
* @param numeric $p
*/
function f($p): bool {
if (empty($p)) {
return false;
}
return true;
}'
],
];
}

0 comments on commit 8777c95

Please sign in to comment.
You can’t perform that action at this time.