Skip to content

Commit

Permalink
Added Generic disallow short/long array syntax sniffs (request squizl…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Mar 18, 2015
1 parent f2204b9 commit 081f49a
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 0 deletions.
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
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
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) {}
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) {}
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
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
];
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
);
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
24 changes: 24 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
- PHPCS can now exit with 0 even if errors are found
-- Set the ignore_errors_on_exit config variable to 1 to set this behaviour
-- Use with the ignore_warnings_on_exit config variable to never return a non-zero exit code
- Added Generic DisallowLongArraySyntaxSniff to enforce the use of the PHP short array syntax (request #483)
-- Thanks to Xaver Loppenstedt for helping with tests
- Added Generic DisallowShortArraySyntaxSniff to ban the use of the PHP short array syntax (request #483)
-- Thanks to Xaver Loppenstedt for helping with tests
- Squiz ConcatenationSpacingSniff now has a setting to ignore newline characters around operators (request #511)
-- Default remains FALSE, so newlines are not allowed
-- Override the "ignoreNewlines" setting in a ruleset.xml file to change
Expand Down Expand Up @@ -256,6 +260,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
</dir>
<dir name="Sniffs">
<dir name="Arrays">
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
</dir>
<dir name="Classes">
<file baseinstalldir="PHP" name="DuplicateClassNameSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
Expand Down Expand Up @@ -451,6 +463,18 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
</dir>
<dir name="Tests">
<dir name="Arrays">
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.inc" role="test" />
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP" name="DisallowLongArraySyntaxUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.inc" role="test" />
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP" name="DisallowShortArraySyntaxUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
</dir>
<dir name="Classes">
<file baseinstalldir="PHP" name="DuplicateClassNameUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP" name="DuplicateClassNameUnitTest.2.inc" role="test" />
Expand Down

0 comments on commit 081f49a

Please sign in to comment.