Skip to content

Commit aee74bb

Browse files
authored
Merge pull request #621 from PHPCSStandards/feature/abstractarraydeclaration-getactualarraykeys-extra-test
AbstractArrayDeclaration::GetActualArrayKey(): add extra test
2 parents 8a1f3f5 + 3179443 commit aee74bb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Tests/AbstractSniffs/AbstractArrayDeclaration/GetActualArrayKeyTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,13 @@ $validStringKeys = array(
146146
'_5_' => 'value5',
147147
'_6_' => 'value6',
148148
);
149+
150+
/* testZeroPrefixedNumericStringKeys */
151+
$validNumericStringKeys = array(
152+
'01' => 'value1',
153+
'002' => 'value2',
154+
'0o3' => 'value3',
155+
'0b1' => 'value4',
156+
'0x7' => 'value5',
157+
'0.0' => 'value6',
158+
);

Tests/AbstractSniffs/AbstractArrayDeclaration/GetActualArrayKeyTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)