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

Commit 0a8f398

Browse files
committed
Merge branch 'feature/unittests' into develop
2 parents a888543 + dda9084 commit 0a8f398

9 files changed

+107
-11
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()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
if ($a AND $b === $c) {} // Fail
4+
if ($a === $b AND $c) {} // Fail
5+
if ($a === $b AND $a === $c) {} // Fail
6+
if ($a === $b && $a === $c) {} // Fail
7+
if ($a === 1 and $a === 2 AND $a === 3) {} // Fail 2 errors
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
if ($a && $b === $c) {} // Fail
4+
if ($a === $b && $c) {} // Fail
5+
if ($a === $b && $a === $c) {} // Fail
6+
if ($a === $b && $a === $c) {} // Fail
7+
if ($a === 1 && $a === 2 && $a === 3) {} // Fail 2 errors
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Boolean And Unit Test
4+
*
5+
* @package CodeIgniter4-Standard
6+
* @author Louis Linehan <[email protected]>
7+
* @copyright 2017 Louis Linehan
8+
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
9+
*/
10+
11+
namespace CodeIgniter4\Tests\Operators;
12+
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
14+
15+
class BooleanAndUnitTest extends AbstractSniffUnitTest
16+
{
17+
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* The key of the array should represent the line number and the value
23+
* should represent the number of errors that should occur on that line.
24+
*
25+
* @return array<int, int>
26+
*/
27+
public function getErrorList()
28+
{
29+
return array(
30+
3 => 1,
31+
4 => 1,
32+
5 => 1,
33+
7 => 2,
34+
);
35+
36+
}//end getErrorList()
37+
38+
39+
/**
40+
* Returns the lines where warnings should occur.
41+
*
42+
* The key of the array should represent the line number and the value
43+
* should represent the number of warnings that should occur on that line.
44+
*
45+
* @return array<int, int>
46+
*/
47+
public function getWarningList()
48+
{
49+
return array();
50+
51+
}//end getWarningList()
52+
53+
54+
}//end class
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3-
if ($a === $b or $a === $c) {} // Fail
4-
if ($a === $b || $a === $c) {} // Pass
5-
if ($a === 1 or $a === 2 or $a === 3) {} // Fail 2 errors
3+
if ($a OR $b === $c) {} // Fail
4+
if ($a === $b OR $c) {} // Fail
5+
if ($a === $b OR $a === $c) {} // Fail
6+
if ($a === 1 or $a === 2 OR $a === 3) {} // Fail 2 errors
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if ($a || $b === $c) {} // Fail
4+
if ($a === $b || $c) {} // Fail
5+
if ($a === $b || $a === $c) {} // Fail
6+
if ($a === 1 || $a === 2 || $a === 3) {} // Fail 2 errors

CodeIgniter4/Tests/Operators/BooleanOrUnitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public function getErrorList()
2828
{
2929
return array(
3030
3 => 1,
31-
4 => 0,
32-
5 => 2,
31+
4 => 1,
32+
5 => 1,
33+
6 => 2,
3334
);
3435

3536
}//end getErrorList()

0 commit comments

Comments
 (0)