Skip to content

Fix phpstan/phpstan#10786: Invalid return type definition#5397

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-oldcpwk
Open

Fix phpstan/phpstan#10786: Invalid return type definition#5397
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-oldcpwk

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When checking is_null($a->value) && is_null($b->value) and throwing, PHPStan should know that at least one property is non-null afterward. This already worked for plain variables but not for property fetches and other general expressions. The fix extends conditional expression holders to work with property fetches and fixes the isset handling for nullable native typed properties.

Changes

  • src/Analyser/TypeSpecifier.php: Replaced instanceof Expr\Variable checks in processBooleanSureConditionalTypes and processBooleanNotSureConditionalTypes with a new canBeConditionalExpressionHolder() helper that also accepts PropertyFetch, StaticPropertyFetch, and ArrayDimFetch. Simplified the self-exclusion loop to a simple unset($conditions[$exprString]).
  • src/Analyser/MutatingScope.php: In issetCheck for native typed properties not tracked in expression types, nullable properties now fall through to the type callback instead of returning issetCheckUndefined(). This allows isset() falsey to properly produce specified types that trigger conditional expression holders. Non-nullable properties retain the old behavior (may be uninitialized).

Root cause

Two issues combined:

  1. processBooleanSureConditionalTypes and processBooleanNotSureConditionalTypes in TypeSpecifier only created conditional expression holders for Expr\Variable, skipping property fetches entirely.
  2. issetCheck in MutatingScope returned null (via issetCheckUndefined) for nullable native typed properties not tracked in the scope, causing filterByFalseyValue(isset($a->value)) to produce empty specified types. Without specified types, the conditional holders could never fire.

Test

Added tests/PHPStan/Analyser/nsrt/bug-10786.php with two test methods verifying that $a->value ?? $b->value resolves to int (not int|null) after checking both properties for null and throwing.

Fixes phpstan/phpstan#10786

…tches in && conditions

- Extended ConditionalExpressionHolder creation in TypeSpecifier to support
  property fetches, static property fetches, and array dim fetches (not just variables)
- Fixed issetCheck for nullable native typed properties to fall through to type
  callback instead of returning undefined, enabling proper narrowing in ?? operator
- New regression test in tests/PHPStan/Analyser/nsrt/bug-10786.php
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