Repository navigation
Use and update scope-tracked chain links when unset() rewrites a nested offset, and keep maybe-missing offsets optional - #6780
Open
phpstan-bot wants to merge 1 commit into
Conversation
…sted offset, and keep maybe-missing offsets optional
- AssignHandler (KIND_EXISTING_ARRAY_DIM_FETCH branch, used by unset($a[...][...])):
when the scope tracks a link of the chain (e.g. `$a[$k]` narrowed by
is_array()/isset() with a non-constant `$k`), read its container type from
the scope instead of `getOffsetValueType()` on the parent, and re-assign the
link's new type after the root variable is rewritten. Before, the root's
general array type answered `mixed` and the narrowing was lost.
- When the container only maybe has the offset, union the written type with
the "offset absent" branch (`unsetOffset()` for constant arrays, the
container itself otherwise) - unset() never creates a missing key, but
`setExistingOffsetValueType()` made optional keys required
(`array{a?: ...}` became `array{a: ...}`). This was a pre-existing bug that
the tracked links also surfaced in IgnoredErrorHelperResult.
- Ported both changes to the turbo-ext C++ mirror (turbo-ext/src/AssignHandler.cpp).
- Probed property (`$this->p[$k]['x']`), static property, and deeper nested
chains; they go through the same code path and are covered by the test.
- bug-12927: `$list[$k]` after `unset($list[$k]['abc'])` is now `array{}`
(was the looser `array{}|array{abc: string}`).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
After
unset($export[$key]['x'])with a non-constant$key, PHPStan forgot that$export[$key]had been narrowed to an array (byis_array()/isset()), so it becamemixed. With a constant key it worked, because the narrowing then lives in the root array's shape. This PR makes the virtual write thatunset()performs on the parent chain use and update the scope-tracked chain links. It also fixes a related, pre-existing bug: unsetting through an optional key made that key required.Changes
src/Analyser/ExprHandler/AssignHandler.php,KIND_EXISTING_ARRAY_DIM_FETCHbranch ofapplyWrite()(only reached fromUnsetHandler):ExistingArrayDimFetchlink is converted into the equivalentArrayDimFetch(newtoArrayDimFetch()). If the scope tracks it, its container type is read from the scope (getStateType()) instead ofgetOffsetValueType()on the parent.rebuildExistingOffsetChain()replaces the two duplicated loops (PHPDoc and native flavour).unsetOffset()for constant arrays, the container itself otherwise, which keeps list-ness.unset()never creates a missing key, butsetExistingOffsetValueType()turnedarray{a?: array{b: int, c: int}}intoarray{a: array{c: int}}afterunset($x['a']['b']).turbo-ext/src/AssignHandler.cpp: same change inapplyWriteExistingArrayDimFetch()/rebuildExistingOffsetChain(), plus a nativetoArrayDimFetch().$this->prop[$key]['x']), static property chains (self::$prop[$key]['x']), deeper chains ($a[$k][$k2]['x']) and unsetting a non-constant inner key (unset($a[$k][$k2])). They all share this code path and were all broken before; they are covered by the new test.Root cause
unset($a[$k]['x'])is modelled as the virtual write$a[$k] = <$a[$k] without 'x'>. That write rebuilt the chain only from the root variable's type, usinggetOffsetValueType()andsetExistingOffsetValueType(). It ignored the type the scope tracks for$a[$k]. For a general array root, that type ismixed. On top of that, rewriting the root invalidated the tracked$a[$k]. The rebuild also assumed every intermediate offset exists, so optional keys of constant arrays became required.Test
tests/PHPStan/Analyser/nsrt/bug-15461.php: the issue's two functions, nested, property and static property chains, a non-constant inner key, an untracked chain (unchanged behaviour), and the two optional-key cases (array{a?: ...}root, and an optional key under a tracked link).tests/PHPStan/Rules/Arrays/data/bug-15461.php+NonexistentOffsetInArrayDimFetchRuleTest::testBug15461: the playground sample verbatim, withreportPossiblyNonexistentGeneralArrayOffsetenabled, expecting no errors.tests/PHPStan/Rules/Methods/data/bug-12927.php: the expectation for$list[$k]afterunset($list[$k]['abc'])is tightened to the now-precisearray{}.All new assertions fail without the fix. The full test suite passes with and without the turbo extension loaded,
make phpstanis clean, and analysis output is identical with and without the extension.Fixes phpstan/phpstan#15461
🤖 Generated with Claude Code