@@ -173,5 +173,51 @@ public function testStringLiteralsWithNumbers()
173173 . \var_export ($ expected [$ itemNr ], true ) . ' for item number ' . $ itemNr
174174 );
175175 }
176+
177+ // Verify against handling by PHP itself.
178+ $ expectedKeys = array_values ($ expected );
179+ $ actualKeys = array_keys (array_combine ($ expected , $ expected ));
180+ $ this ->assertSame ($ expectedKeys , $ actualKeys , 'getActualArrayKey() results do not match PHP native handling ' );
181+ }
182+
183+ /**
184+ * Test retrieving the actual array key when string keys look like numeric keys but start with 0.
185+ *
186+ * @return void
187+ */
188+ public function testZeroPrefixedNumericStringKeys ()
189+ {
190+ $ testObj = new ArrayDeclarationSniffTestDouble ();
191+ $ testObj ->tokens = self ::$ phpcsFile ->getTokens ();
192+
193+ $ stackPtr = $ this ->getTargetToken ('/* testZeroPrefixedNumericStringKeys */ ' , [\T_ARRAY , \T_OPEN_SHORT_ARRAY ]);
194+ $ arrayItems = PassedParameters::getParameters (self ::$ phpcsFile , $ stackPtr );
195+
196+ $ expected = [
197+ 1 => '01 ' ,
198+ 2 => '002 ' ,
199+ 3 => '0o3 ' ,
200+ 4 => '0b1 ' ,
201+ 5 => '0x7 ' , // phpcs:ignore PHPCompatibility.Miscellaneous.ValidIntegers.HexNumericStringFound
202+ 6 => '0.0 ' ,
203+ ];
204+
205+ $ this ->assertCount (\count ($ expected ), $ arrayItems );
206+
207+ foreach ($ arrayItems as $ itemNr => $ arrayItem ) {
208+ $ arrowPtr = Arrays::getDoubleArrowPtr (self ::$ phpcsFile , $ arrayItem ['start ' ], $ arrayItem ['end ' ]);
209+ $ result = $ testObj ->getActualArrayKey (self ::$ phpcsFile , $ arrayItem ['start ' ], ($ arrowPtr - 1 ));
210+ $ this ->assertSame (
211+ $ expected [$ itemNr ],
212+ $ result ,
213+ 'Failed: actual key ' . \var_export ($ result , true ) . ' is not the same as the expected key '
214+ . \var_export ($ expected [$ itemNr ], true ) . ' for item number ' . $ itemNr
215+ );
216+ }
217+
218+ // Verify against handling by PHP itself.
219+ $ expectedKeys = array_values ($ expected );
220+ $ actualKeys = array_keys (array_combine ($ expected , $ expected ));
221+ $ this ->assertSame ($ expectedKeys , $ actualKeys , 'getActualArrayKey() results do not match PHP native handling ' );
176222 }
177223}
0 commit comments