Skip to content

[CodeQuality] Skip AssertIssetToSpecificMethodRector on ArrayAccess objects#692

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-assert-isset-array-access
Jun 29, 2026
Merged

[CodeQuality] Skip AssertIssetToSpecificMethodRector on ArrayAccess objects#692
TomasVotruba merged 1 commit into
mainfrom
fix-assert-isset-array-access

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Bug

AssertIssetToSpecificMethodRector turns isset($x[$key]) inside an assert into
assertArrayHasKey($key, $x). That is correct for native arrays, but not for
ArrayAccess objects:

  • assertArrayHasKey() requires the key to be int|string; isset($obj[$key])
    (i.e. offsetExists()) accepts any key type.
  • offsetExists() semantics can differ from array_key_exists().

Found in webonyx/graphql-php. Its
MixedStoreTest exercises an ArrayAccess store with keys of every type
(null, bool, float, array, object). The conversion produces code that
throws for those keys, which is why rector.php skips the rule on that file:

-self::assertTrue(isset($this->mixedStore[$key]), $err);
+self::assertArrayHasKey($key, $this->mixedStore, $err);

Fix

Skip the rule when the array-dim subject is an ArrayAccess object. Native
arrays (the intended target) are unaffected. Added skip_array_access_object.php.inc

  • an ArrayAccess source stub.

…bjects

isset($obj[$key]) on an ArrayAccess object is not equivalent to
assertArrayHasKey($key, $obj): the key may be any type (assertArrayHasKey()
requires int|string) and offsetExists() semantics can differ from
array_key_exists(). Converting it produced code that throws for non-scalar
keys. Skip the rule when the subject is an ArrayAccess object.
@TomasVotruba TomasVotruba merged commit ef9ace2 into main Jun 29, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the fix-assert-isset-array-access branch June 29, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant