Skip to content

Commit

Permalink
Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align …
Browse files Browse the repository at this point in the history
…calculation in array with cyrillic keys
  • Loading branch information
gsherwood committed Aug 29, 2018
1 parent b0ee3ee commit 32351dd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2138 : Tokenizer detects wrong token for php ::class feature with spaces
- Fixed bug #2143 : PSR2.Namespaces.UseDeclaration does not properly fix "use function" and "use const" statements
-- Thanks to Chris Wilkinson for the patch
- Fixed bug #2144 : Squiz.Arrays.ArrayDeclaration does incorrect align calculation in array with cyrillic keys
</notes>
<contents>
<dir name="/">
Expand Down
17 changes: 11 additions & 6 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,19 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
if ($indexStart === $indexEnd) {
$currentEntry['index'] = $indexEnd;
$currentEntry['index_content'] = $tokens[$indexEnd]['content'];
$currentEntry['index_length'] = $tokens[$indexEnd]['length'];
} else {
$currentEntry['index'] = $indexStart;
$currentEntry['index_content'] = $phpcsFile->getTokensAsString($indexStart, ($indexEnd - $indexStart + 1));
$currentEntry['index_content'] = '';
$currentEntry['index_length'] = 0;
for ($i = $indexStart; $i <= $indexEnd; $i++) {
$currentEntry['index_content'] .= $tokens[$i]['content'];
$currentEntry['index_length'] += $tokens[$i]['length'];
}
}

$indexLength = strlen($currentEntry['index_content']);
if ($maxLength < $indexLength) {
$maxLength = $indexLength;
if ($maxLength < $currentEntry['index_length']) {
$maxLength = $currentEntry['index_length'];
}

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

$arrowStart = ($tokens[$index['index']]['column'] + $maxLength + 1);
if ($tokens[$index['arrow']]['column'] !== $arrowStart) {
$expected = ($arrowStart - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
$found = ($tokens[$index['arrow']]['column'] - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
$expected = ($arrowStart - ($index['index_length'] + $tokens[$index['index']]['column']));
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$index['index']]['column']));
$error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s';
$data = [
$expected,
Expand Down
10 changes: 10 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,13 @@ HERE
HERE
,
);

$foo = array(
'тип' => 'авто',
'цвет' => 'синий',
);

$paths = array(
Init::ROOT_DIR.'/тип' => 'авто',
Init::ROOT_DIR.'/цвет' => 'синий',
);
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,13 @@ HERE
HERE
,
);

$foo = array(
'тип' => 'авто',
'цвет' => 'синий',
);

$paths = array(
Init::ROOT_DIR.'/тип' => 'авто',
Init::ROOT_DIR.'/цвет' => 'синий',
);
10 changes: 10 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,13 @@ HERE
HERE
,
];

$foo = [
'тип' => 'авто',
'цвет' => 'синий',
];

$paths = [
Init::ROOT_DIR.'/тип' => 'авто',
Init::ROOT_DIR.'/цвет' => 'синий',
];
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,13 @@ HERE
HERE
,
];

$foo = [
'тип' => 'авто',
'цвет' => 'синий',
];

$paths = [
Init::ROOT_DIR.'/тип' => 'авто',
Init::ROOT_DIR.'/цвет' => 'синий',
];

0 comments on commit 32351dd

Please sign in to comment.