forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Generic disallow short/long array syntax sniffs (request squizl…
- Loading branch information
Showing
9 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
CodeSniffer/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Bans the use of the PHP long array syntax. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Bans the use of the PHP long array syntax. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Sniffs_Arrays_DisallowLongArraySyntaxSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Registers the tokens that this sniff wants to listen for. | ||
* | ||
* @return int[] | ||
*/ | ||
public function register() | ||
{ | ||
return array(T_ARRAY); | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no'); | ||
|
||
$error = 'Short array syntax must be used to define arrays'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found'); | ||
|
||
if ($fix === true) { | ||
$tokens = $phpcsFile->getTokens(); | ||
$opener = $tokens[$stackPtr]['parenthesis_opener']; | ||
$closer = $tokens[$stackPtr]['parenthesis_closer']; | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
|
||
if ($opener === null) { | ||
$phpcsFile->fixer->replaceToken($stackPtr, '[]'); | ||
} else { | ||
$phpcsFile->fixer->replaceToken($stackPtr, ''); | ||
$phpcsFile->fixer->replaceToken($opener, '['); | ||
$phpcsFile->fixer->replaceToken($closer, ']'); | ||
} | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
72 changes: 72 additions & 0 deletions
72
CodeSniffer/Standards/Generic/Sniffs/Arrays/DisallowShortArraySyntaxSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Bans the use of the PHP short array syntax. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Bans the use of the PHP short array syntax. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Sniffs_Arrays_DisallowShortArraySyntaxSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Registers the tokens that this sniff wants to listen for. | ||
* | ||
* @return int[] | ||
*/ | ||
public function register() | ||
{ | ||
return array(T_OPEN_SHORT_ARRAY); | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes'); | ||
|
||
$error = 'Short array syntax is not allowed'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found'); | ||
|
||
if ($fix === true) { | ||
$tokens = $phpcsFile->getTokens(); | ||
$opener = $tokens[$stackPtr]['bracket_opener']; | ||
$closer = $tokens[$stackPtr]['bracket_closer']; | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->replaceToken($opener, 'array('); | ||
$phpcsFile->fixer->replaceToken($closer, ')'); | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
15 changes: 15 additions & 0 deletions
15
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
$var = array(); | ||
$var = [1,2,3]; | ||
$var = array(1,2,3); | ||
echo $var[1]; | ||
$foo = array($var[1],$var[2]); | ||
$foo = array( | ||
1 | ||
2 | ||
3 | ||
); | ||
$var = array/*comment*/(1,2,3); | ||
$var = array; | ||
|
||
function foo(array $array) {} |
15 changes: 15 additions & 0 deletions
15
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
$var = []; | ||
$var = [1,2,3]; | ||
$var = [1,2,3]; | ||
echo $var[1]; | ||
$foo = [$var[1],$var[2]]; | ||
$foo = [ | ||
1 | ||
2 | ||
3 | ||
]; | ||
$var = /*comment*/[1,2,3]; | ||
$var = []; | ||
|
||
function foo(array $array) {} |
70 changes: 70 additions & 0 deletions
70
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Unit test class for the DisallowLongArraySyntax sniff. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Unit test class for the DisallowLongArraySyntax sniff. | ||
* | ||
* A sniff unit test checks a .inc file for expected violations of a single | ||
* coding standard. Expected errors and warnings are stored in this class. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Tests_Arrays_DisallowLongArraySyntaxUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return array( | ||
2 => 1, | ||
4 => 1, | ||
6 => 1, | ||
7 => 1, | ||
12 => 1, | ||
13 => 1, | ||
); | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return array(); | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
12 changes: 12 additions & 0 deletions
12
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowShortArraySyntaxUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
$var = array(); | ||
$var = []; | ||
$var = array(1,2,3); | ||
$var = [1,2,3]; | ||
echo $var[1]; | ||
$foo = [$var[1],$var[2]]; | ||
$foo = [ | ||
1 | ||
2 | ||
3 | ||
]; |
12 changes: 12 additions & 0 deletions
12
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowShortArraySyntaxUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
$var = array(); | ||
$var = array(); | ||
$var = array(1,2,3); | ||
$var = array(1,2,3); | ||
echo $var[1]; | ||
$foo = array($var[1],$var[2]); | ||
$foo = array( | ||
1 | ||
2 | ||
3 | ||
); |
68 changes: 68 additions & 0 deletions
68
CodeSniffer/Standards/Generic/Tests/Arrays/DisallowShortArraySyntaxUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Unit test class for the DisallowShortArraySyntax sniff. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Unit test class for the DisallowShortArraySyntax sniff. | ||
* | ||
* A sniff unit test checks a .inc file for expected violations of a single | ||
* coding standard. Expected errors and warnings are stored in this class. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class Generic_Tests_Arrays_DisallowShortArraySyntaxUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return array( | ||
3 => 1, | ||
5 => 1, | ||
7 => 1, | ||
8 => 1, | ||
); | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return array(); | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters