Skip to content

Commit 081f49a

Browse files
committed
Added Generic disallow short/long array syntax sniffs (request #483)
1 parent f2204b9 commit 081f49a

9 files changed

+367
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Bans the use of the PHP long array syntax.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Bans the use of the PHP long array syntax.
17+
*
18+
* @category PHP
19+
* @package PHP_CodeSniffer
20+
* @author Greg Sherwood <[email protected]>
21+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23+
* @version Release: @package_version@
24+
* @link http://pear.php.net/package/PHP_CodeSniffer
25+
*/
26+
class Generic_Sniffs_Arrays_DisallowLongArraySyntaxSniff implements PHP_CodeSniffer_Sniff
27+
{
28+
29+
30+
/**
31+
* Registers the tokens that this sniff wants to listen for.
32+
*
33+
* @return int[]
34+
*/
35+
public function register()
36+
{
37+
return array(T_ARRAY);
38+
39+
}//end register()
40+
41+
42+
/**
43+
* Processes this test, when one of its tokens is encountered.
44+
*
45+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46+
* @param int $stackPtr The position of the current token
47+
* in the stack passed in $tokens.
48+
*
49+
* @return void
50+
*/
51+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52+
{
53+
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
54+
55+
$error = 'Short array syntax must be used to define arrays';
56+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
57+
58+
if ($fix === true) {
59+
$tokens = $phpcsFile->getTokens();
60+
$opener = $tokens[$stackPtr]['parenthesis_opener'];
61+
$closer = $tokens[$stackPtr]['parenthesis_closer'];
62+
63+
$phpcsFile->fixer->beginChangeset();
64+
65+
if ($opener === null) {
66+
$phpcsFile->fixer->replaceToken($stackPtr, '[]');
67+
} else {
68+
$phpcsFile->fixer->replaceToken($stackPtr, '');
69+
$phpcsFile->fixer->replaceToken($opener, '[');
70+
$phpcsFile->fixer->replaceToken($closer, ']');
71+
}
72+
73+
$phpcsFile->fixer->endChangeset();
74+
}
75+
76+
}//end process()
77+
78+
79+
}//end class
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Bans the use of the PHP short array syntax.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Bans the use of the PHP short array syntax.
17+
*
18+
* @category PHP
19+
* @package PHP_CodeSniffer
20+
* @author Greg Sherwood <[email protected]>
21+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23+
* @version Release: @package_version@
24+
* @link http://pear.php.net/package/PHP_CodeSniffer
25+
*/
26+
class Generic_Sniffs_Arrays_DisallowShortArraySyntaxSniff implements PHP_CodeSniffer_Sniff
27+
{
28+
29+
30+
/**
31+
* Registers the tokens that this sniff wants to listen for.
32+
*
33+
* @return int[]
34+
*/
35+
public function register()
36+
{
37+
return array(T_OPEN_SHORT_ARRAY);
38+
39+
}//end register()
40+
41+
42+
/**
43+
* Processes this test, when one of its tokens is encountered.
44+
*
45+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46+
* @param int $stackPtr The position of the current token
47+
* in the stack passed in $tokens.
48+
*
49+
* @return void
50+
*/
51+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52+
{
53+
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
54+
55+
$error = 'Short array syntax is not allowed';
56+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
57+
58+
if ($fix === true) {
59+
$tokens = $phpcsFile->getTokens();
60+
$opener = $tokens[$stackPtr]['bracket_opener'];
61+
$closer = $tokens[$stackPtr]['bracket_closer'];
62+
63+
$phpcsFile->fixer->beginChangeset();
64+
$phpcsFile->fixer->replaceToken($opener, 'array(');
65+
$phpcsFile->fixer->replaceToken($closer, ')');
66+
$phpcsFile->fixer->endChangeset();
67+
}
68+
69+
}//end process()
70+
71+
72+
}//end class
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
$var = array();
3+
$var = [1,2,3];
4+
$var = array(1,2,3);
5+
echo $var[1];
6+
$foo = array($var[1],$var[2]);
7+
$foo = array(
8+
1
9+
2
10+
3
11+
);
12+
$var = array/*comment*/(1,2,3);
13+
$var = array;
14+
15+
function foo(array $array) {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
$var = [];
3+
$var = [1,2,3];
4+
$var = [1,2,3];
5+
echo $var[1];
6+
$foo = [$var[1],$var[2]];
7+
$foo = [
8+
1
9+
2
10+
3
11+
];
12+
$var = /*comment*/[1,2,3];
13+
$var = [];
14+
15+
function foo(array $array) {}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Unit test class for the DisallowLongArraySyntax sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the DisallowLongArraySyntax sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class Generic_Tests_Arrays_DisallowLongArraySyntaxUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @return array<int, int>
40+
*/
41+
public function getErrorList()
42+
{
43+
return array(
44+
2 => 1,
45+
4 => 1,
46+
6 => 1,
47+
7 => 1,
48+
12 => 1,
49+
13 => 1,
50+
);
51+
52+
}//end getErrorList()
53+
54+
55+
/**
56+
* Returns the lines where warnings should occur.
57+
*
58+
* The key of the array should represent the line number and the value
59+
* should represent the number of warnings that should occur on that line.
60+
*
61+
* @return array<int, int>
62+
*/
63+
public function getWarningList()
64+
{
65+
return array();
66+
67+
}//end getWarningList()
68+
69+
70+
}//end class
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
$var = array();
3+
$var = [];
4+
$var = array(1,2,3);
5+
$var = [1,2,3];
6+
echo $var[1];
7+
$foo = [$var[1],$var[2]];
8+
$foo = [
9+
1
10+
2
11+
3
12+
];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
$var = array();
3+
$var = array();
4+
$var = array(1,2,3);
5+
$var = array(1,2,3);
6+
echo $var[1];
7+
$foo = array($var[1],$var[2]);
8+
$foo = array(
9+
1
10+
2
11+
3
12+
);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Unit test class for the DisallowShortArraySyntax sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the DisallowShortArraySyntax sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class Generic_Tests_Arrays_DisallowShortArraySyntaxUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @return array<int, int>
40+
*/
41+
public function getErrorList()
42+
{
43+
return array(
44+
3 => 1,
45+
5 => 1,
46+
7 => 1,
47+
8 => 1,
48+
);
49+
50+
}//end getErrorList()
51+
52+
53+
/**
54+
* Returns the lines where warnings should occur.
55+
*
56+
* The key of the array should represent the line number and the value
57+
* should represent the number of warnings that should occur on that line.
58+
*
59+
* @return array<int, int>
60+
*/
61+
public function getWarningList()
62+
{
63+
return array();
64+
65+
}//end getWarningList()
66+
67+
68+
}//end class

package.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
2929
- PHPCS can now exit with 0 even if errors are found
3030
-- Set the ignore_errors_on_exit config variable to 1 to set this behaviour
3131
-- Use with the ignore_warnings_on_exit config variable to never return a non-zero exit code
32+
- Added Generic DisallowLongArraySyntaxSniff to enforce the use of the PHP short array syntax (request #483)
33+
-- Thanks to Xaver Loppenstedt for helping with tests
34+
- Added Generic DisallowShortArraySyntaxSniff to ban the use of the PHP short array syntax (request #483)
35+
-- Thanks to Xaver Loppenstedt for helping with tests
3236
- Squiz ConcatenationSpacingSniff now has a setting to ignore newline characters around operators (request #511)
3337
-- Default remains FALSE, so newlines are not allowed
3438
-- Override the "ignoreNewlines" setting in a ruleset.xml file to change
@@ -256,6 +260,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
256260
</dir>
257261
</dir>
258262
<dir name="Sniffs">
263+
<dir name="Arrays">
264+
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxSniff.php" role="php">
265+
<tasks:replace from="@package_version@" to="version" type="package-info" />
266+
</file>
267+
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxSniff.php" role="php">
268+
<tasks:replace from="@package_version@" to="version" type="package-info" />
269+
</file>
270+
</dir>
259271
<dir name="Classes">
260272
<file baseinstalldir="PHP" name="DuplicateClassNameSniff.php" role="php">
261273
<tasks:replace from="@package_version@" to="version" type="package-info" />
@@ -451,6 +463,18 @@ http://pear.php.net/dtd/package-2.0.xsd">
451463
</dir>
452464
</dir>
453465
<dir name="Tests">
466+
<dir name="Arrays">
467+
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.inc" role="test" />
468+
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.inc.fixed" role="test" />
469+
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.php" role="test">
470+
<tasks:replace from="@package_version@" to="version" type="package-info" />
471+
</file>
472+
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.inc" role="test" />
473+
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.inc.fixed" role="test" />
474+
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.php" role="test">
475+
<tasks:replace from="@package_version@" to="version" type="package-info" />
476+
</file>
477+
</dir>
454478
<dir name="Classes">
455479
<file baseinstalldir="PHP" name="DuplicateClassNameUnitTest.1.inc" role="test" />
456480
<file baseinstalldir="PHP" name="DuplicateClassNameUnitTest.2.inc" role="test" />

0 commit comments

Comments
 (0)