Skip to content

Commit 2b884f9

Browse files
committed
Merge pull request #13 from nkovacs/long_array_syntax
disallow old array() syntax
2 parents 8f0e288 + cf89309 commit 2b884f9

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
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>
34-
<exclude-pattern>*/requirements/views/*</exclude-pattern>
35+
<exclude-pattern>*/requirements/*</exclude-pattern>
3536

36-
<exclude-pattern>YiiRequirementChecker.php</exclude-pattern>
3737
<exclude-pattern>ProfileTarget.php</exclude-pattern>
3838
</ruleset>

0 commit comments

Comments
 (0)