Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,29 @@ public function process(File $phpcsFile, $stackPtr): void
$line = $current['line'];

if ($lastLine === $line) {
$previousComma = $this->getPreviousComma($phpcsFile, $i, $start);

$msg = 'only one "=>" assignments per line is allowed in a multi line array';
$phpcsFile->addError($msg, $i, 'OneAssignmentPerLine');

if ($previousComma !== false) {
$fixable = $phpcsFile->addFixableError($msg, $i, 'OneAssignmentPerLine');

if ($fixable === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline((int) $previousComma);
$phpcsFile->fixer->endChangeset();
}
} else {
// Remove current and previous '=>' from array for further processing.
array_pop($assignments);
array_pop($assignments);
$phpcsFile->addError($msg, $i, 'OneAssignmentPerLine');
}
}

$hasKeyInLine = false;

$j = ($i - 1);
$j = ((int) $i - 1);
while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) {
if (\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true) === false) {
$hasKeyInLine = true;
Expand All @@ -122,11 +138,17 @@ public function process(File $phpcsFile, $stackPtr): void
}

if ($hasKeyInLine === false) {
$phpcsFile->addError(
$fixable = $phpcsFile->addFixableError(
'in arrays, keys and "=>" must be on the same line',
$i,
'KeyAndValueNotOnSameLine'
);

if ($fixable === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($j, '');
$phpcsFile->fixer->endChangeset();
}
}

if ($column > $keyEndColumn) {
Expand All @@ -137,16 +159,68 @@ public function process(File $phpcsFile, $stackPtr): void
}//end if
}//end for

$doubleArrowStartColumn = ($keyEndColumn + 1);

foreach ($assignments as $ptr) {
$current = $tokens[$ptr];
$column = $current['column'];

if ($column !== ($keyEndColumn + 1)) {
$phpcsFile->addError('each "=>" assignments must be aligned', (int) $ptr, 'AssignmentsNotAligned');
$beforeArrowPtr = ((int) $ptr - 1);
$currentIndent = \strlen($tokens[$beforeArrowPtr]['content']);
$correctIndent = (int) ($currentIndent - $column + $doubleArrowStartColumn);
if ($column !== $doubleArrowStartColumn) {
$fixable = $phpcsFile->addFixableError("each \"=>\" assignments must be aligned; current indentation before \"=>\" are $currentIndent space(s), must be $correctIndent space(s)", (int) $ptr, 'AssignmentsNotAligned');

if ($fixable === false) {
continue;
}

$phpcsFile->fixer->beginChangeset();
if ($tokens[$beforeArrowPtr]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($beforeArrowPtr, str_repeat(' ', $correctIndent));
} else {
$phpcsFile->fixer->addContent($beforeArrowPtr, str_repeat(' ', $correctIndent));
}

$phpcsFile->fixer->endChangeset();
}
}
}//end foreach

}//end process()


/**
* Find previous comma in array.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
* @param int $start Start of the array
*
* @return bool|int
*/
private function getPreviousComma(File $phpcsFile, $stackPtr, $start)
{
$previousComma = false;
$tokens = $phpcsFile->getTokens();

$ptr = $phpcsFile->findPrevious([T_COMMA, T_CLOSE_SHORT_ARRAY], $stackPtr, $start);
while ($ptr !== false) {
if ($tokens[$ptr]['code'] === T_COMMA) {
$previousComma = $ptr;
break;
}

if ($tokens[$ptr]['code'] === T_CLOSE_SHORT_ARRAY) {
$ptr = $tokens[$ptr]['bracket_opener'];
}

$ptr = $phpcsFile->findPrevious([T_COMMA, T_CLOSE_SHORT_ARRAY], ($ptr - 1), $start);
}

return $previousComma;

}//end getPreviousComma()


}//end class
13 changes: 13 additions & 0 deletions MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.fail.inc
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ $a = [
98765432
=> 'fail',
];

$a=[
'v'=>1,
'vv'=>2,
];
// deal with syntax errors
$aa = [
'y' => [1, 2, 3] 'x' => 0 'z' => -1
];

$a=[
'v'=>1 'vv'=>2
];
75 changes: 44 additions & 31 deletions MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.fail.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@


$a = [
'one' => 1,
'one' => 1,
'four' => 4,
];

$a = [
'one' => 1,
'one' => 1,
2,
'four' => 4,
];

// bad
$a = [
'one' => 1, 'two' => 2,
'three' => 1, 'four' => 4,
'one' => 1,
'two' => 2,
'three' => 1,
'four' => 4,
];

$a = [
'one' => 1,
'one' => 1,
2,
'four' /* comment */ => 4,
];

$a = [
'one' => 1,
'one' => 1,
2,
// comment
'four' => 4,
Expand All @@ -35,42 +37,42 @@ $a = [
2,
'four' => 4,
// comment
'one' => /* comment */
'one' => /* comment */
1,
];

$a = array(
'short' => 1,
'short' => 1,
'verylongerkey' => 2,
'x' => 3,
'x' => 3,
);

$a = [
'short' => 1,
'short' => 1,
'verylongerkey' => 2,
'x' => 3,
'x' => 3,
];

$a = array(
'short' => 1,
'verylongerkey' => 2,
'array' => array(
'val1' => 1,
'val112' => 1,
'val1' => 1,
'val112' => 1,
'val11234567' => 1,
'v' => 1,
'v' => 1,
),
'x' => 3,
);

$a = [
'short' => 1,
'short' => 1,
'verylongerkey' => 2,
'array' => [
'val1' => 1,
'val112' => 1,
'val1' => 1,
'val112' => 1,
'val1123456' => 1,
'v' => 1,
'v' => 1,
],
'x' => 3,
];
Expand All @@ -79,30 +81,30 @@ $a = [
'short' => 1,
'verylongerkey' => 2,
'array' => array(
'val1' => 1,
'val112' => 1,
'val1' => 1,
'val112' => 1,
'val11234567' => 1,
'v' => 1,
'v' => 1,
),
'x' => 3,
];

$a = array(
'short' => 1,
'verylongerkey' => 2,
'array' => [
'val1' => 1,
'array' => [
'val1' => 1,
'val112' => 1,
'val1123' => 1,
'v' => 1,
'v' => 1,
],
'x' => 3,
);

$a = [
0 => [ 1 => 'O', 0 => 'O'],
1 => [ 1 => 'O', 0 => 'I'],
2 => [ 1 => 'I', 0 => 'O'],
2 => [ 1 => 'I', 0 => 'O'],
3 => [ 1 => 'I', 0 => 'I'],
];

Expand All @@ -127,11 +129,22 @@ $a = [
},
function () {
},
'one' => 1,
'one' => 1,
2,
'four' => 2,
123456789
/* bla */ => 'fail',
98765432
=> 'fail',
'four' => 2,
123456789 /* bla */ => 'fail',
98765432 => 'fail',
];

$a=[
'v' =>1,
'vv' =>2,
];
// deal with syntax errors
$aa = [
'y' => [1, 2, 3] 'x' => 0 'z' => -1
];

$a=[
'v'=>1 'vv'=>2
];
4 changes: 4 additions & 0 deletions MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ protected function getErrorList(string $testFile=''): array
132 => 1,
134 => 1,
136 => 2,
140 => 1,
141 => 1,
145 => 2,
149 => 1,
];
}//end switch

Expand Down