Skip to content

Commit

Permalink
use better issue types
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Oct 20, 2021
1 parent 4755131 commit cf8e443
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
54 changes: 33 additions & 21 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Exception\TypeParseTreeException;
use Psalm\Issue\DocblockTypeContradiction;
use Psalm\Issue\ParadoxicalCondition;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\RedundantConditionGivenDocblockType;
Expand All @@ -21,11 +22,15 @@
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEmpty;
use Psalm\Type\Atomic\TEmptyMixed;
use Psalm\Type\Atomic\TEmptyNumeric;
use Psalm\Type\Atomic\TEmptyScalar;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyArray;
Expand Down Expand Up @@ -2239,19 +2244,26 @@ private static function reconcileFalsyOrEmpty(
}

if ($did_remove_type && $existing_var_type->getAtomicTypes() === []) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new ParadoxicalCondition(
if ($code_location && $key) {
if ($existing_var_type->from_docblock) {
$issue = new DocblockTypeContradiction(
'Found a paradox when evaluating ' . $key
. ' of type ' . $old_var_type_string
. ' and trying to reconcile it with a ' . $assertion . ' assertion',
$code_location,
null
);
} else {
$issue = new ParadoxicalCondition(
'Found a paradox when evaluating ' . $key
. ' of type ' . $old_var_type_string
. ' and trying to reconcile it with a ' . $assertion . ' assertion',
$code_location
),
$suppressed_issues
)
) {
// fall through
);
}
if (IssueBuffer::accepts($issue, $suppressed_issues)) {
// fall through
}
}

$failed_reconciliation = 2;
Expand All @@ -2268,8 +2280,8 @@ private static function reconcileFalsyOrEmpty(
$existing_var_type->removeType('array');
$existing_var_type->addType(new TArray(
[
new Type\Union([new TEmpty]),
new Type\Union([new TEmpty]),
new Type\Union([new TEmpty()]),
new Type\Union([new TEmpty()]),
]
));
}
Expand All @@ -2279,7 +2291,7 @@ private static function reconcileFalsyOrEmpty(

if (get_class($mixed_atomic_type) === TMixed::class) {
$existing_var_type->removeType('mixed');
$existing_var_type->addType(new Type\Atomic\TEmptyMixed());
$existing_var_type->addType(new TEmptyMixed());
} elseif ($existing_var_type->isSingle() && $mixed_atomic_type instanceof TEmptyMixed) {
if ($code_location && $key && !$did_remove_type) {
if ($existing_var_type->from_docblock) {
Expand Down Expand Up @@ -2353,17 +2365,17 @@ private static function reconcileFalsyOrEmpty(

if (get_class($string_atomic_type) === TString::class) {
$existing_var_type->removeType('string');
$existing_var_type->addType(new Type\Atomic\TLiteralString(''));
$existing_var_type->addType(new Type\Atomic\TLiteralString('0'));
$existing_var_type->addType(new TLiteralString(''));
$existing_var_type->addType(new TLiteralString('0'));
} elseif (get_class($string_atomic_type) === TNonEmptyString::class) {
$existing_var_type->removeType('string');
$existing_var_type->addType(new Type\Atomic\TLiteralString('0'));
$existing_var_type->addType(new TLiteralString('0'));
} elseif (get_class($string_atomic_type) === TNonEmptyLowercaseString::class) {
$existing_var_type->removeType('string');
$existing_var_type->addType(new Type\Atomic\TLiteralString('0'));
$existing_var_type->addType(new TLiteralString('0'));
} elseif (get_class($string_atomic_type) === TNonEmptyNonspecificLiteralString::class) {
$existing_var_type->removeType('string');
$existing_var_type->addType(new Type\Atomic\TLiteralString('0'));
$existing_var_type->addType(new TLiteralString('0'));
} elseif ($existing_var_type->isSingle() && $string_atomic_type instanceof TNonFalsyString) {
if ($code_location && $key && !$did_remove_type) {
if ($existing_var_type->from_docblock) {
Expand Down Expand Up @@ -2401,12 +2413,12 @@ private static function reconcileFalsyOrEmpty(
foreach ($existing_range_types as $int_key => $literal_type) {
if ($literal_type->contains(0)) {
$existing_var_type->removeType($int_key);
$existing_var_type->addType(new Type\Atomic\TLiteralInt(0));
$existing_var_type->addType(new TLiteralInt(0));
}
}
} else {
$existing_var_type->removeType('int');
$existing_var_type->addType(new Type\Atomic\TLiteralInt(0));
$existing_var_type->addType(new TLiteralInt(0));
}

if ($existing_var_type->isSingle()) {
Expand All @@ -2416,7 +2428,7 @@ private static function reconcileFalsyOrEmpty(

if ($existing_var_type->hasFloat()) {
$existing_var_type->removeType('float');
$existing_var_type->addType(new Type\Atomic\TLiteralFloat(0.0));
$existing_var_type->addType(new TLiteralFloat(0.0));

if ($existing_var_type->isSingle()) {
return $existing_var_type;
Expand All @@ -2425,7 +2437,7 @@ private static function reconcileFalsyOrEmpty(

if ($existing_var_type->hasNumeric()) {
$existing_var_type->removeType('numeric');
$existing_var_type->addType(new Type\Atomic\TEmptyNumeric());
$existing_var_type->addType(new TEmptyNumeric());

if ($existing_var_type->isSingle()) {
return $existing_var_type;
Expand Down
27 changes: 17 additions & 10 deletions src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Psalm\Internal\Type;

use Psalm\CodeLocation;
use Psalm\Issue\DocblockTypeContradiction;
use Psalm\Issue\ParadoxicalCondition;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\RedundantConditionGivenDocblockType;
Expand Down Expand Up @@ -599,20 +600,26 @@ private static function reconcileFalsyOrEmpty(
}

if ($did_remove_type && $existing_var_type->getAtomicTypes() === []) {
if ($code_location
&& $key
&& !$is_empty_assertion
&& IssueBuffer::accepts(
new ParadoxicalCondition(
if ($code_location && $key && !$is_empty_assertion) {
if ($existing_var_type->from_docblock) {
$issue = new DocblockTypeContradiction(
'Found a paradox when evaluating ' . $key
. ' of type ' . $old_var_type_string
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
null
);
} else {
$issue = new ParadoxicalCondition(
'Found a paradox when evaluating ' . $key
. ' of type ' . $old_var_type_string
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location
),
$suppressed_issues
)
) {
// fall through
);
}
if (IssueBuffer::accepts($issue, $suppressed_issues)) {
// fall through
}
}

$failed_reconciliation = 2;
Expand Down

0 comments on commit cf8e443

Please sign in to comment.