From 9d70ae352fdd6ce849d0cc91b5565e948809c3e7 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Mon, 5 Sep 2016 14:58:54 +0900 Subject: [PATCH 1/4] Revert "(#2503) fixed coding standard rule considering whitespace" (refs #2503) This reverts commit 307729cb350ce8fd290aa78d1a61c4f31717cf71. --- .../Sniffs/WhiteSpace/ControlSignatureSniff.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php index d89d61dda..d89fd6268 100644 --- a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php +++ b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php @@ -19,14 +19,14 @@ class OpenPNE3_Sniffs_WhiteSpace_ControlSignatureSniff extends PHP_CodeSniffer_S protected function getPatterns() { return array( - 'tryEOL *{EOL...}EOL *catch (...)EOL *{EOL', - 'doEOL *{EOL...}EOL *while (...);EOL', - 'while (...)EOL *{EOL', - 'for (...)EOL *{EOL', - 'if (...)EOL *{EOL', - 'foreach (...)EOL *{EOL', - '} *EOLelseif (...)EOL *{EOL', - '} *EOLelseEOL *{EOL', + 'tryEOL{EOL...}EOLcatch (...)EOL{EOL', + 'doEOL{EOL...}EOLwhile (...);EOL', + 'while (...)EOL{EOL', + 'for (...)EOL{EOL', + 'if (...)EOL{EOL', + 'foreach (...)EOL{EOL', + '}EOLelseif (...)EOL{EOL', + '}EOLelseEOL{EOL', ); } } From 7fb0fcb286f5fd738f878da9bd89df6b35ce9380 Mon Sep 17 00:00:00 2001 From: Kousuke Ebihara Date: Wed, 27 Oct 2010 19:06:23 +0900 Subject: [PATCH 2/4] added the some consideration of wrong-checking in CodingStandard (refs #4002, BP from #1744) (cherry picked from commit 6ca5dcd853b3594685a5274bf7d526d48bc9eaf8) --- .../OpenPNE3/OpenPNE3CodingStandard.php | 4 + .../Sniffs/Arrays/ArrayDeclarationSniff.php | 3 + .../Sniffs/Commenting/ClassCommentSniff.php | 168 ++++++++++++++++++ .../Functions/FunctionCallSignatureSniff.php | 23 +++ .../NamingConventions/ValidClassNameSniff.php | 19 +- .../WhiteSpace/ControlSignatureSniff.php | 32 ---- 6 files changed, 214 insertions(+), 35 deletions(-) create mode 100644 data/CodingStandard/OpenPNE3/Sniffs/Functions/FunctionCallSignatureSniff.php delete mode 100644 data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php diff --git a/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php b/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php index 6cb9caaee..7b68afe69 100644 --- a/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php +++ b/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php @@ -30,6 +30,9 @@ public function getIncludedSniffs() 'Squiz/Sniffs/Scope/MemberVarScopeSniff.php', 'Squiz/Sniffs/Scope/MethodScopeSniff.php', 'Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php', + + 'Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php', + 'Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php' ); } @@ -38,6 +41,7 @@ public function getExcludedSniffs() return array( 'PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php', 'PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php', + 'PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php', 'Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php', 'Squiz/Sniffs/Functions/GlobalFunctionSniff.php', ); diff --git a/data/CodingStandard/OpenPNE3/Sniffs/Arrays/ArrayDeclarationSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/Arrays/ArrayDeclarationSniff.php index 9ac3068c2..9e18551a7 100644 --- a/data/CodingStandard/OpenPNE3/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/data/CodingStandard/OpenPNE3/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -23,6 +23,9 @@ public function register() public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + // this class is not stable yet... + return null; + $tokens = $phpcsFile->getTokens(); $arrayStart = $tokens[$stackPtr]['parenthesis_opener']; diff --git a/data/CodingStandard/OpenPNE3/Sniffs/Commenting/ClassCommentSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/Commenting/ClassCommentSniff.php index a126e4117..0eadc52d8 100644 --- a/data/CodingStandard/OpenPNE3/Sniffs/Commenting/ClassCommentSniff.php +++ b/data/CodingStandard/OpenPNE3/Sniffs/Commenting/ClassCommentSniff.php @@ -16,6 +16,174 @@ */ class OpenPNE3_Sniffs_Commenting_ClassCommentSniff extends Squiz_Sniffs_Commenting_ClassCommentSniff { + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + if ( + // myUser.class.php should be excluded + false !== strpos($phpcsFile->getFilename(), 'myUser.class.php') + ) + { + return null; + } + + $this->currentFile = $phpcsFile; + + $tokens = $phpcsFile->getTokens(); + $find = array (T_ABSTRACT, T_WHITESPACE, T_FINAL); + + // Extract the class comment docblock. + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); + + if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) + { + $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr); + + return; + } else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) + { + $phpcsFile->addError('Missing class doc comment', $stackPtr); + + return; + } + + $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1); + $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); + + // Distinguish file and class comment. + $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1)); + if ($prevClassToken === false) + { + // This is the first class token in this file, need extra checks. + $prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true); + if ($prevNonComment !== false) + { + $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1)); + if ($prevComment === false) + { + // There is only 1 doc comment between open tag and class token. + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); + if ($newlineToken !== false) + { + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, $phpcsFile->eolChar); + if ($newlineToken !== false) + { + // Blank line between the class and the doc block. + // The doc block is most likely a file comment. + $phpcsFile->addError('Missing class doc comment', ($stackPtr + 1)); + + return; + } + }//end if + }//end if + + // Exactly one blank line before the class comment. + $prevTokenEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); + if ($prevTokenEnd !== false) + { + $blankLineBefore = 0; + for ($i = ($prevTokenEnd + 1); $i < $commentStart; $i++) + { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) + { + $blankLineBefore++; + } + } + + if ($blankLineBefore !== 2) + { + $error = 'There must be exactly one blank line before the class comment'; + $phpcsFile->addError($error, ($commentStart - 1)); + } + } + }//end if + }//end if + + $commentString = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1)); + + // Parse the class comment docblock. + try + { + $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($commentString, $phpcsFile); + $this->commentParser->parse(); + } + catch (PHP_CodeSniffer_CommentParser_ParserException $e) + { + $line = ($e->getLineWithinComment() + $commentStart); + $phpcsFile->addError($e->getMessage(), $line); + + return; + } + + $comment = $this->commentParser->getComment(); + if (is_null($comment) === true) + { + $error = 'Class doc comment is empty'; + $phpcsFile->addError($error, $commentStart); + + return; + } + + // The first line of the comment should just be the /** code. + $eolPos = strpos($commentString, $phpcsFile->eolChar); + $firstLine = substr($commentString, 0, $eolPos); + if ($firstLine !== '/**') + { + $error = 'The open comment tag must be the only content on the line'; + $phpcsFile->addError($error, $commentStart); + } + + // Check for a comment description. + $short = rtrim($comment->getShortComment(), $phpcsFile->eolChar); + if (trim($short) === '') + { + $error = 'Missing short description in class doc comment'; + $phpcsFile->addError($error, $commentStart); + + return; + } + + // No extra newline before short description. + $newlineCount = 0; + $newlineSpan = strspn($short, $phpcsFile->eolChar); + if ($short !== '' && $newlineSpan > 0) + { + $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; + $error = "Extra $line found before class comment short description"; + $phpcsFile->addError($error, ($commentStart + 1)); + } + + // Exactly one blank line before tags. + $tags = $this->commentParser->getTagOrders(); + if (count($tags) > 1) + { + $newlineSpan = $comment->getNewlineAfter(); + if ($newlineSpan !== 2) + { + $error = 'There must be exactly one blank line before the tags in class comment'; + if ($long !== '') + { + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); + } + + $phpcsFile->addError($error, ($commentStart + $newlineCount)); + $short = rtrim($short, $phpcsFile->eolChar.' '); + } + } + + // Check for unknown/deprecated tags. + $unknownTags = $this->commentParser->getUnknown(); + foreach ($unknownTags as $errorTag) + { + $error = "@$errorTag[tag] tag is not allowed in class comment"; + $phpcsFile->addWarning($error, ($commentStart + $errorTag['line'])); + + return; + } + + // Check each tag. + $this->processTags($commentStart, $commentEnd); + } + protected function processTags($commentStart, $commentEnd) { $foundTags = $this->commentParser->getTagOrders(); diff --git a/data/CodingStandard/OpenPNE3/Sniffs/Functions/FunctionCallSignatureSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/Functions/FunctionCallSignatureSniff.php new file mode 100644 index 000000000..b59f0c6b3 --- /dev/null +++ b/data/CodingStandard/OpenPNE3/Sniffs/Functions/FunctionCallSignatureSniff.php @@ -0,0 +1,23 @@ + + */ +class OpenPNE3_Sniffs_Functions_FunctionCallSignatureSniff extends PEAR_Sniffs_Functions_FunctionCallSignatureSniff +{ + public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens) + { + return null; + } +} diff --git a/data/CodingStandard/OpenPNE3/Sniffs/NamingConventions/ValidClassNameSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/NamingConventions/ValidClassNameSniff.php index 0b3a420d9..6043ea735 100644 --- a/data/CodingStandard/OpenPNE3/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/data/CodingStandard/OpenPNE3/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -18,9 +18,22 @@ class OpenPNE3_Sniffs_NamingConventions_ValidClassNameSniff extends Squiz_Sniffs { public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - if (false !== strpos($phpcsFile->getFilename(), 'model') - || false !== strpos($phpcsFile->getFilename(), 'form') - || false !== strpos($phpcsFile->getFilename(), 'filter') + if ( + // sfOpenPNE... classes are already replaced to op... classes + // But these are still in the source code for BC reason. + false !== strpos($phpcsFile->getFilename(), 'sfOpenPNE') + + // don't check auto-generated files + || false !== strpos($phpcsFile->getFilename(), 'model') + || false !== strpos($phpcsFile->getFilename(), 'form') + || false !== strpos($phpcsFile->getFilename(), 'filter') + + // myUser.class.php should be excluded + || false !== strpos($phpcsFile->getFilename(), 'myUser.class.php') + // actioon classes should be excluded + || false !== strpos($phpcsFile->getFilename(), 'actions.class.php') + || false !== strpos($phpcsFile->getFilename(), 'components.class.php') + || false !== strpos($phpcsFile->getFilename(), 'Action.class.php') ) { return null; diff --git a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php deleted file mode 100644 index d89fd6268..000000000 --- a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ControlSignatureSniff.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ -class OpenPNE3_Sniffs_WhiteSpace_ControlSignatureSniff extends PHP_CodeSniffer_Standards_AbstractPatternSniff -{ - protected function getPatterns() - { - return array( - 'tryEOL{EOL...}EOLcatch (...)EOL{EOL', - 'doEOL{EOL...}EOLwhile (...);EOL', - 'while (...)EOL{EOL', - 'for (...)EOL{EOL', - 'if (...)EOL{EOL', - 'foreach (...)EOL{EOL', - '}EOLelseif (...)EOL{EOL', - '}EOLelseEOL{EOL', - ); - } -} From 610431425c73c881980a76ade5933224f39a8b9e Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Mon, 5 Sep 2016 15:24:55 +0900 Subject: [PATCH 3/4] migrate coding standard ruleset to PHP_CodeSniffer 2.x (refs #4004) Exclude patterns are based on https://www.openpne.jp/archives/3263/ --- .../OpenPNE3/OpenPNE3CodingStandard.php | 49 ------------------- .../Sniffs/WhiteSpace/ScopeIndentSniff.php | 2 +- data/CodingStandard/OpenPNE3/ruleset.xml | 26 ++++++++++ 3 files changed, 27 insertions(+), 50 deletions(-) delete mode 100644 data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php create mode 100644 data/CodingStandard/OpenPNE3/ruleset.xml diff --git a/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php b/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php deleted file mode 100644 index 7b68afe69..000000000 --- a/data/CodingStandard/OpenPNE3/OpenPNE3CodingStandard.php +++ /dev/null @@ -1,49 +0,0 @@ - - */ -class PHP_CodeSniffer_Standards_OpenPNE3_OpenPNE3CodingStandard extends PHP_CodeSniffer_Standards_CodingStandard -{ - public function getIncludedSniffs() - { - return array( - 'Zend', - 'Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php', - 'Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php', - 'Squiz/Sniffs/Scope/MemberVarScopeSniff.php', - 'Squiz/Sniffs/Scope/MethodScopeSniff.php', - 'Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php', - - 'Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php', - 'Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php' - ); - } - - public function getExcludedSniffs() - { - return array( - 'PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php', - 'PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php', - 'PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php', - 'Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php', - 'Squiz/Sniffs/Functions/GlobalFunctionSniff.php', - ); - } -} diff --git a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php index c79cc5c90..699e65251 100644 --- a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php @@ -16,5 +16,5 @@ */ class OpenPNE3_Sniffs_WhiteSpace_ScopeIndentSniff extends Generic_Sniffs_WhiteSpace_ScopeIndentSniff { - protected $indent = 2; + public $indent = 2; } diff --git a/data/CodingStandard/OpenPNE3/ruleset.xml b/data/CodingStandard/OpenPNE3/ruleset.xml new file mode 100644 index 000000000..f7a35d1c7 --- /dev/null +++ b/data/CodingStandard/OpenPNE3/ruleset.xml @@ -0,0 +1,26 @@ + + + OpenPNE3 Coding Standard + + ^(cache|config|data|plugins|test|web)/ + ^lib/plugins/ + ^lib/vendor/ + ^lib/(filter|form|model)/doctrine/base/ + ^lib/(filter|form|model)/doctrine/op.+Plugin/ + (^|/)templates/ + + + + + + + + + + + + + + + + From 14daa2be71b615d37569362ee0e23b75beeaa144 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Mon, 5 Sep 2016 15:48:19 +0900 Subject: [PATCH 4/4] use rule properties instead of inheriting the sniff class (refs #4004) --- .../Sniffs/WhiteSpace/ScopeIndentSniff.php | 20 ------------------- data/CodingStandard/OpenPNE3/ruleset.xml | 5 +++++ 2 files changed, 5 insertions(+), 20 deletions(-) delete mode 100644 data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php diff --git a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php b/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php deleted file mode 100644 index 699e65251..000000000 --- a/data/CodingStandard/OpenPNE3/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ /dev/null @@ -1,20 +0,0 @@ - - */ -class OpenPNE3_Sniffs_WhiteSpace_ScopeIndentSniff extends Generic_Sniffs_WhiteSpace_ScopeIndentSniff -{ - public $indent = 2; -} diff --git a/data/CodingStandard/OpenPNE3/ruleset.xml b/data/CodingStandard/OpenPNE3/ruleset.xml index f7a35d1c7..0e580d7a4 100644 --- a/data/CodingStandard/OpenPNE3/ruleset.xml +++ b/data/CodingStandard/OpenPNE3/ruleset.xml @@ -22,5 +22,10 @@ + + + + +