Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Yii2/Sniffs/PHP/DisallowLongArraySyntaxSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This sniff prohibits the use of the old array() syntax.
*/

/**
* This sniff prohibits the use of the old array() syntax.
*
* For example:
*
* <code>
* $array = array(
* "foo" => "bar",
* "bar" => "foo",
* );
* </code>
*
* The short syntax should be used instead:
*
* <code>
* $array = [
* "foo" => "bar",
* "bar" => "foo",
* ];
* </code>
*
*/

class Yii2_Sniffs_PHP_DisallowLongArraySyntaxSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns the token types that this sniff is interested in.
*
* @return array(int)
*/
public function register()
{
return array(T_ARRAY);
}//end register()

/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] === 'array') {
$error = 'Expected [], found %s';
$data = array(trim($tokens[$stackPtr]['content']));
$phpcsFile->addError($error, $stackPtr, '', $data);
}

}//end process()

}
4 changes: 2 additions & 2 deletions Yii2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
<!-- TODO: -->

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

<exclude-pattern>*/i18n/data/*</exclude-pattern>
<exclude-pattern>*/views/errorHandler/*</exclude-pattern>
<exclude-pattern>*/requirements/views/*</exclude-pattern>
<exclude-pattern>*/requirements/*</exclude-pattern>

<exclude-pattern>YiiRequirementChecker.php</exclude-pattern>
<exclude-pattern>ProfileTarget.php</exclude-pattern>
</ruleset>