From fe112a7592f04b0b45f97b73bf87c9a53c5198c3 Mon Sep 17 00:00:00 2001 From: Mikael Randy Date: Thu, 16 Jul 2015 22:48:41 +0200 Subject: [PATCH] Mod: update Symfony2 coding standard to PHP_CodeSniffer v2 interface --- README.md | 3 +- Symfony2/Docs/.placeholder | 0 .../Arrays/MultiLineArrayCommaSniff.php | 95 ++++++++++++ .../Classes/MultipleClassesOneFileSniff.php | 6 +- .../Classes/PropertyDeclarationSniff.php | 84 ++++++++++ .../Sniffs/Commenting/ClassCommentSniff.php | 4 +- .../Commenting/FunctionCommentSniff.php | 55 ++++--- .../Formatting/BlankLineBeforeReturnSniff.php | 8 +- Symfony2/Sniffs/Functions/ScopeOrderSniff.php | 103 +++++++++++++ .../InterfaceSuffixSniff.php | 78 ---------- .../NamingConventions/ValidClassNameSniff.php | 145 ++++++++++++++++++ .../Objects/ObjectInstantiationSniff.php | 91 +++++++++++ .../WhiteSpace/BinaryOperatorSpacingSniff.php | 71 +++++++++ .../Sniffs/WhiteSpace/CommaSpacingSniff.php | 77 ++++++++++ .../WhiteSpace/DiscourageFitzinatorSniff.php | 8 +- .../Arrays/MultiLineArrayCommaUnitTest.inc | 73 +++++++++ .../Arrays/MultiLineArrayCommaUnitTest.php | 61 ++++++++ .../BlankLineBeforeReturnUnitTest.inc | 15 +- .../BlankLineBeforeReturnUnitTest.php | 14 +- .../Objects/ObjectInstantiationUnitTest.inc | 23 +++ .../Objects/ObjectInstantiationUnitTest.php | 61 ++++++++ Symfony2/ruleset.xml | 22 +-- 22 files changed, 966 insertions(+), 131 deletions(-) delete mode 100644 Symfony2/Docs/.placeholder create mode 100644 Symfony2/Sniffs/Arrays/MultiLineArrayCommaSniff.php create mode 100644 Symfony2/Sniffs/Classes/PropertyDeclarationSniff.php create mode 100644 Symfony2/Sniffs/Functions/ScopeOrderSniff.php delete mode 100644 Symfony2/Sniffs/NamingConventions/InterfaceSuffixSniff.php create mode 100644 Symfony2/Sniffs/NamingConventions/ValidClassNameSniff.php create mode 100644 Symfony2/Sniffs/Objects/ObjectInstantiationSniff.php create mode 100644 Symfony2/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php create mode 100644 Symfony2/Sniffs/WhiteSpace/CommaSpacingSniff.php create mode 100644 Symfony2/Tests/Arrays/MultiLineArrayCommaUnitTest.inc create mode 100644 Symfony2/Tests/Arrays/MultiLineArrayCommaUnitTest.php create mode 100644 Symfony2/Tests/Objects/ObjectInstantiationUnitTest.inc create mode 100644 Symfony2/Tests/Objects/ObjectInstantiationUnitTest.php mode change 100644 => 100755 Symfony2/ruleset.xml diff --git a/README.md b/README.md index 5ccb1ea..5c8da0b 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ We also added to this project our own standard for our Symfony2 developments ent ## Credits -Developped by [M6Web](http://tech.m6web.fr/). +Developped by [M6Web](http://tech.m6web.fr/), cloned from [lapistano/Symfony2-coding-standard](https://github.com/lapistano/Symfony2-coding-standard). +The PHP_CodeSniffer 2.0+ Symfony2 standard is very broadly inspired from [escapestudios/Symfony2-coding-standard](https://github.com/escapestudios/Symfony2-coding-standard) ## License diff --git a/Symfony2/Docs/.placeholder b/Symfony2/Docs/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/Symfony2/Sniffs/Arrays/MultiLineArrayCommaSniff.php b/Symfony2/Sniffs/Arrays/MultiLineArrayCommaSniff.php new file mode 100644 index 0000000..debcb29 --- /dev/null +++ b/Symfony2/Sniffs/Arrays/MultiLineArrayCommaSniff.php @@ -0,0 +1,95 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_WhiteSpace_MultiLineArrayCommaSniff. + * + * Throws warnings if the last item in a multi line array does not have a + * trailing comma + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_Arrays_MultiLineArrayCommaSniff + implements PHP_CodeSniffer_Sniff +{ + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_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) + { + $tokens = $phpcsFile->getTokens(); + $open = $tokens[$stackPtr]; + + if ($open['code'] === T_ARRAY) { + $closePtr = $open['parenthesis_closer']; + } else { + $closePtr = $open['bracket_closer']; + } + + if ($open['line'] <> $tokens[$closePtr]['line']) { + $lastComma = $phpcsFile->findPrevious(T_COMMA, $closePtr); + + while ($lastComma < $closePtr -1) { + $lastComma++; + + if ($tokens[$lastComma]['code'] !== T_WHITESPACE + && $tokens[$lastComma]['code'] !== T_COMMENT + ) { + $phpcsFile->addError( + 'Add a comma after each item in a multi-line array', + $stackPtr, + 'Invalid' + ); + break; + } + } + } + + }//end process() + +}//end class + diff --git a/Symfony2/Sniffs/Classes/MultipleClassesOneFileSniff.php b/Symfony2/Sniffs/Classes/MultipleClassesOneFileSniff.php index 1cca743..3f22381 100644 --- a/Symfony2/Sniffs/Classes/MultipleClassesOneFileSniff.php +++ b/Symfony2/Sniffs/Classes/MultipleClassesOneFileSniff.php @@ -7,10 +7,10 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Symfony2-phpcs-authors + * @author Symfony2-phpcs-authors * @license http://spdx.org/licenses/MIT MIT License * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ /** @@ -24,7 +24,7 @@ * @package PHP_CodeSniffer-Symfony2 * @author Dave Hauenstein * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ class Symfony2_Sniffs_Classes_MultipleClassesOneFileSniff implements PHP_CodeSniffer_Sniff { diff --git a/Symfony2/Sniffs/Classes/PropertyDeclarationSniff.php b/Symfony2/Sniffs/Classes/PropertyDeclarationSniff.php new file mode 100644 index 0000000..7229b20 --- /dev/null +++ b/Symfony2/Sniffs/Classes/PropertyDeclarationSniff.php @@ -0,0 +1,84 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_Classes_PropertyDeclarationSniff. + * + * Throws warnings if properties are declared after methods + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_Classes_PropertyDeclarationSniff implements PHP_CodeSniffer_Sniff +{ + + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_CLASS, + ); + }//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) + { + $tokens = $phpcsFile->getTokens(); + $scope = $phpcsFile->findNext(T_FUNCTION, $stackPtr, $tokens[$stackPtr]['scope_closer']); + + $wantedTokens = array( + T_PUBLIC, + T_PROTECTED, + T_PRIVATE + ); + + while ($scope) { + $scope = $phpcsFile->findNext($wantedTokens, $scope + 1, $tokens[$stackPtr]['scope_closer']); + + if ($scope && $tokens[$scope + 2]['code'] === T_VARIABLE) { + $phpcsFile->addError( + 'Declare class properties before methods', + $scope, + 'Invalid' + ); + } + } + }//end process() + +}//end class diff --git a/Symfony2/Sniffs/Commenting/ClassCommentSniff.php b/Symfony2/Sniffs/Commenting/ClassCommentSniff.php index 262f777..e053676 100644 --- a/Symfony2/Sniffs/Commenting/ClassCommentSniff.php +++ b/Symfony2/Sniffs/Commenting/ClassCommentSniff.php @@ -14,8 +14,8 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ -if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) { - $error = 'Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found'; +if (class_exists('PHP_CodeSniffer_Tokenizers_Comment', true) === false) { + $error = 'Class PHP_CodeSniffer_Tokenizers_Comment not found'; throw new PHP_CodeSniffer_Exception($error); } diff --git a/Symfony2/Sniffs/Commenting/FunctionCommentSniff.php b/Symfony2/Sniffs/Commenting/FunctionCommentSniff.php index a200b65..48cffe8 100644 --- a/Symfony2/Sniffs/Commenting/FunctionCommentSniff.php +++ b/Symfony2/Sniffs/Commenting/FunctionCommentSniff.php @@ -6,10 +6,10 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Symfony2-phpcs-authors + * @author Symfony2-phpcs-authors * @license http://spdx.org/licenses/MIT MIT License * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ if (class_exists('PEAR_Sniffs_Commenting_FunctionCommentSniff', true) === false) { @@ -68,29 +68,31 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) /** * Process the return comment of this function comment. * - * @param int $commentStart The position in the stack where the comment started. - * @param int $commentEnd The position in the stack where the comment ended. + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param int $commentStart The position in the stack where the comment started. * * @return void */ - protected function processReturn($commentStart, $commentEnd) + protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) { - if ($this->isInheritDoc()) { + + if ($this->isInheritDoc($phpcsFile, $stackPtr)) { return; } - $tokens = $this->currentFile->getTokens(); - $funcPtr = $this->currentFile->findNext(T_FUNCTION, $commentEnd); + $tokens = $phpcsFile->getTokens(); // Only check for a return comment if a non-void return statement exists - if (isset($tokens[$funcPtr]['scope_opener'])) { - $start = $tokens[$funcPtr]['scope_opener']; + if (isset($tokens[$stackPtr]['scope_opener'])) { + $start = $tokens[$stackPtr]['scope_opener']; // iterate over all return statements of this function, // run the check on the first which is not only 'return;' - while ($returnToken = $this->currentFile->findNext(T_RETURN, $start, $tokens[$funcPtr]['scope_closer'])) { + while ($returnToken = $phpcsFile->findNext(T_RETURN, $start, $tokens[$stackPtr]['scope_closer'])) { if ($this->isMatchingReturn($tokens, $returnToken)) { - parent::processReturn($commentStart, $commentEnd); + parent::processReturn($phpcsFile, $stackPtr, $commentStart); break; } $start = $returnToken + 1; @@ -102,11 +104,20 @@ protected function processReturn($commentStart, $commentEnd) /** * Is the comment an inheritdoc? * + * @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 boolean True if the comment is an inheritdoc */ - protected function isInheritDoc () + protected function isInheritDoc(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - $content = $this->commentParser->getComment()->getContent(); + $tokens = $phpcsFile->getTokens(); + + $start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1); + $end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start); + + $content = $phpcsFile->getTokensAsString($start, ($end - $start)); return preg_match('#{@inheritdoc}#i', $content) === 1; } // end isInheritDoc() @@ -114,18 +125,22 @@ protected function isInheritDoc () /** * Process the function parameter comments. * - * @param int $commentStart The position in the stack where - * the comment started. + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param int $commentStart The position in the stack where the comment started. * * @return void */ - protected function processParams($commentStart) + protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) { - if ($this->isInheritDoc()) { + $tokens = $phpcsFile->getTokens(); + + if ($this->isInheritDoc($phpcsFile, $stackPtr)) { return; } - parent::processParams($commentStart); + parent::processParams($phpcsFile, $stackPtr, $commentStart); } // end processParams() /** @@ -136,7 +151,7 @@ protected function processParams($commentStart) * * @return boolean True if the return does not return anything */ - protected function isMatchingReturn ($tokens, $returnPos) + protected function isMatchingReturn($tokens, $returnPos) { do { $returnPos++; diff --git a/Symfony2/Sniffs/Formatting/BlankLineBeforeReturnSniff.php b/Symfony2/Sniffs/Formatting/BlankLineBeforeReturnSniff.php index fbe36c6..c82c530 100644 --- a/Symfony2/Sniffs/Formatting/BlankLineBeforeReturnSniff.php +++ b/Symfony2/Sniffs/Formatting/BlankLineBeforeReturnSniff.php @@ -7,10 +7,10 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Symfony2-phpcs-authors + * @author Symfony2-phpcs-authors * @license http://spdx.org/licenses/MIT MIT License * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ /** @@ -24,7 +24,7 @@ * @package PHP_CodeSniffer-Symfony2 * @author Dave Hauenstein * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ class Symfony2_Sniffs_Formatting_BlankLineBeforeReturnSniff implements PHP_CodeSniffer_Sniff { @@ -68,6 +68,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($tokens[$current]['line'] == $previousLine && $tokens[$current]['type'] !== 'T_WHITESPACE' && $tokens[$current]['type'] !== 'T_COMMENT' + && $tokens[$current]['type'] !== 'T_DOC_COMMENT_CLOSE_TAG' + && $tokens[$current]['type'] !== 'T_DOC_COMMENT_WHITESPACE' ) { $prevLineTokens[] = $tokens[$current]['type']; } diff --git a/Symfony2/Sniffs/Functions/ScopeOrderSniff.php b/Symfony2/Sniffs/Functions/ScopeOrderSniff.php new file mode 100644 index 0000000..ead4577 --- /dev/null +++ b/Symfony2/Sniffs/Functions/ScopeOrderSniff.php @@ -0,0 +1,103 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_Functions_ScopeOrderSniff. + * + * Throws warnings if properties are declared after methods + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_Functions_ScopeOrderSniff implements PHP_CodeSniffer_Sniff +{ + + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_CLASS, + T_INTERFACE, + ); + }//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) + { + $tokens = $phpcsFile->getTokens(); + $function = $stackPtr; + + $scopes = array( + 0 => T_PUBLIC, + 1 => T_PROTECTED, + 2 => T_PRIVATE, + ); + + $whitelisted = array( + '__construct', + 'setUp', + 'tearDown', + ); + + while ($function) { + $function = $phpcsFile->findNext(T_FUNCTION, $function + 1, $tokens[$stackPtr]['scope_closer']); + + if (isset($tokens[$function]['parenthesis_opener'])) { + $scope = $phpcsFile->findPrevious($scopes, $function -1, $stackPtr); + $name = $phpcsFile->findNext(T_STRING, $function + 1, $tokens[$function]['parenthesis_opener']); + + if ($scope && $name && !in_array($tokens[$name]['content'], $whitelisted)) { + $current = array_keys($scopes, $tokens[$scope]['code']); + $current = $current[0]; + + if (isset($previous) && $current < $previous) { + $phpcsFile->addError( + 'Declare public methods first, then protected ones and finally private ones', + $scope, + 'Invalid' + ); + } + + $previous = $current; + } + } + } + }//end process() + +}//end class diff --git a/Symfony2/Sniffs/NamingConventions/InterfaceSuffixSniff.php b/Symfony2/Sniffs/NamingConventions/InterfaceSuffixSniff.php deleted file mode 100644 index 77534c5..0000000 --- a/Symfony2/Sniffs/NamingConventions/InterfaceSuffixSniff.php +++ /dev/null @@ -1,78 +0,0 @@ - - * @license http://spdx.org/licenses/MIT MIT License - * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard - */ - -/** - * Symfony2_Sniffs_NamingConventions_InterfaceSuffixSniff. - * - * Throws errors if interface names are not suffixed with "Interface". - * - * Symfony coding standard specifies: "Suffix interfaces with Interface;" - * - * @category PHP - * @package PHP_CodeSniffer-Symfony2 - * @author Dave Hauenstein - * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/opensky/Symfony2-coding-standard - */ -class Symfony2_Sniffs_NamingConventions_InterfaceSuffixSniff implements PHP_CodeSniffer_Sniff -{ - /** - * A list of tokenizers this sniff supports. - * - * @var array - */ - public $supportedTokenizers = array( - 'PHP', - ); - - /** - * Returns an array of tokens this test wants to listen for. - * - * @return array - */ - public function register() - { - return array(T_INTERFACE); - } - - /** - * Processes this test, when one of its tokens is encountered. - * - * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. - * @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) - { - $tokens = $phpcsFile->getTokens(); - $line = $tokens[$stackPtr]['line']; - - while ($tokens[$stackPtr]['line'] == $line) { - if ('T_STRING' == $tokens[$stackPtr]['type']) { - if (substr($tokens[$stackPtr]['content'], -9) != 'Interface') { - $phpcsFile->addError( - 'Interface name is not suffixed with "Interface"', - $stackPtr - ); - } - break; - } - $stackPtr++; - } - - return; - } -} diff --git a/Symfony2/Sniffs/NamingConventions/ValidClassNameSniff.php b/Symfony2/Sniffs/NamingConventions/ValidClassNameSniff.php new file mode 100644 index 0000000..dec1047 --- /dev/null +++ b/Symfony2/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -0,0 +1,145 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_NamingConventions_ValidClassNameSniff. + * + * Throws errors if symfony's naming conventions are not met. + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author Dave Hauenstein + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff +{ + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_INTERFACE, + T_TRAIT, + T_EXTENDS, + T_ABSTRACT + ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. + * @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) + { + $tokens = $phpcsFile->getTokens(); + $line = $tokens[$stackPtr]['line']; + + while ($tokens[$stackPtr]['line'] == $line) { + + /* + * Suffix interfaces with Interface; + */ + if ('T_INTERFACE' == $tokens[$stackPtr]['type']) { + $name = $phpcsFile->findNext(T_STRING, $stackPtr); + + if ($name && substr($tokens[$name]['content'], -9) != 'Interface') { + $phpcsFile->addError( + 'Interface name is not suffixed with "Interface"', + $stackPtr, + 'Invalid' + ); + } + break; + } + + /* + * Suffix traits with Trait; + */ + if ('T_TRAIT' == $tokens[$stackPtr]['type']) { + $name = $phpcsFile->findNext(T_STRING, $stackPtr); + + if ($name && substr($tokens[$name]['content'], -5) != 'Trait') { + $phpcsFile->addError( + 'Trait name is not suffixed with "Trait"', + $stackPtr, + 'Invalid' + ); + } + break; + } + + /* + * Suffix exceptions with Exception; + */ + if ('T_EXTENDS' == $tokens[$stackPtr]['type']) { + $extend = $phpcsFile->findNext(T_STRING, $stackPtr); + + if ($extend && substr($tokens[$extend]['content'], -9) == 'Exception') { + $class = $phpcsFile->findPrevious(T_CLASS, $stackPtr); + $name = $phpcsFile->findNext(T_STRING, $class); + + if ($name && substr($tokens[$name]['content'], -9) != 'Exception') { + $phpcsFile->addError( + 'Exception name is not suffixed with "Exception"', + $stackPtr, + 'Invalid' + ); + } + } + break; + } + + /* + * Prefix abstract classes with Abstract. + */ + if ('T_ABSTRACT' == $tokens[$stackPtr]['type']) { + $name = $phpcsFile->findNext(T_STRING, $stackPtr); + $function = $phpcsFile->findNext(T_FUNCTION, $stackPtr); + + // making sure we're not dealing with an abstract function + if ($name && (is_null($function) || $name < $function) && substr($tokens[$name]['content'], 0, 8) != 'Abstract') { + $phpcsFile->addError( + 'Abstract class name is not prefixed with "Abstract"', + $stackPtr, + 'Invalid' + ); + } + break; + } + + $stackPtr++; + } + + return; + } +} diff --git a/Symfony2/Sniffs/Objects/ObjectInstantiationSniff.php b/Symfony2/Sniffs/Objects/ObjectInstantiationSniff.php new file mode 100644 index 0000000..a03d352 --- /dev/null +++ b/Symfony2/Sniffs/Objects/ObjectInstantiationSniff.php @@ -0,0 +1,91 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_Objects_ObjectInstantiationSniff. + * + * Throws a warning if an object isn't instantiated using parenthesis. + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_Objects_ObjectInstantiationSniff implements PHP_CodeSniffer_Sniff +{ + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_NEW, + ); + + }//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) + { + $tokens = $phpcsFile->getTokens(); + $allowed = array( + T_STRING, + T_NS_SEPARATOR, + T_VARIABLE, + ); + + $object = $stackPtr; + $line = $tokens[$object]['line']; + + while ($object && $tokens[$object]['line'] === $line) { + $object = $phpcsFile->findNext($allowed, $object + 1); + + if ($tokens[$object]['line'] === $line && !in_array($tokens[$object + 1]['code'], $allowed)) { + if ($tokens[$object + 1]['code'] !== T_OPEN_PARENTHESIS) { + $phpcsFile->addError( + 'Use parentheses when instantiating classes', + $stackPtr, + 'Invalid' + ); + } + + break; + } + } + + }//end process() + +}//end class diff --git a/Symfony2/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php b/Symfony2/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php new file mode 100644 index 0000000..52c3abf --- /dev/null +++ b/Symfony2/Sniffs/WhiteSpace/BinaryOperatorSpacingSniff.php @@ -0,0 +1,71 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_WhiteSpace_BinaryOperatorSpacingSniff. + * + * Throws warnings if a binary operator isn't surrounded with whitespace. + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_WhiteSpace_BinaryOperatorSpacingSniff +{ + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return PHP_CodeSniffer_Tokens::$comparisonTokens; + + }//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) + { + $tokens = $phpcsFile->getTokens(); + + if ($tokens[$stackPtr -1]['code'] !== T_WHITESPACE || $tokens[$stackPtr +1]['code'] !== T_WHITESPACE) { + $phpcsFile->addError( + 'Add a single space around binary operators', + $stackPtr, + 'Invalid' + ); + } + }//end process() + +}//end class diff --git a/Symfony2/Sniffs/WhiteSpace/CommaSpacingSniff.php b/Symfony2/Sniffs/WhiteSpace/CommaSpacingSniff.php new file mode 100644 index 0000000..c4c3bf2 --- /dev/null +++ b/Symfony2/Sniffs/WhiteSpace/CommaSpacingSniff.php @@ -0,0 +1,77 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Symfony2_Sniffs_WhiteSpace_CommaSpacingSniff. + * + * Throws warnings if comma isn't followed by a whitespace. + * + * @category PHP + * @package PHP_CodeSniffer-Symfony2 + * @author wicliff wolda + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Sniffs_WhiteSpace_CommaSpacingSniff implements PHP_CodeSniffer_Sniff +{ + /** + * A list of tokenizers this sniff supports. + * + * @var array + */ + public $supportedTokenizers = array( + 'PHP', + ); + + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array( + T_COMMA, + ); + + }//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) + { + $tokens = $phpcsFile->getTokens(); + $line = $tokens[$stackPtr]['line']; + + if ($tokens[$stackPtr + 1]['line'] === $line && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) { + $phpcsFile->addError( + 'Add a single space after each comma delimiter', + $stackPtr, + 'Invalid' + ); + } + + }//end process() + +}//end class + diff --git a/Symfony2/Sniffs/WhiteSpace/DiscourageFitzinatorSniff.php b/Symfony2/Sniffs/WhiteSpace/DiscourageFitzinatorSniff.php index 8ac4abe..98f1480 100644 --- a/Symfony2/Sniffs/WhiteSpace/DiscourageFitzinatorSniff.php +++ b/Symfony2/Sniffs/WhiteSpace/DiscourageFitzinatorSniff.php @@ -6,10 +6,10 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Symfony2-phpcs-authors + * @author Symfony2-phpcs-authors * @license http://spdx.org/licenses/MIT MIT License * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ /** @@ -19,9 +19,9 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Justin Hileman + * @author Justin Hileman * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ class Symfony2_Sniffs_WhiteSpace_DiscourageFitzinatorSniff implements PHP_CodeSniffer_Sniff { diff --git a/Symfony2/Tests/Arrays/MultiLineArrayCommaUnitTest.inc b/Symfony2/Tests/Arrays/MultiLineArrayCommaUnitTest.inc new file mode 100644 index 0000000..dc8f061 --- /dev/null +++ b/Symfony2/Tests/Arrays/MultiLineArrayCommaUnitTest.inc @@ -0,0 +1,73 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Unit test class for the MultiLineArrayComma 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 Craige leeder + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ +class Symfony2_Tests_Arrays_MultiLineArrayCommaUnitTest 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 array( + 11 => 1, + 24 => 1, + 37 => 1, + 47 => 1, + 60 => 1, + 70 => 1, + ); + } + + /** + * 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 errors that should occur on that line. + * + * @return array(int => int) + */ + public function getWarningList() + { + return array(); + } +} diff --git a/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc b/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc index 4e2a58d..90dd845 100644 --- a/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc +++ b/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc @@ -17,6 +17,20 @@ function validFunctionReturnThree() return; } +function validFunctionReturnFour() +{ + // comment + return; +} + +function validFunctionReturnFive() +{ + /** + * multi-line + */ + return; +} + function invalidFunctionReturnOne() { echo ""; @@ -30,4 +44,3 @@ switch ($condition) { default: return false; } - diff --git a/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.php b/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.php index cafc60e..4550622 100644 --- a/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.php +++ b/Symfony2/Tests/Formatting/BlankLineBeforeReturnUnitTest.php @@ -6,10 +6,10 @@ * * @category PHP * @package PHP_CodeSniffer-Symfony2 - * @author Symfony2-phpcs-authors + * @author Symfony2-phpcs-authors * @license http://spdx.org/licenses/MIT MIT License * @version GIT: master - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ /** @@ -23,7 +23,7 @@ * @author Tom Klingenberg * @copyright 2012 Tom Klingenberg, some rights reserved. * @license http://spdx.org/licenses/MIT MIT License - * @link https://github.com/opensky/Symfony2-coding-standard + * @link https://github.com/M6Web/Symfony2-coding-standard */ class Symfony2_Tests_Formatting_BlankLineBeforeReturnUnitTest extends AbstractSniffUnitTest { @@ -38,22 +38,20 @@ class Symfony2_Tests_Formatting_BlankLineBeforeReturnUnitTest extends AbstractSn public function getErrorList() { return array( - 23 => 1, + 37 => 1 ); - } /** * 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. + * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); - } -} \ No newline at end of file +} diff --git a/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.inc b/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.inc new file mode 100644 index 0000000..75e70e5 --- /dev/null +++ b/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.inc @@ -0,0 +1,23 @@ +foo); +new Foo\Bar(); +new Foo\Bar(true); +new Foo\Bar($this->foo); +new \Foo\Bar(); +new \Foo\Bar(true); +new \Foo\Bar($this->foo); +new $foo(); +new $foo(true); +new $foo($this->foo); diff --git a/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.php b/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.php new file mode 100644 index 0000000..1f033da --- /dev/null +++ b/Symfony2/Tests/Objects/ObjectInstantiationUnitTest.php @@ -0,0 +1,61 @@ + + * @license http://spdx.org/licenses/MIT MIT License + * @version GIT: master + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +/** + * Unit test class for the ObjectInstantiation 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 Roman Weich + * @license http://spdx.org/licenses/MIT MIT License + * @link https://github.com/M6Web/Symfony2-coding-standard + */ + +class Symfony2_Tests_Objects_ObjectInstantiationUnitTest 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 array( + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 1, + ); + } + + /** + * 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 errors that should occur on that line. + * + * @return array(int => int) + */ + public function getWarningList() + { + return array(); + } +} diff --git a/Symfony2/ruleset.xml b/Symfony2/ruleset.xml old mode 100644 new mode 100755 index 1401e14..9915456 --- a/Symfony2/ruleset.xml +++ b/Symfony2/ruleset.xml @@ -5,6 +5,12 @@ */Resources/* + + + + + + - + @@ -73,15 +74,14 @@ 0 - + 0 - - + 0 - + 0