Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit c125cc0

Browse files
committed
Closing parenthesis should be after array closing bracket "])"
1 parent a888543 commit c125cc0

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CodeIgniter4/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
403403
// If this is the first argument in a function ensure the bracket to be right after the parenthesis. eg "array_combine([".
404404
if ($tokens[$prevNonWhitespaceToken]['code'] === T_OPEN_PARENTHESIS && $tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
405405
if ($tokens[$stackPtr]['line'] > $tokens[$prevNonWhitespaceToken]['line']) {
406-
$error = 'Array open bracket should be after function open parenthesis "(["';
406+
$error = 'Array openening bracket should be after function open parenthesis "(["';
407407
$data = array();
408408
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ShortArrayOpenWrongLine', $data);
409409
if ($fix === true) {
@@ -417,11 +417,31 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
417417
}
418418
}
419419

420-
// Check the closing bracket is on a new line.
420+
// Get content before closing array bracket/brace.
421421
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), $arrayStart, true);
422+
423+
// Check for ) after last Array end.
424+
$afterCloser = $phpcsFile->findNext(T_WHITESPACE, ($arrayEnd + 1), null, true);
425+
if ($tokens[$afterCloser]['code'] === T_CLOSE_PARENTHESIS) {
426+
if ($tokens[$afterCloser]['column'] !== ($tokens[$arrayEnd]['column'] + 1)) {
427+
$error = 'Closing parenthesis should be after array closing bracket "])"';
428+
$data = array();
429+
$fix = $phpcsFile->addFixableError($error, $afterCloser, 'CloseBracketAfterArrayBracket');
430+
if ($fix === true) {
431+
$phpcsFile->fixer->beginChangeset();
432+
for ($i = ($arrayEnd + 1); $i < $afterCloser; $i++) {
433+
$phpcsFile->fixer->replaceToken($i, '');
434+
}
435+
436+
$phpcsFile->fixer->endChangeset();
437+
}
438+
}
439+
}
440+
441+
// Check the closing bracket is on a new line.
422442
if ($tokens[$lastContent]['line'] === $tokens[$arrayEnd]['line']) {
423443
$error = 'Closing parenthesis of array declaration must be on a new line';
424-
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNewLine');
444+
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseArrayBraceNewLine');
425445
if ($fix === true) {
426446
$phpcsFile->fixer->addNewlineBefore($arrayEnd);
427447
}
@@ -442,7 +462,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
442462
($found / $this->tabWidth),
443463
);
444464

445-
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotAligned', $data);
465+
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseArrayBraceNotAligned', $data);
446466
if ($fix === true) {
447467
if ($found === 0) {
448468
$phpcsFile->fixer->addContent(($arrayEnd - 1), str_repeat(' ', $expected));

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.inc.fixed

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ class MyClass {
182182
$message->setCustomProperty('user_info', [
183183
'id' => $id,
184184
'name' => 'Test message',
185-
]
186-
);
185+
]);
187186

188187
$users = [
189188
[

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function getErrorList()
9696
117 => 1,
9797
118 => 1,
9898
119 => 1,
99+
120 => 1,
99100
);
100101

101102
}//end getErrorList()

0 commit comments

Comments
 (0)