Skip to content

Report patterns that fail to match with a warning - #41

Open
pavog wants to merge 2 commits into
masterfrom
pattern-analyser-report-preg-errors
Open

pavog wants to merge 2 commits into
masterfrom
pattern-analyser-report-preg-errors

Conversation

@pavog

@pavog pavog commented Nov 13, 2026

Copy link
Copy Markdown
Member

Based on #36

When preg_match_all() returned false, for example because a pattern exceeded the backtrack limit, PatternAnalyser treated it like a pattern that doesn't match.
Such a pattern silently never found its problem, and nothing showed that it was broken.

findMatches() now triggers an E_USER_WARNING that names the pattern and the reason.
For example:

    preg_match_all() failed for pattern /I have a problem with (\w+)|(?:a+)+b/: Backtrack limit exhausted

The pattern is still treated as not matching, so the rest of the log is analysed as before.

This builds on #36, because the check lives in findMatches().

Decisions

  • A warning and not an exception: PHP itself reports an invalid pattern with a warning and false, and one failing pattern should not stop the analysis of the whole log.
  • The message names the pattern and not the insight class, because findMatches() only knows the pattern and the text.
  • Matches that were found before the pattern failed are discarded, as before.
  • An invalid pattern now produces two warnings, the one from PHP and this one.
    Only this one contains the pattern.

Changes

  • trigger a warning in PatternAnalyser::findMatches() when preg_match_all() fails
  • test that a failing pattern is reported and the other patterns and entries are still analysed
  • test that matches found before a pattern failed are discarded

Not covered

  • Projects that use codex only fail their tests on such a warning if PHPUnit runs with failOnWarning.
    Their current test logs don't trigger it.
  • An application that converts warnings to exceptions aborts the analysis when a pattern fails.

@pavog
pavog requested a review from matthi4s November 13, 2026 12:32
@pavog pavog added this to the v5.0.0 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant