Skip to content

Commit 722f1c8

Browse files
committed
added fixer for Use Array Short Tag sniff
1 parent ecece3d commit 722f1c8

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Sniffs/Formatting/UseArrayShortTagSniff.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,21 @@ public function register()
5252
*/
5353
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5454
{
55-
$phpcsFile->addError('Array short tag [ ... ] must be used', $stackPtr);
55+
$fix = $phpcsFile->addFixableError('Array short tag [ ... ] must be used', $stackPtr);
56+
57+
if ($fix) {
58+
$tokens = $phpcsFile->getTokens();
59+
$token = $tokens[$stackPtr];
60+
61+
$phpcsFile->fixer->beginChangeset();
62+
$phpcsFile->fixer->replaceToken($stackPtr, '');
63+
$phpcsFile->fixer->replaceToken($token['parenthesis_opener'], '[');
64+
for ($i = $stackPtr + 1; $i < $token['parenthesis_opener']; $i++) {
65+
$phpcsFile->fixer->replaceToken($i, '');
66+
}
67+
$phpcsFile->fixer->replaceToken($token['parenthesis_closer'], ']');
68+
$phpcsFile->fixer->endChangeset();
69+
}
5670
}
5771
}
5872

Tests/Formatting/UseArrayShortTagUnitTest.fail.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ $b = array(
88
$b = array(
99
$a,
1010
array(4, 5, 6),
11-
);
11+
);
12+
13+
array
14+
(
15+
array/* will be deleted */( ),
16+
array (1), array (3)
17+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
$a = [1, 2, 3];
4+
$b = [
5+
$a,
6+
[4, 5, 6],
7+
];
8+
$b = [
9+
$a,
10+
[4, 5, 6],
11+
];
12+
13+
[
14+
[ ],
15+
[1], [3]
16+
];

Tests/Formatting/UseArrayShortTagUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ protected function getErrorList($testFile = '')
4949
4 => 1,
5050
8 => 1,
5151
10 => 1,
52+
13 => 1,
53+
15 => 1,
54+
16 => 2,
5255
);
5356
}
5457

0 commit comments

Comments
 (0)