Skip to content

Don't take matching another pattern for a paradox - #14444

Merged
orklah merged 2 commits into
vimeo:masterfrom
phcorp:pattern-assertions-are-no-paradox
Nov 21, 2026
Merged

orklah merged 2 commits into
vimeo:masterfrom
phcorp:pattern-assertions-are-no-paradox

Conversation

@phcorp

@phcorp phcorp commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Matching another pattern after a failed match

Psalm reports a false ParadoxicalCondition for a preg_match() on a value that already failed another pattern removing the same taints.

Before

<?php
function f(string $value): int {
    if ("transparent" === $value) {
        return 0;
    }
    if (preg_match("/^light-dark\((.+)\)$/s", $value)) {
        return 1;
    }
    if (preg_match("/^color-mix\((.+)\)$/s", $value)) { // ParadoxicalCondition
        return 2;
    }

    return 3;
}

After

    if (preg_match("/^color-mix\((.+)\)$/s", $value)) { // no issue

Matching two patterns

Psalm reports a false RedundantCondition for two preg_match() calls with different patterns that remove the same taints.

Before

<?php
function f(string $v): int {
    if (preg_match('/^a\((.+)\)$/s', $v) && preg_match('/^b\((.+)\)$/s', $v)) { // RedundantCondition: $v is matches-pattern-8388608 has already been asserted
        return 1;
    }
    return 0;
}

After

    if (preg_match('/^a\((.+)\)$/s', $v) && preg_match('/^b\((.+)\)$/s', $v)) { // no issue

Fixes #14443.

A pattern assertion only holds the taints its pattern removes, the same
for another pattern removing the same taints: like in_array() assertions,
it can't tell that a condition contradicts an earlier one.

Fixes vimeo#14443

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The test fails on master with the ParadoxicalCondition of #14443 and passes here.

The same equality of MatchesPattern assertions still reaches the other place that compares clauses by value: the hash check of AlgebraAnalyzer::checkForParadox(), which skips InArray/NotInArray through hasInArrayPossibility() but not MatchesPattern. So this still reports a false RedundantCondition (also new in 7.13):

function f(string $v): int {
    if (preg_match('/^a\((.+)\)$/s', $v) && preg_match('/^b\((.+)\)$/s', $v)) { // RedundantCondition: $v is matches-pattern-8388608 has already been asserted
        return 1;
    }
    return 0;
}

Could you handle MatchesPattern there too (ideally one helper used by both findImplication() and checkForParadox(), with the comment on it, e.g. "these assertions don't say which pattern or array they were made from: two equal ones are not the same condition"), and add this snippet as a TypeAlgebraTest valid case?

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 757 96.9 $\color{gray}{\textsf{+1.4\%}}$ 25 912 53 476 682
✅ symfony 3 600 209.6 $\color{gray}{\textsf{+1.1\%}}$ 50 632 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-6}}$ 98 448 $\color{green}{\textsf{-54}}$ 1 528
✅ doctrine-orm 381 22.0 $\color{gray}{\textsf{+0.9\%}}$ 3 481 6 143 0
✅ psl 617 27.0 $\color{gray}{\textsf{-1.1\%}}$ 1 375 5 615 1
✅ phpunit 455 10.8 $\color{gray}{\textsf{+0.4\%}}$ 1 744 3 459 18
✅ php-parser 226 7.2 $\color{gray}{\textsf{+0.7\%}}$ 17 132 21 025 0
✅ tempest 895 19.7 $\color{gray}{\textsf{+0.1\%}}$ 5 048 10 525 252
✅ amphp-amp 68 1.7 29 49 0
✅ webmozarts-assert 272 4.3 $\color{gray}{\textsf{+1.9\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.5 $\color{gray}{\textsf{-0.7\%}}$ 155 434 2
✅ monolog 135 5.4 913 2 479 10
✅ drupal 4 841 253.9 $\color{gray}{\textsf{-3.2\%}}$ 97 589 169 294 583
Total 13 291 659.9 $\color{gray}{\textsf{-0.7\%}}$ 204 587 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-6}}$ 373 667 $\color{green}{\textsf{-54}}$ 3 076
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 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. 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{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.
✅ symfony: issues +0 −6

Memory 3 600 → 3 600 MB (<0.1 %) · Wall 207.2 → 209.6 s (+1.1 %)

Weighted 98 502 → 98 448 (−54, −0.05 %): L4 −1 · always −5

Issue type added removed
ParadoxicalCondition 0 5
RedundantCondition 0 1

Removed (6)

  • src/Symfony/Component/JsonPath/JsonCrawler.php:1181 RedundantCondition (error): $selector is not matches-pattern-8388608 has already been asserted
  • src/Symfony/Component/Tui/Style/TailwindStylesheet.php:296 ParadoxicalCondition (error): Condition ($class is matches-pattern-13378774) contradicts a previously-established condition ($class is not matches-pattern-13378774)
  • src/Symfony/Component/Tui/Style/TailwindStylesheet.php:299 ParadoxicalCondition (error): Condition ($class is matches-pattern-13378774) contradicts a previously-established condition ($class is not matches-pattern-13378774)
  • src/Symfony/Component/Tui/Style/TailwindStylesheet.php:302 ParadoxicalCondition (error): Condition ($class is matches-pattern-13378774) contradicts a previously-established condition ($class is not matches-pattern-13378774)
  • src/Symfony/Component/Tui/Style/TailwindStylesheet.php:340 ParadoxicalCondition (error): Condition ($class is matches-pattern-13378774) contradicts a previously-established condition ($class is not matches-pattern-13378774)
  • src/Symfony/Component/Tui/Style/TailwindStylesheet.php:345 ParadoxicalCondition (error): Condition ($class is matches-pattern-13378774) contradicts a previously-established condition ($class is not matches-pattern-13378774)

head 99542c1 · base cd22a51 (merge-base) · workflow run

Like in-array assertions, a MatchesPattern assertion doesn't say which pattern
it was made from, so the hash check of checkForParadox() reported a
RedundantCondition for two different patterns that remove the same taints.
One helper now skips these assertions in checkForParadox() and findImplication().
@phcorp

phcorp commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Done in 99542c1: hasUnidentifiedPossibility() now skips InArray, NotInArray and MatchesPattern in both checkForParadox() and findImplication(), with your comment on it.

The same RedundantCondition also came from !preg_match(a) && !preg_match(b) and from str_starts_with($u, "https://") && str_starts_with($u, "https://example.com/") (the URL-scheme assertion is a MatchesPattern too). These are TypeAlgebraTest valid cases now, next to your snippet. A TaintTest valid case covers the ParadoxicalCondition of an elseif matching another list of URL hosts. That one needs the taint graph, so TypeAlgebraTest can't show it.

The same pattern twice (preg_match(a, $v) && preg_match(a, $v)) is no longer reported either, like two in_array() calls on the same array and like 7.12.

@phcorp
phcorp requested a review from orklah November 21, 2026 08:56
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 21, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @phcorp!

@orklah
orklah merged commit c5f4dfd into vimeo:master Nov 21, 2026
70 of 71 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.

False ParadoxicalCondition for preg_match() with another pattern after a failed one

2 participants