Skip to content
Permalink
Browse files

Improve string comparison negation

  • Loading branch information
muglug committed Dec 22, 2019
1 parent 4c8730c commit 3be8e4c6ba206006e5cdaf7e5295dc413c69a0b0
Showing with 69 additions and 1 deletion.
  1. +69 −1 src/Psalm/Internal/Type/NegatedAssertionReconciler.php
@@ -146,6 +146,16 @@ public static function reconcile(
);
}

if ($assertion === 'string' && !$existing_var_type->hasMixed() && !$is_equality) {
return self::reconcileString(
$existing_var_type,
$key,
$code_location,
$suppressed_issues,
$failed_reconciliation
);
}

if ($assertion === 'falsy' || $assertion === 'empty') {
return self::reconcileFalsyOrEmpty(
$assertion,
@@ -928,7 +938,7 @@ private static function reconcileNumeric(
$old_var_type_string,
$key,
'!numeric',
$did_remove_type,
!$did_remove_type,
$code_location,
$suppressed_issues
);
@@ -948,6 +958,64 @@ private static function reconcileNumeric(
return Type::getMixed();
}

/**
* @param string[] $suppressed_issues
* @param 0|1|2 $failed_reconciliation
*/
private static function reconcileString(
Type\Union $existing_var_type,
?string $key,
?CodeLocation $code_location,
array $suppressed_issues,
int &$failed_reconciliation
) : Type\Union {
$old_var_type_string = $existing_var_type->getId();
$non_string_types = [];
$did_remove_type = $existing_var_type->hasScalar();

foreach ($existing_var_type->getTypes() as $type) {
if ($type instanceof TTemplateParam) {
if (!$type->as->hasString()) {
if ($type->as->hasMixed()) {
$did_remove_type = true;
}

$non_string_types[] = $type;
}
} elseif (!$type instanceof TString) {
$non_string_types[] = $type;
} else {
$did_remove_type = true;
}
}

if ((!$non_string_types || !$did_remove_type)) {
if ($key && $code_location) {
self::triggerIssueForImpossible(
$existing_var_type,
$old_var_type_string,
$key,
'!string',
!$did_remove_type,
$code_location,
$suppressed_issues
);
}

if (!$did_remove_type) {
$failed_reconciliation = 1;
}
}

if ($non_string_types) {
return new Type\Union($non_string_types);
}

$failed_reconciliation = 2;

return Type::getMixed();
}

/**
* @return void
*/

0 comments on commit 3be8e4c

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