Skip to content

Commit 32351dd

Browse files
committed
Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align calculation in array with cyrillic keys
1 parent b0ee3ee commit 32351dd

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5555
- Fixed bug #2138 : Tokenizer detects wrong token for php ::class feature with spaces
5656
- Fixed bug #2143 : PSR2.Namespaces.UseDeclaration does not properly fix "use function" and "use const" statements
5757
-- Thanks to Chris Wilkinson for the patch
58+
- Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align calculation in array with cyrillic keys
5859
</notes>
5960
<contents>
6061
<dir name="/">

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,19 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
485485
if ($indexStart === $indexEnd) {
486486
$currentEntry['index'] = $indexEnd;
487487
$currentEntry['index_content'] = $tokens[$indexEnd]['content'];
488+
$currentEntry['index_length'] = $tokens[$indexEnd]['length'];
488489
} else {
489490
$currentEntry['index'] = $indexStart;
490-
$currentEntry['index_content'] = $phpcsFile->getTokensAsString($indexStart, ($indexEnd - $indexStart + 1));
491+
$currentEntry['index_content'] = '';
492+
$currentEntry['index_length'] = 0;
493+
for ($i = $indexStart; $i <= $indexEnd; $i++) {
494+
$currentEntry['index_content'] .= $tokens[$i]['content'];
495+
$currentEntry['index_length'] += $tokens[$i]['length'];
496+
}
491497
}
492498

493-
$indexLength = strlen($currentEntry['index_content']);
494-
if ($maxLength < $indexLength) {
495-
$maxLength = $indexLength;
499+
if ($maxLength < $currentEntry['index_length']) {
500+
$maxLength = $currentEntry['index_length'];
496501
}
497502

498503
// Find the value of this index.
@@ -739,8 +744,8 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
739744

740745
$arrowStart = ($tokens[$index['index']]['column'] + $maxLength + 1);
741746
if ($tokens[$index['arrow']]['column'] !== $arrowStart) {
742-
$expected = ($arrowStart - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
743-
$found = ($tokens[$index['arrow']]['column'] - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
747+
$expected = ($arrowStart - ($index['index_length'] + $tokens[$index['index']]['column']));
748+
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$index['index']]['column']));
744749
$error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s';
745750
$data = [
746751
$expected,

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,13 @@ HERE
393393
HERE
394394
,
395395
);
396+
397+
$foo = array(
398+
'тип' => 'авто',
399+
'цвет' => 'синий',
400+
);
401+
402+
$paths = array(
403+
Init::ROOT_DIR.'/тип' => 'авто',
404+
Init::ROOT_DIR.'/цвет' => 'синий',
405+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,13 @@ HERE
421421
HERE
422422
,
423423
);
424+
425+
$foo = array(
426+
'тип' => 'авто',
427+
'цвет' => 'синий',
428+
);
429+
430+
$paths = array(
431+
Init::ROOT_DIR.'/тип' => 'авто',
432+
Init::ROOT_DIR.'/цвет' => 'синий',
433+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,13 @@ HERE
382382
HERE
383383
,
384384
];
385+
386+
$foo = [
387+
'тип' => 'авто',
388+
'цвет' => 'синий',
389+
];
390+
391+
$paths = [
392+
Init::ROOT_DIR.'/тип' => 'авто',
393+
Init::ROOT_DIR.'/цвет' => 'синий',
394+
];

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,13 @@ HERE
408408
HERE
409409
,
410410
];
411+
412+
$foo = [
413+
'тип' => 'авто',
414+
'цвет' => 'синий',
415+
];
416+
417+
$paths = [
418+
Init::ROOT_DIR.'/тип' => 'авто',
419+
Init::ROOT_DIR.'/цвет' => 'синий',
420+
];

0 commit comments

Comments
 (0)