Skip to content

Commit 3301611

Browse files
committed
disallow old array() syntax
1 parent 8f0e288 commit 3301611

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* This sniff prohibits the use of the old array() syntax.
5+
*/
6+
7+
/**
8+
* This sniff prohibits the use of the old array() syntax.
9+
*
10+
* For example:
11+
*
12+
* <code>
13+
* $array = array(
14+
* "foo" => "bar",
15+
* "bar" => "foo",
16+
* );
17+
* </code>
18+
*
19+
* The short syntax should be used instead:
20+
*
21+
* <code>
22+
* $array = [
23+
* "foo" => "bar",
24+
* "bar" => "foo",
25+
* ];
26+
* </code>
27+
*
28+
*/
29+
30+
class Yii2_Sniffs_PHP_DisallowLongArraySyntaxSniff implements PHP_CodeSniffer_Sniff
31+
{
32+
/**
33+
* Returns the token types that this sniff is interested in.
34+
*
35+
* @return array(int)
36+
*/
37+
public function register()
38+
{
39+
return array(T_ARRAY);
40+
}//end register()
41+
42+
/**
43+
* Processes the tokens that this sniff is interested in.
44+
*
45+
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
46+
* @param int $stackPtr The position in the stack where
47+
* the token was found.
48+
*
49+
* @return void
50+
*/
51+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52+
{
53+
$tokens = $phpcsFile->getTokens();
54+
if ($tokens[$stackPtr]['content'] === 'array') {
55+
$error = 'Expected [], found %s';
56+
$data = array(trim($tokens[$stackPtr]['content']));
57+
$phpcsFile->addError($error, $stackPtr, '', $data);
58+
}
59+
60+
}//end process()
61+
62+
}

Yii2/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<!-- TODO: -->
2929

3030
<!-- ... other Yii2 specific rules. -->
31+
<rule ref="Yii2.PHP.DisallowLongArraySyntax"/>
3132

3233
<exclude-pattern>*/i18n/data/*</exclude-pattern>
3334
<exclude-pattern>*/views/errorHandler/*</exclude-pattern>

0 commit comments

Comments
 (0)