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

Commit 4ae78d0

Browse files
committed
Merge branch 'release/1.0.0-beta0011'
2 parents 9f2ba55 + ce6e0f9 commit 4ae78d0

File tree

8 files changed

+91
-20
lines changed

8 files changed

+91
-20
lines changed

CodeIgniter4/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ public function process(File $phpcsFile, $stackPtr)
8585

8686
$tokens = $phpcsFile->getTokens();
8787

88+
// First make sure all arrays use short array syntax, this makes fixing much easier.
89+
if ($tokens[$stackPtr]['code'] === T_ARRAY) {
90+
$error = 'Short array syntax must be used to define arrays';
91+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'FoundLongArray');
92+
93+
if ($fix === true) {
94+
$tokens = $phpcsFile->getTokens();
95+
$opener = $tokens[$stackPtr]['parenthesis_opener'];
96+
$closer = $tokens[$stackPtr]['parenthesis_closer'];
97+
98+
$phpcsFile->fixer->beginChangeset();
99+
100+
if ($opener === null) {
101+
$phpcsFile->fixer->replaceToken($stackPtr, '[]');
102+
} else {
103+
$phpcsFile->fixer->replaceToken($stackPtr, '');
104+
$phpcsFile->fixer->replaceToken($opener, '[');
105+
$phpcsFile->fixer->replaceToken($closer, ']');
106+
}
107+
108+
$phpcsFile->fixer->endChangeset();
109+
}
110+
}//end if
111+
88112
if ($tokens[$stackPtr]['code'] === T_ARRAY) {
89113
$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
90114
if (isset($tokens[$arrayStart]['parenthesis_closer']) === false) {
@@ -328,6 +352,8 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
328352
T_OBJECT_CAST,
329353
T_STATIC,
330354
T_CONST,
355+
T_RETURN,
356+
T_OBJECT_OPERATOR,
331357
);
332358

333359
$firstOnLine = $phpcsFile->findFirstOnLine($starts, $prevNonWhitespaceToken);
@@ -357,6 +383,23 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
357383
$indentStart = $stackPtr;
358384
}//end switch
359385

386+
// If this is the first argument in a function ensure the bracket to be right after the parenthesis. eg "array_combine([".
387+
if ($tokens[$prevNonWhitespaceToken]['code'] === T_OPEN_PARENTHESIS && $tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
388+
if ($tokens[$stackPtr]['line'] > $tokens[$prevNonWhitespaceToken]['line']) {
389+
$error = 'Array open bracket should be after function open parenthesis "(["';
390+
$data = array();
391+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ShortArrayOpenWrongLine', $data);
392+
if ($fix === true) {
393+
$phpcsFile->fixer->beginChangeset();
394+
for ($i = ($prevNonWhitespaceToken + 1); $i < $stackPtr; $i++) {
395+
$phpcsFile->fixer->replaceToken($i, '');
396+
}
397+
398+
$phpcsFile->fixer->endChangeset();
399+
}
400+
}
401+
}
402+
360403
// Check the closing bracket is on a new line.
361404
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), $arrayStart, true);
362405
if ($tokens[$lastContent]['line'] === $tokens[$arrayEnd]['line']) {

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,17 @@ class MyClass {
9999
'perPage' => $this->pager->getPerPage(),
100100
'total' => $this->pager->getPerPage() * $this->pager->getPageCount(),
101101
] , 15);
102+
103+
return preg_replace(
104+
array(
105+
'#<img[\s/]+.*?src\s*=\s*(["\'])([^\\1]+?)\\1.*?\>#i',
106+
'#<img[\s/]+.*?src\s*=\s*?(([^\s"\'=<>`]+)).*?\>#i'
107+
), '\\2', $str
108+
);
109+
110+
$this->db->table($this->table)
111+
->insert([
112+
'version' => $version,
113+
'name' => $this->name,
114+
]);
102115
}

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.inc.fixed

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ class MyClass {
107107
],
108108
];
109109

110-
$a = array
110+
$a =
111111
// comment
112-
(
112+
[
113113
'a',
114114
'b',
115-
);
115+
];
116116

117-
$a = array /* comment */ (
118-
'a',
119-
'b',
120-
);
117+
$a = /* comment */ [
118+
'a',
119+
'b',
120+
];
121121

122122
protected static $statusCodes = [
123123
// 1xx: Informational
@@ -149,7 +149,7 @@ class MyClass {
149149
];
150150
}
151151

152-
public $arr = array();
152+
public $arr = [];
153153

154154
public $arr = [];
155155

@@ -166,4 +166,16 @@ class MyClass {
166166
'perPage' => $this->pager->getPerPage(),
167167
'total' => $this->pager->getPerPage() * $this->pager->getPageCount(),
168168
] , 15);
169+
170+
return preg_replace([
171+
'#<img[\s/]+.*?src\s*=\s*(["\'])([^\\1]+?)\\1.*?\>#i',
172+
'#<img[\s/]+.*?src\s*=\s*?(([^\s"\'=<>`]+)).*?\>#i',
173+
], '\\2', $str
174+
);
175+
176+
$this->db->table($this->table)
177+
->insert([
178+
'version' => $version,
179+
'name' => $this->name,
180+
]);
169181
}

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public function getErrorList()
6565
47 => 2,
6666
48 => 2,
6767
49 => 2,
68-
52 => 1,
69-
56 => 1,
68+
52 => 2,
69+
56 => 2,
7070
60 => 1,
7171
61 => 1,
7272
62 => 1,
@@ -77,7 +77,7 @@ public function getErrorList()
7777
80 => 1,
7878
81 => 1,
7979
82 => 1,
80-
85 => 1,
80+
85 => 2,
8181
87 => 1,
8282
89 => 1,
8383
92 => 1,
@@ -86,6 +86,12 @@ public function getErrorList()
8686
99 => 1,
8787
100 => 1,
8888
101 => 1,
89+
104 => 1,
90+
105 => 1,
91+
106 => 1,
92+
107 => 1,
93+
112 => 1,
94+
113 => 1,
8995
);
9096

9197
}//end getErrorList()

CodeIgniter4/ruleset.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Louis Linehan <[email protected]>
88
* @copyright 2017 Louis Linehan
99
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
10-
* @version Version 1.0.0-beta0010
10+
* @version Version 1.0.0-beta0011
1111
-->
1212
<!--
1313
Files MUST have a doc block comment.
@@ -310,16 +310,13 @@
310310
-->
311311
<rule ref="CodeIgniter4.NamingConventions.ValidFunctionName"/>
312312
<!--
313-
Checks arrays are declared as "[]" not "array()".
314-
-->
315-
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
316-
<!--
317313
Checks array bracket spaces.
318314
-->
319315
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
320316
<!--
321317
Check array declaration, indentation, alignment and that the last item
322318
has a trailing comma.
319+
Checks arrays are declared as "[]" not "array()".
323320
-->
324321
<rule ref="CodeIgniter4.Arrays.ArrayDeclaration"/>
325322
<!--

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[CodeIgniter](https://codeigniter.com) 4 coding standard for use with [PHP_CodeSniffer 3](https://github.com/squizlabs/PHP_CodeSniffer).
44

5-
Version 1.0.0-beta0010
5+
Version 1.0.0-beta0011
66

77
| Master | Develop |
88
| :---: | :---: |
@@ -34,7 +34,7 @@ Set the `phpcs standard path` and `phpcbf standard path` in your editor/plugin c
3434

3535
### Download install
3636

37-
Download [CodeIgniter4-Standard](https://github.com/louisl/CodeIgniter4-Standard/archive/v1.0.0-beta0010.zip).
37+
Download [CodeIgniter4-Standard](https://github.com/louisl/CodeIgniter4-Standard/archive/v1.0.0-beta0011.zip).
3838

3939
Set `standard ` paths to your local filesystem:
4040

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "louisl/codeigniter4-standard",
33
"description": "CodeIgniter 4 Standard for PHP_CodeSniffer 3.",
4-
"version":"1.0.0-beta0010",
4+
"version":"1.0.0-beta0011",
55
"license":"MIT",
66
"authors": [
77
{

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)