Skip to content

fix(algebra): a condition is paradoxical when all the clauses of its negation are implied - #13358

Merged
orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/5712-paradox-all-negated-clauses
Nov 9, 2026
Merged

orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/5712-paradox-all-negated-clauses

Conversation

@jirkace

@jirkace jirkace commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Psalm reports a false ParadoxicalCondition for an in_array() whose list holds a value an earlier condition excluded.

Before

<?php
const A = "a";
const ALL = [A, "b", "c"];

function test(string $value): int {
    if ($value === A) {
        return 1;
    }

    if (in_array($value, ALL, true)) { // ParadoxicalCondition: Condition ($value is string(a)) contradicts a previously-established condition ($value is not string(a))
        return 2;
    }

    return 3;
}

After

    if (in_array($value, ALL, true)) { // no issue

Fixes #5712

@jirkace

jirkace commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • AlgebraAnalyzer::checkForParadox() reported ParadoxicalCondition as soon as one clause of the negation of the new condition was implied by a clause that is already established. The negation of a condition is the conjunction of its clauses, so the condition is only impossible when the established clauses imply all of them: an in_array() over a list (or an ||) that holds a value an earlier condition excluded is not impossible, only one of its alternatives is. Now all the (reconcilable, non-tautological) clauses of the negation must be implied.
  • Visible effects: no ParadoxicalCondition for if ($v === "a") return; if (in_array($v, ["a", "b"], true)) (also with constants, the report of ParadoxicalCondition if array has previously used value in condition #5712) and for if ($v === "a") return; if ($v === "a" || $v === "b"); a condition whose alternatives are all excluded (if ($v === "a" || $v === "b") return; if ($v === "a" || $v === "b")) and the single-clause cases (switch/match duplicates, ternary, call arguments) are still reported: the nine existing tests with ParadoxicalCondition that I ran (SwitchTypeTest, MatchTest, TypeAlgebraTest::paradoxInTernary, FunctionCallTest::disallowNeverTypeForParam, ...) pass unchanged.
  • One of the issues of this check was in Psalm's own code: the baseline entry for in_array($method_id, ArgumentsAnalyzer::ARRAY_FILTERLIKE, true) in ArrayFunctionArgumentsAnalyzer is removed, as self-analysis reported it as unused with the fix.
  • Verified: the two new valid TypeAlgebraTest cases fail on master and pass here (the invalid guard passes on both); phpcs and full self-analysis are clean. Not run: the rest of the test suite and the perf report.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Issues: unchanged

Project Memory (MB) Wall (s) Issues Weighted
✅ laravel 1 819 73.0 $\color{gray}{\textsf{-0.6\%}}$ 26 182 51 289
✅ symfony 4 007 245.0 $\color{gray}{\textsf{+0.8\%}}$ 51 245 100 913
✅ doctrine-orm 429 18.2 $\color{gray}{\textsf{+1.2\%}}$ 3 510 5 576
✅ psl 652 22.6 1 392 5 434
✅ phpunit 507 18.0 $\color{gray}{\textsf{-1.7\%}}$ 1 614 1 248
✅ php-parser 245 18.2 $\color{gray}{\textsf{-0.1\%}}$ 17 169 20 624
✅ tempest 923 31.8 $\color{gray}{\textsf{-0.2\%}}$ 5 012 9 563
✅ amphp-amp 61 2.2 $\color{gray}{\textsf{-1.8\%}}$ 32 79
✅ webmozarts-assert 274 5.0 $\color{gray}{\textsf{+0.6\%}}$ 577 2 714
✅ guzzle-promises 55 2.2 $\color{gray}{\textsf{-1.8\%}}$ 162 405
✅ monolog 161 5.5 $\color{gray}{\textsf{+0.2\%}}$ 1 011 2 792
✅ drupal 5 198 195.0 98 856 161 469
Total 14 330 636.6 $\color{gray}{\textsf{+0.2\%}}$ 206 762 362 106
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 +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted: issues weighted by error level (levels 1–8 as is, always-errors 10, feature-specific and purity suggestions 0). 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.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • 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{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 ⚠️).
  • Issues (heading): the total weighted change, increased · decreased · unchanged. Informational, never fails the check.

head ed424e5 · base a376df1 (merge-base) · workflow run

@orklah

orklah commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Please rebase

@jirkace
jirkace force-pushed the fix/5712-paradox-all-negated-clauses branch from fdde53e to 362611f Compare November 9, 2026 19:56
…negation are implied vimeo#5712

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@orklah
orklah force-pushed the fix/5712-paradox-all-negated-clauses branch from 362611f to ed424e5 Compare November 9, 2026 20:14
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 9, 2026
@orklah
orklah merged commit a7f9fd9 into vimeo:master Nov 9, 2026
73 of 74 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.

ParadoxicalCondition if array has previously used value in condition

2 participants