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

Improve coverage of Reconciler and fix issues with float type comparison

This commit is contained in:
Matthew Brown 2018-06-10 10:48:19 -04:00
parent 82715a03ea
commit 3a2fa99969
3 changed files with 73 additions and 1 deletions

View File

@ -1399,7 +1399,7 @@ class Reconciler
} elseif ($scalar_type === 'float') {
$value = (float) $value;
if ($existing_var_type->hasInt()) {
if ($existing_var_type->hasFloat()) {
$existing_float_types = $existing_var_type->getLiteralFloats();
if ($existing_float_types) {

View File

@ -880,6 +880,28 @@ class TypeReconciliationTest extends TestCase
if ($a) {}',
],
'removeStringWithIsScalar' => [
'<?php
$a = rand(0, 1) ? "hello" : null;
if (is_scalar($a)) {
exit;
}',
'assertions' => [
'$a' => 'null',
],
],
'removeNullWithIsScalar' => [
'<?php
$a = rand(0, 1) ? "hello" : null;
if (!is_scalar($a)) {
exit;
}',
'assertions' => [
'$a' => 'string',
],
],
];
}
@ -1030,6 +1052,24 @@ class TypeReconciliationTest extends TestCase
if ($a) {}',
'error_message' => 'RedundantCondition',
],
'allRemovalOfStringWithIsScalar' => [
'<?php
$a = rand(0, 1) ? "hello" : "goodbye";
if (is_scalar($a)) {
exit;
}',
'error_message' => 'RedundantCondition',
],
'noRemovalOfStringWithIsScalar' => [
'<?php
$a = rand(0, 1) ? "hello" : "goodbye";
if (!is_scalar($a)) {
exit;
}',
'error_message' => 'TypeDoesNotContainType',
],
];
}
}

View File

@ -410,6 +410,38 @@ class ValueTest extends TestCase
}',
'error_message' => 'TypeDoesNotContainType',
],
'neverEqualsFloatType' => [
'<?php
$a = 4.0;
if ($a === 4.1) {
// do something
}',
'error_message' => 'TypeDoesNotContainType',
],
'alwaysIdenticalFloatType' => [
'<?php
$a = 4.1;
if ($a === 4.1) {
// do something
}',
'error_message' => 'RedundantCondition',
],
'alwaysNotIdenticalFloatType' => [
'<?php
$a = 4.0;
if ($a !== 4.1) {
// do something
}',
'error_message' => 'RedundantCondition',
],
'neverNotIdenticalFloatType' => [
'<?php
$a = 4.1;
if ($a !== 4.1) {
// do something
}',
'error_message' => 'TypeDoesNotContainType',
],
'SKIPPED-phpstanPostedArrayTest' => [
'<?php
$array = [1, 2, 3];