Repository navigation
fix(isset): keep the array value type when an ArrayAccess object is a union alternative - #13118
Conversation
… union alternative vimeo#4875 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Behaviour change
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 7e88954. Configure here.
✅ Performance: unchanged · Types: unchanged
How to read this table
head e7eb769 · base c5f4dfd (merge-base) · workflow run |
…union-isset # Conflicts: # tests/ArrayAccessTest.php
…narrowed on an object too vimeo#4875 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # tests/ArrayAccessTest.php
|
Checked the report's findings on the branch (merged with current master,
Finding 1 confirmed in part: for an intermediate segment (first row) the type is computed from the array alternative only ( Ran: probes on master and on the branch. No code change pushed. |
# Conflicts: # tests/ArrayAccessTest.php
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…aster dropped null from it vimeo#4875 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
alies-dev
left a comment
There was a problem hiding this comment.
🤖 Fixes the reported loss of the array value type at the correct layer (getValueForKey() no longer aborts the whole lookup because one union alternative is an ArrayAccess object); no blocking findings.
Already fixed on master: no. On upstream/master the issue's snippet still traces $test as ArrayAccess<string, string>|array{a: mixed, ...<string, string>} and $b = $test["a"] ?? "a" as mixed|string. With this PR merged into current master the traces are array{a: string, ...} and string.
Overlap / conflicts: tests/ArrayAccessTest.php conflicts with #13087 and #13344 (same insertion spot); trivial. #12633 conflicts textually in Reconciler::getValueForKey(), and the two interact: see my review of #12633 (the continue added here skips the $offset_is_set bookkeeping that PR adds).
Recommendations (non-blocking)
tests/ArrayAccessTest.php:1448(issetOnArrayOrArrayAccessKeepsTheValueTypeOfTheObject) pinsint|null|stringfor$test['a']insideisset($test['a']). Thenullcannot be there afterisset; it comes fromoffsetGet(): TValue|null, and it exists because the final key is deliberately dropped from the scope (:621), so a read recomputes from the union (@psalm-trace $test['a']in the branch is still "undefined variable"). Either record the narrowed final offset (array value type plus theArrayAccessvalue withoutnull), or do not pin a known-imprecise result in a test that reads as intended behaviour.- The open cursor[bot] comment (flag reuse at
Reconciler.php:1194) was written against an earlier commit; theelseif (!$did_type_exist) unset(...)branch now covers it.$has_object_array_accessnow means "an object alternative was skipped" as well as "abort"; a separate name would avoid the next reader repeating the same worry.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e of a union with an ArrayAccess Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed 5123fee. CI of the previous head failed in Run: those cases one by one, phpcs, a full self-analysis (clean). Not run: the whole suite. |
…ntermediate offset of an isset path
For `isset($test['a']['b'])` on `array{a?: array{b?: int}}|ArrayAccess<string, array{b?: string}>`
the type of `$test['a']` came from the array alternative only, so `$test['a']['b']` was `int`
although the object can give `string`. An ArrayAccess alternative now contributes the indexable
part of its value type to the intermediate key, and any other object alternative leaves the key
undefined, as before this PR.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
From the triage report generated 2026-11-19 05:16 UTC (reviewed head 5123fee); pushed fbb94f5.
Ran single: the 8 |
…e of an intermediate offset, never made it When no alternative of the base is an array, the intermediate offset of an isset or array_key_exists path is left as it is, since an object can give a different value on each offsetGet() call (the case arrayKeyExistsLeavesAnOffsetOfAnArrayAccessAsItIs, which failed after the previous commit). The value type of an ArrayAccess is only combined with the type that the array alternatives give. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Correction to my comment about fbb94f5: that commit broke Pushed 9c840c6: when no alternative of the base is an array, the intermediate offset is left as it is (an object can give a different value on each |
# Conflicts: # tests/ArrayAccessTest.php
alies-dev
left a comment
There was a problem hiding this comment.
🤖 Re-checked against the current head: nothing blocks this anymore, and it is ready to merge. Since the last review, other PRs landed on master and this branch now conflicts in tests/ArrayAccessTest.php (test cases only; #13087 and #13344 add cases at the same spot, so rebasing after them avoids a second round). Please rebase onto master and keep both sides' test cases; I'll merge once CI is green.
# Conflicts: # tests/ArrayAccessTest.php
# Conflicts: # tests/ArrayAccessTest.php
orklah
left a comment
There was a problem hiding this comment.
Thanks for this. One regression shows up in the perf report and reproduces locally: when the only other alternative of the object is null or false, the TNull/TFalse branch of getValueForKey() now becomes the offset's type, so the offset is recorded as null. This is the +1 RedundantCondition on Symfony (XliffFileLoader.php:144, $attributes is the ?SimpleXMLElement returned by attributes()):
function f(?SimpleXMLElement $a, bool $b): void {
if (!(isset($a['r']) || $b)) {
return;
}
echo isset($a['r']) && $a['r'] ? 1 : 0;
// master: no issue
// this PR: RedundantCondition: Type null for $a['r'] is always falsy
}Could you keep the narrowed type only when at least one array alternative (TArray, TKeyedArray, string) contributed to it, and give up as before (return null) otherwise? Please add this snippet as a test case (it must fail on the current head), plus the same shape with SomeObject|false.
The perf report also loses one PossiblyInvalidArrayAccess on Drupal (LocalTaskDefault.php:101, $this->pluginDefinition is array|PluginDefinitionInterface, an object that does not implement ArrayAccess, so the read still fails at runtime). Please check that it comes back with the fix above, or explain why it goes away.
Also, the "After" block of the description says null|string for $b, while issetOnArrayOrArrayAccessKeepsTheArrayValueType asserts string; please align it.
# Conflicts: # tests/ArrayAccessTest.php
…lternative gave its type vimeo#4875 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@orklah thanks, addressed in e7eb769:
Behaviour change (compared with the previous head)
All cases this PR adds pass one by one; full self-analysis clean (apart from the two local-environment |

Psalm infers
mixedforisset($test['a'])or$test['a'] ?? ...when$testisarray<string, string>|ArrayAccess<string, string>, and narrows the array alternative toarray{a: mixed}.Before
After
Fixes #4875