Skip to content

Narrow array dim fetch types after isset+type guard in BooleanAnd falsey context#5686

Open
phpstan-bot wants to merge 1 commit into
phpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-111bklj
Open

Narrow array dim fetch types after isset+type guard in BooleanAnd falsey context#5686
phpstan-bot wants to merge 1 commit into
phpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-111bklj

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

  • After if (isset($array['key']) && !is_string($array['key'])) { throw; }, PHPStan now correctly narrows $array['key'] ?? '' to string instead of mixed|string
  • Added processBooleanTruthyConditionalTypes method to TypeSpecifier that creates ConditionalExpressionHolders when the falsey BooleanAnd handler encounters an empty left-side (like isset on a general array dim fetch)
  • The holders fire when filterByTruthyValue(isset(...)) is later confirmed during coalesce evaluation, applying the type narrowing from the right side of the original && condition

Test plan

  • New regression test tests/PHPStan/Analyser/nsrt/bug-6202.php covering:
    • Original bug: isset + !is_string guard with ?? coalesce
    • Analogous type guards: is_int, is_array, is_bool
    • instanceof check
    • !isset || !is_string or-pattern (already worked)
    • Nested array dim fetch
    • Direct access after guard (with second isset)
  • make tests passes (12078 tests, 79704 assertions)
  • make phpstan passes (no errors)
  • make cs-fix passes

Closes phpstan/phpstan#6202

…sey context

After `if (isset($array['key']) && !is_string($array['key'])) { throw; }`,
PHPStan now correctly narrows `$array['key'] ?? ''` to `string` instead of
`mixed|string`. The fix creates ConditionalExpressionHolders in the BooleanAnd
falsey handler that fire when the isset condition is later confirmed (e.g.,
during coalesce evaluation).

Closes phpstan/phpstan#6202
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.

2 participants