Skip to content

fix(assertions): $a !== $b with several possible values of $b doesn't exclude them all - #14525

Merged
orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/10534-in-array-after-var-identity
Nov 23, 2026
Merged

orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/10534-in-array-after-var-identity

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

After if ($new === $old) return; with $old a union of literals, Psalm takes $new to be none of them, so a later check of those values is a false RedundantCondition.

Before

<?php
/** @param 1|2|3|5 $old */
function changeType(int $new, int $old): bool {
    if ($new === $old) {
        return true;
    }
    if (!in_array($new, [2, 3, 5], true)) { // RedundantCondition: $new is not int(5) has already been asserted (×3)
        return false;
    }
    return true;
}

After

    if (!in_array($new, [2, 3, 5], true)) { // no issue

The elseif after such a comparison

The same negation removes the values from the variable compared, so an elseif comparing it with another variable of those values is a false TypeDoesNotContainType.

Before

<?php
$a = rand(0, 1) ? "a" : "b";
$b = rand(0, 1) ? "a" : "b";
$s = rand(0, 1) ? $a : $b;
if (rand(0, 1)) {
    $s = "c";
}
if ($s === $a) {
} elseif ($s === $b) { // TypeDoesNotContainType: 'c' cannot be identical to 'a'|'b'
    $result = $s;      // 'c'
}

After

} elseif ($s === $b) { // no issue
    $result = $s;      // 'a'|'b'

Fixes #10534
Fixes #10487

… exclude them all vimeo#10534

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

$a === $b asserts that $a has one of the values of $b's type. When that type has several values (1|2|3|5), each IsIdentical of the assertion now records $b as its comparand (like non-literal types already did), so the negation ($a !== $b) no longer says $a is none of them: it is not identical to one value of $b, not to all. A single literal (@param 5 $old) is unchanged, as is the narrowing where they are identical ($new: 1|2 inside if ($new === $old), checked in the test).

Verified: RedundantConditionTest::testValidCode@notIdenticalToAVariableOfSeveralLiteralsDoesNotExcludeThem fails on master and passes here; the original nextcloud snippet of the issue no longer reports the 4 RedundantCondition. Corpora (errorLevel 1): laravel 27864, symfony 11700, drupal 33457 lines, identical before and after. Full self-analysis: no new issue (only 2 MixedArgument from my local revolt/event-loop v1.0.9, unrelated).

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 97.7 $\color{gray}{\textsf{-0.4\%}}$ 25 792 53 100 743 0
✅ symfony 3 619 193.7 $\color{gray}{\textsf{+4.0\%}}$ 51 767 100 629 1 582 0
✅ doctrine-orm 381 24.2 $\color{gray}{\textsf{-1.3\%}}$ 3 459 6 218 0 0
✅ psl 618 28.1 $\color{gray}{\textsf{-1.0\%}}$ 1 387 5 600 1 0
✅ phpunit 456 14.6 $\color{gray}{\textsf{-1.6\%}}$ 1 735 3 418 18 0
✅ php-parser 226 9.9 $\color{gray}{\textsf{+1.6\%}}$ 17 116 21 187 0 0
✅ tempest 898 28.3 $\color{gray}{\textsf{-1.6\%}}$ 5 094 10 556 275 0
✅ amphp-amp 48 1.2 $\color{gray}{\textsf{-2.4\%}}$ 29 49 0 0
✅ webmozarts-assert 273 3.6 $\color{gray}{\textsf{-1.4\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 1.3 $\color{gray}{\textsf{+0.8\%}}$ 155 433 2 0
✅ monolog 136 4.7 $\color{gray}{\textsf{-0.8\%}}$ 918 2 491 11 0
✅ drupal 4 847 186.0 97 949 169 915 575 0
✅ wordpress 2 061 190.1 $\color{gray}{\textsf{+0.3\%}}$ 143 633 678 503 505 1
Total 15 381 783.5 $\color{gray}{\textsf{+0.8\%}}$ 349 609 1 054 813 3 712 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head bebe72b · base b5ea40d (merge-base) · workflow run

… doesn't exclude them vimeo#10487

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change (update, 2nd commit): the negation of $a === $b with several possible values of $b (an IsNotIdentical/IsNotLooselyEqual of a literal with a comparand) no longer removes that literal from $a in NegatedAssertionReconciler, so the else/elseif of such a comparison keeps the values (#10487: $s === $b in the elseif is no longer a false TypeDoesNotContainType, $s is 'a'|'b' there instead of 'c'). Comparisons with a single literal or a single-valued variable still exclude it. New test ValueTest::testValidCode@notIdenticalToAVariableOfSeveralLiteralsKeepsThemInTheElseif fails on master and passes here; existing ValueTest@ifThisOrThat / @ifDifferentString still pass. Corpora (laravel/symfony/drupal) identical and full self-analysis clean again with this commit.

# Conflicts:
#	tests/TypeReconciliation/RedundantConditionTest.php
# Conflicts:
#	tests/TypeReconciliation/RedundantConditionTest.php
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026
@orklah

orklah commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit 58c1da0 into vimeo:master Nov 23, 2026
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in 5.18.0 throws RedundantCondition after comparing with === to another value of the same type Literal Strings else/elseif incorrect type

2 participants