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

Be more explicit about negation

This commit is contained in:
Matt Brown 2020-10-18 00:39:43 -04:00
parent 3c29ffd0b7
commit 0897abf5cb
6 changed files with 113 additions and 55 deletions

View File

@ -547,7 +547,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
$assertion,
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);
@ -574,7 +575,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
$assertion,
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);
@ -1025,7 +1027,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
$negated xor $can_be_equal,
$can_be_equal,
$negated,
$code_location,
$suppressed_issues
);
@ -1039,6 +1042,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
true,
$negated,
$code_location,
$suppressed_issues
@ -1072,7 +1076,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
$negated xor $can_be_equal,
$can_be_equal,
$negated,
$code_location,
$suppressed_issues
);
@ -1166,7 +1171,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
$negated xor $can_be_equal,
$can_be_equal,
$negated,
$code_location,
$suppressed_issues
);
@ -1187,6 +1193,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
false,
$negated,
$code_location,
$suppressed_issues
@ -1228,7 +1235,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
$negated xor $can_be_equal,
$can_be_equal,
$negated,
$code_location,
$suppressed_issues
);
@ -1242,6 +1250,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
false,
$negated,
$code_location,
$suppressed_issues
@ -1275,7 +1284,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$var_id,
$assertion,
$negated xor $can_be_equal,
$can_be_equal,
$negated,
$code_location,
$suppressed_issues
);

View File

@ -259,7 +259,8 @@ class NegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!=' . $assertion,
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);
@ -276,6 +277,7 @@ class NegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!' . $assertion,
false,
$negated,
$code_location,
$suppressed_issues
@ -381,7 +383,8 @@ class NegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!' . $assertion,
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -400,7 +403,8 @@ class NegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!=' . $assertion,
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);

View File

@ -494,7 +494,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'non-empty-countable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -596,7 +597,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'positive-numeric',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -700,7 +702,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'object with method ' . $method_name,
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -791,7 +794,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'string',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -885,7 +889,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'int',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -963,7 +968,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'bool',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1037,7 +1043,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'scalar',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1128,7 +1135,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'numeric',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1212,7 +1220,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'object',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1268,7 +1277,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'resource',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1338,7 +1348,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'countable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1397,7 +1408,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'iterable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1541,7 +1553,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'Traversable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1613,7 +1626,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'array',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1731,7 +1745,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'array',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1801,7 +1816,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'string-array-access',
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);
@ -1860,7 +1876,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'int-or-string-array-access',
!$negated,
true,
$negated,
$code_location,
$suppressed_issues
);
@ -1956,7 +1973,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
'callable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -2269,7 +2287,8 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$old_var_type_string,
$key,
$assertion,
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);

View File

@ -301,7 +301,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!bool',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -382,7 +383,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!non-empty-countable',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -438,7 +440,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!null',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -503,7 +506,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!false',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -780,7 +784,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!' . $assertion,
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -861,7 +866,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!scalar',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -953,7 +959,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!object',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1042,7 +1049,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!numeric',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1137,7 +1145,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!int',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1227,7 +1236,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!float',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1330,7 +1340,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!string',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1430,7 +1441,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!array',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
@ -1499,7 +1511,8 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$old_var_type_string,
$key,
'!resource',
$negated xor !$did_remove_type,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);

View File

@ -810,15 +810,27 @@ class Reconciler
string $key,
string $assertion,
bool $redundant,
bool $negated,
CodeLocation $code_location,
array $suppressed_issues
): void {
$never = $assertion[0] === '!';
$not = $assertion[0] === '!';
if ($never) {
$safe_assertion = $assertion;
if ($not) {
$assertion = substr($assertion, 1);
}
if ($negated) {
$redundant = !$redundant;
$not = !$not;
}
if ($not) {
$assertion = '!' . $assertion;
}
$existing_var_atomic_types = $existing_var_type->getAtomicTypes();
$from_docblock = $existing_var_type->from_docblock
@ -831,9 +843,9 @@ class Reconciler
new RedundantConditionGivenDocblockType(
'Docblock-defined type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'never ' : 'always ') . $assertion,
. ' is always ' . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
$old_var_type_string . ' ' . $safe_assertion
),
$suppressed_issues
)) {
@ -844,9 +856,9 @@ class Reconciler
new RedundantCondition(
'Type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'never ' : 'always ') . $assertion,
. ' is always ' . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
$old_var_type_string . ' ' . $safe_assertion
),
$suppressed_issues
)) {
@ -859,9 +871,9 @@ class Reconciler
new DocblockTypeContradiction(
'Docblock-defined type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'always ' : 'never ') . $assertion,
. ' is never ' . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
$old_var_type_string . ' ' . $safe_assertion
),
$suppressed_issues
)) {
@ -872,17 +884,17 @@ class Reconciler
$issue = new TypeDoesNotContainNull(
'Type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'always ' : 'never ') . $assertion,
. ' is never ' . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
$old_var_type_string . ' ' . $safe_assertion
);
} else {
$issue = new TypeDoesNotContainType(
'Type ' . $old_var_type_string
. ' for ' . $key
. ' is ' . ($never ? 'always ' : 'never ') . $assertion,
. ' is never ' . $assertion,
$code_location,
$old_var_type_string . ' ' . $assertion
$old_var_type_string . ' ' . $safe_assertion
);
}

View File

@ -258,7 +258,7 @@ class ConditionalTest extends \Psalm\Tests\TestCase
'assertions' => [
'$b' => 'null',
],
'error_levels' => ['TypeDoesNotContainNull', 'RedundantCondition'],
'error_levels' => ['TypeDoesNotContainType', 'RedundantCondition'],
],
'ignoreNullCheckAndMaintainNullableValue' => [
'<?php