Repository navigation
Keep a variable whose native type allows null defined in !isset() with uncertain PHPDoc types - #7060
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes phpstan/phpstan#15728
With
treatPhpDocTypesAsCertain: false, the false branch ofisset($var)made a certainly-assigned variable undefined when its PHPDoc type is non-nullable but its native type allowsnull(e.g.$type = getInt()with@return intand 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. Theif/elseform has had this since 2.0. Since f884691 (#6822),TernaryHandlerpicks its final scope from the native condition type (bool) when PHPDoc types are uncertain, so it merges this falsey scope in, and everyisset($x) ? … : …left$xmaybe-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 tonull, the same way$var !== nullalready behaves (PHPDoc*NEVER*, nativenull). A maybe-defined variable gets "reduce certainty to maybe" instead of becoming undefined. Behaviour withtreatPhpDocTypesAsCertain: trueis unchanged.The turbo-ext mirror (
DefaultNarrowingHelper.cppplus the regenerated header) gets the same change and the new autowiredtreatPhpDocTypesAsCertainconstructor parameter.Verified locally: the new
DefinedVariableRuleWithUncertainPhpDocTypesTestfails before the fix (all four errors from the issue) and passes after.make testspasses with and without the extension loaded. The strict turbo build,tests/smoke.php,signature-parity.phpand method parity all pass, and analysis output is identical with the extension on and off.