Skip to content

fix(stubs): preg_grep() can return false - #14574

Merged
orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/9543-preg-grep-false
Nov 23, 2026
Merged

orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/9543-preg-grep-false

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Psalm types preg_grep() as never returning false, so the check of its failure value is reported as impossible.

Before

<?php
$rc = preg_grep("/^foo/i", ["foobar", "toobar"]);

if ($rc === false) { // DocblockTypeContradiction: array<int<0, 1>, string> does not contain false
    $r = null;
} else {
    $r = $rc;
}

After

if ($rc === false) { // no issue
// $rc: array<int<0, 1>, string>|false

Fixes #9543

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

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • preg_grep() returns array<TKey, string>|false with @psalm-ignore-falsable-return, like preg_match(), preg_match_all() and preg_split(). PHP returns false on failure, e.g. for an invalid pattern.
  • With the default ignoreInternalFunctionFalseReturn="false", the false is now reported where the result is used without a check, as it already is for preg_split(). Corpus diffs against master: laravel +1 (RiskyTruthyFalsyComparison); symfony +5 (RiskyTruthyFalsyComparison ×3, PossiblyFalseArgument ×2); drupal +8/−1 (PossiblyFalseArgument ×3, PossiblyFalseIterator, FalsableReturnStatement/InvalidFalsableReturnType and the Mixed* issues that follow a PossiblyFalseArgument to array_merge). DocblockTypeContradiction on === false checks is gone.
  • With ignoreInternalFunctionFalseReturn="true" nothing new is reported.
  • Psalm's own IncludeCollector::getFilteredIncludedFiles() iterated the preg_grep() result unchecked. It now filters the phar:// paths with array_filter() + str_starts_with(), which has no failure value.
  • Verified: the new case fails on master and passes on the branch; the existing preg_grep case passes; self-analysis reports only the two known environment MixedArguments.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: increased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 749 95.0 $\color{gray}{\textsf{-0.3\%}}$ 25 909 $\color{red}{\textsf{+2}}$ $\color{gray}{\textsf{-0}}$ 53 464 $\color{red}{\textsf{+4}}$ 682
✅ symfony 3 599 180.9 $\color{gray}{\textsf{+1.0\%}}$ 50 617 $\color{red}{\textsf{+8}}$ $\color{green}{\textsf{-1}}$ 98 420 $\color{red}{\textsf{+17}}$ 1 528
✅ doctrine-orm 380 20.5 $\color{gray}{\textsf{-1.3\%}}$ 3 481 6 143 0
✅ psl 617 26.9 $\color{gray}{\textsf{-0.1\%}}$ 1 375 5 615 1
✅ phpunit 455 14.3 $\color{gray}{\textsf{+0.3\%}}$ 1 731 3 410 18
✅ php-parser 226 9.3 $\color{gray}{\textsf{-0.5\%}}$ 17 154 21 245 0
✅ tempest 895 26.5 $\color{gray}{\textsf{-0.2\%}}$ 5 047 10 528 252
✅ amphp-amp 45 1.4 $\color{gray}{\textsf{+0.7\%}}$ 29 49 0
✅ webmozarts-assert 273 4.3 $\color{gray}{\textsf{+0.2\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.5 $\color{gray}{\textsf{+1.3\%}}$ 155 434 2
✅ monolog 135 5.5 $\color{gray}{\textsf{-1.1\%}}$ 913 2 479 10
✅ drupal 4 842 237.7 $\color{gray}{\textsf{-0.1\%}}$ 97 607 $\color{red}{\textsf{+17}}$ $\color{green}{\textsf{-1}}$ 169 336 $\color{red}{\textsf{+43}}$ 583
Total 13 260 623.8 $\color{gray}{\textsf{+0.1\%}}$ 204 595 $\color{red}{\textsf{+27}}$ $\color{green}{\textsf{-2}}$ 373 843 $\color{red}{\textsf{+64}}$ 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.
✅ laravel: issues +2 −0

Memory 1 749 → 1 749 MB (<0.1 %) · Wall 95.4 → 95.0 s (−0.3 %)

Weighted 53 460 → 53 464 (+4, +0.01 %): L2 +2

Issue type added removed
RiskyTruthyFalsyComparison 2 0

Added (2)

  • src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:238 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • src/Illuminate/Validation/Concerns/ValidatesAttributes.php:928 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
✅ symfony: issues +8 −1

Memory 3 599 → 3 599 MB (<0.1 %) · Wall 179.1 → 180.9 s (+1.0 %)

Weighted 98 403 → 98 420 (+17, +0.02 %): L2 +4 · L3 +3

Issue type added removed
MixedArgument 1 1
PossiblyFalseArgument 3 0
RiskyTruthyFalsyComparison 4 0

Changed in place (1)

  • src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php:234 MixedArgument: …cannot be mixed|non-empty-array<array-key, stringmixed>, expecting array<array-key, mixed>

Added (7)

  • src/Symfony/Component/Console/Application.php:753 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • src/Symfony/Component/Console/Application.php:809 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • src/Symfony/Component/Console/Application.php:813 PossiblyFalseArgument (error): Argument 1 of sort cannot be false, possibly array<array-key, mixed> value expected
  • src/Symfony/Component/Console/Application.php:816 PossiblyFalseArgument (error): Argument 1 of count cannot be false, possibly Countable|array<array-key, mixed> value expected
  • src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php:231 PossiblyFalseArgument (error): Argument 2 of array_merge cannot be false, possibly array<array-key, mixed> value expected
  • src/Symfony/Component/Security/Core/Role/RoleHierarchy.php:134 RiskyTruthyFalsyComparison (error): Operand of type array<int<0, max>, string>|false contains type array<int<0, max>, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • src/Symfony/Component/Yaml/Parser.php:177 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
✅ drupal: issues +17 −1

Memory 4 842 → 4 842 MB (<0.1 %) · Wall 237.9 → 237.7 s (−0.1 %)

Weighted 169 293 → 169 336 (+43, +0.03 %): L1 +3 · L2 +3 · L3 +8 · L5 +2

Issue type added removed
DocblockTypeContradiction 0 1
FalsableReturnStatement 2 0
InvalidFalsableReturnType 2 0
MixedArgument 1 0
MixedAssignment 2 0
PossiblyFalseArgument 3 0
PossiblyFalseIterator 5 0
RiskyTruthyFalsyComparison 2 0

Added (17)

  • core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/RequiredConfigDependenciesConstraintValidator.php:71 RiskyTruthyFalsyComparison (error): Operand of type array<array-key, string>|false contains type array<array-key, string>, which can be falsy and truthy. This can cause possibly unexpected …
  • core/lib/Drupal/Core/Database/Schema.php:218 InvalidFalsableReturnType (error): The declared return type 'array<array-key, mixed>' for Schema::findTables does not allow false, but 'array<non-falsy-string, string>|false' contains false
  • core/lib/Drupal/Core/Database/Schema.php:261 FalsableReturnStatement (error): The declared return type 'array<array-key, mixed>' for Schema::findTables does not allow false, but the function returns 'array<non-falsy-string, string>|false'
  • core/lib/Drupal/Core/Hook/HookCollectorPass.php:181 PossiblyFalseIterator (error): Cannot iterate over falsable var array<int<0, max>, string>|false
  • core/lib/Drupal/Core/Test/TestDiscovery.php:253 PossiblyFalseArgument (error): Argument 1 of array_flip cannot be false, possibly array<array-key, mixed> value expected
  • core/lib/Drupal/Core/Theme/Registry.php:982 PossiblyFalseArgument (error): Argument 1 of array_merge cannot be false, possibly array<array-key, mixed> value expected
  • core/lib/Drupal/Core/Theme/Registry.php:985 MixedAssignment (error): Unable to determine the type that $function is being assigned to
  • core/lib/Drupal/Core/Theme/Registry.php:986 MixedArgument (error): Argument 2 of explode cannot be mixed, expecting string
  • core/lib/Drupal/Core/Theme/Registry.php:987 MixedAssignment (error): Unable to determine the type of this assignment
  • core/lib/Drupal/Core/Update/UpdateHookRegistry.php:116 PossiblyFalseIterator (error): Cannot iterate over falsable var array<int<0, max>, string>|false
  • … and 7 more

Removed (1)

  • core/lib/Drupal/Core/Config/Action/ConfigActionManager.php:231 DocblockTypeContradiction (error): array<array-key, string> does not contain false

head b26ca5a · base 46d8098 (merge-base) · workflow run

@jirkace

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Correction to my corpus numbers: they came from my local subsets. On the full projects, the perf report of the current head (b26ca5a) shows the same kinds of issues in larger numbers:

  • laravel +2: RiskyTruthyFalsyComparison ×2 (Eloquent/Concerns/GuardsAttributes.php:238, Validation/Concerns/ValidatesAttributes.php:928).
  • symfony +8 −1: RiskyTruthyFalsyComparison ×4, PossiblyFalseArgument ×3 (e.g. Console/Application.php:813 sort(), :816 count(), Messenger/Command/ConsumeMessagesCommand.php:231 array_merge()), one MixedArgument changed in place.
  • drupal +17 −1: PossiblyFalseIterator ×5, PossiblyFalseArgument ×3, FalsableReturnStatement ×2, InvalidFalsableReturnType ×2, RiskyTruthyFalsyComparison ×2, and MixedAssignment ×2 / MixedArgument ×1 that follow a false result; one DocblockTypeContradiction (an === false check) is gone.

All of them are places that use the result of preg_grep() without handling false, as described above. With ignoreInternalFunctionFalseReturn="true" they don't appear. Not checked: each line against the source. This is read from the report.

@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 9d64b8d 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.

preg_grep returns boolean false but psalm does not agree

2 participants