Skip to content

Commit 5501cbe

Browse files
committed
AbstractArrayDeclarationSniff::getActualArrayKey(): add extra tests
1 parent eaa0cf7 commit 5501cbe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Tests/AbstractSniffs/AbstractArrayDeclaration/GetActualArrayKeyTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,13 @@ $\{abc}
136136
EOD
137137
=> 3,
138138
];
139+
140+
/* testStringLiteralsWithNumbers */
141+
$validStringKeys = array(
142+
'_1' => 'value1',
143+
'_2' => 'value2',
144+
'3_' => 'value3',
145+
'4_' => 'value4',
146+
'_5_' => 'value5',
147+
'_6_' => 'value6',
148+
);

Tests/AbstractSniffs/AbstractArrayDeclaration/GetActualArrayKeyTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,40 @@ public function testGetActualArrayKeyFromHeredocWithEscapedVarInKey()
138138
);
139139
}
140140
}
141+
142+
/**
143+
* Test retrieving the actual array key when string keys look like numeric literals with underscores.
144+
*
145+
* @return void
146+
*/
147+
public function testStringLiteralsWithNumbers()
148+
{
149+
$testObj = new ArrayDeclarationSniffTestDouble();
150+
$testObj->tokens = self::$phpcsFile->getTokens();
151+
152+
$stackPtr = $this->getTargetToken('/* testStringLiteralsWithNumbers */', [\T_ARRAY, \T_OPEN_SHORT_ARRAY]);
153+
$arrayItems = PassedParameters::getParameters(self::$phpcsFile, $stackPtr);
154+
155+
$expected = [
156+
1 => '_1',
157+
2 => '_2',
158+
3 => '3_',
159+
4 => '4_',
160+
5 => '_5_',
161+
6 => '_6_',
162+
];
163+
164+
$this->assertCount(\count($expected), $arrayItems);
165+
166+
foreach ($arrayItems as $itemNr => $arrayItem) {
167+
$arrowPtr = Arrays::getDoubleArrowPtr(self::$phpcsFile, $arrayItem['start'], $arrayItem['end']);
168+
$result = $testObj->getActualArrayKey(self::$phpcsFile, $arrayItem['start'], ($arrowPtr - 1));
169+
$this->assertSame(
170+
$expected[$itemNr],
171+
$result,
172+
'Failed: actual key ' . \var_export($result, true) . ' is not the same as the expected key '
173+
. \var_export($expected[$itemNr], true) . ' for item number ' . $itemNr
174+
);
175+
}
176+
}
141177
}

0 commit comments

Comments
 (0)