diff --git a/package.xml b/package.xml index aa4f705337..2fcd9181bd 100644 --- a/package.xml +++ b/package.xml @@ -180,6 +180,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -283,6 +284,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -401,6 +403,9 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + + diff --git a/src/Standards/Generic/Docs/Arrays/NoSpaceAfterArrayKeywordStandard.xml b/src/Standards/Generic/Docs/Arrays/NoSpaceAfterArrayKeywordStandard.xml new file mode 100644 index 0000000000..e6020d7302 --- /dev/null +++ b/src/Standards/Generic/Docs/Arrays/NoSpaceAfterArrayKeywordStandard.xml @@ -0,0 +1,23 @@ + + + + + + + array( + 'foo' => 'bar', +); + ]]> + + + array ( + 'foo' => 'bar', +); + ]]> + + + diff --git a/src/Standards/Generic/Sniffs/Arrays/NoSpaceAfterArrayKeywordSniff.php b/src/Standards/Generic/Sniffs/Arrays/NoSpaceAfterArrayKeywordSniff.php new file mode 100644 index 0000000000..ef7578ddae --- /dev/null +++ b/src/Standards/Generic/Sniffs/Arrays/NoSpaceAfterArrayKeywordSniff.php @@ -0,0 +1,73 @@ + + * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; + +class NoSpaceAfterArrayKeywordSniff implements Sniff +{ + + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + */ + public function register() + { + return [T_ARRAY]; + + }//end register() + + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\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(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + $arrayStart = $tokens[$stackPtr]['parenthesis_opener']; + if (isset($tokens[$arrayStart]['parenthesis_closer']) === false) { + return; + } + + if ($arrayStart !== ($stackPtr + 1)) { + $error = 'There must be no space between the "array" keyword and the opening parenthesis'; + + $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $arrayStart, true); + if (isset(Tokens::$commentTokens[$tokens[$next]['code']]) === true) { + // We don't have anywhere to put the comment, so don't attempt to fix it. + $phpcsFile->addError($error, $stackPtr, 'SpaceAfterKeyword'); + } else { + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword'); + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + $phpcsFile->fixer->endChangeset(); + } + } + } + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Tests/Arrays/NoSpaceAfterArrayKeywordUnitTest.inc b/src/Standards/Generic/Tests/Arrays/NoSpaceAfterArrayKeywordUnitTest.inc new file mode 100644 index 0000000000..0fcf00b441 --- /dev/null +++ b/src/Standards/Generic/Tests/Arrays/NoSpaceAfterArrayKeywordUnitTest.inc @@ -0,0 +1,8 @@ + + * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Standards\Generic\Tests\Arrays; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + +class NoSpaceAfterArrayKeywordUnitTest 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 + */ + public function getErrorList() + { + return [ + 4 => 1, + 5 => 1, + 7 => 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 + */ + public function getWarningList() + { + return []; + + }//end getWarningList() + + +}//end class