Skip to content

Keep a variable whose native type allows null defined in !isset() with uncertain PHPDoc types - #7060

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
fix-15728-isset-uncertain-phpdoc
Nov 14, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
fix-15728-isset-uncertain-phpdoc

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Closes phpstan/phpstan#15728

With treatPhpDocTypesAsCertain: false, the false branch of isset($var) made a certainly-assigned variable undefined when its PHPDoc type is non-nullable but its native type allows null (e.g. $type = getInt() with @return int and no native return type).

Root cause: DefaultNarrowingHelper::createIssetSingleSubjectNonTrueTypes() decided "the variable cannot exist in !isset()" from the PHPDoc type alone. A variable's existence is shared by the PHPDoc and native types, so this removed the variable from the native view too. The if/else form has had this since 2.0. Since f884691 (#6822), TernaryHandler picks its final scope from the native condition type (bool) when PHPDoc types are uncertain, so it merges this falsey scope in, and every isset($x) ? … : … left $x maybe-undefined after it.

Fix: when PHPDoc types are not certain, the variable branch also checks the native type. If the native type allows null, the variable stays defined and is narrowed to null, the same way $var !== null already behaves (PHPDoc *NEVER*, native null). A maybe-defined variable gets "reduce certainty to maybe" instead of becoming undefined. Behaviour with treatPhpDocTypesAsCertain: true is unchanged.

The turbo-ext mirror (DefaultNarrowingHelper.cpp plus the regenerated header) gets the same change and the new autowired treatPhpDocTypesAsCertain constructor parameter.

Verified locally: the new DefinedVariableRuleWithUncertainPhpDocTypesTest fails before the fix (all four errors from the issue) and passes after. make tests passes with and without the extension loaded. The strict turbo build, tests/smoke.php, signature-parity.php and method parity all pass, and analysis output is identical with the extension on and off.

…h uncertain PHPDoc types

With treatPhpDocTypesAsCertain off, the falsey narrowing of isset($var)
decided from the PHPDoc type alone that a non-nullable variable cannot
exist, so a variable typed int by PHPDoc but mixed natively became
undefined in the false branch. The variable's existence is shared by the
PHPDoc and native types, so it now also consults the native type: when
that allows null, the variable stays defined and is narrowed to null.

Since the ternary picks its final scope from the native condition type,
this also made every isset() ternary leave the variable maybe-undefined.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d722-68ee-734b-998f-884bc2a71ea4
@ondrejmirtes
ondrejmirtes merged commit ee76055 into 2.3.x Nov 14, 2026
903 of 907 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix-15728-isset-uncertain-phpdoc branch November 14, 2026 09:08
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.

With treatPhpDocTypesAsCertain: false, the false branch of isset($var) makes an assigned variable undefined; now also in every ternary

1 participant