diff --git a/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php new file mode 100644 index 00000000..df59c9ac --- /dev/null +++ b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php @@ -0,0 +1,86 @@ +getTokens(); + + if ($tokens[$stackPtr]['code'] !== T_CONST + && !($tokens[$stackPtr]['content'] === 'define' && $tokens[$stackPtr + 1]['code'] === T_OPEN_PARENTHESIS) + ) { + return; + } + + $constNamePtr = $phpcsFile->findNext( + ($tokens[$stackPtr]['code'] === T_CONST) ? T_STRING : T_CONSTANT_ENCAPSED_STRING, + $stackPtr + 1, + null, + false, + null, + true + ); + $constName = strtolower(trim($tokens[$constNamePtr]['content'], " '\"")); + + $commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true); + if ($commentStartPtr === false) { + return; + } + + $commentCloserPtr = $tokens[$commentStartPtr]['comment_closer']; + for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) { + $token = $tokens[$i]; + + // Not an interesting string + if ($token['code'] !== T_DOC_COMMENT_STRING) { + continue; + } + + // Comment is the same as constant name + $docComment = trim(strtolower($token['content']), ',.'); + if ($docComment === $constName) { + continue; + } + + // Comment is exactly the same as constant name + $docComment = str_replace(' ', '_', $docComment); + if ($docComment === $constName) { + continue; + } + + // We have found at lease one meaningful line in comment description + return; + } + + $phpcsFile->addWarning( + 'Constants must have short description if they add information beyond what the constant name supplies.', + $stackPtr, + 'MissingConstantPHPDoc' + ); + } +} diff --git a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.1.inc b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.1.inc new file mode 100644 index 00000000..e3d2000a --- /dev/null +++ b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.1.inc @@ -0,0 +1,18 @@ +'; + + /** + * Unlike first const, this one is not self explanatory. + */ + const NUMBER_TWO = 2; +} diff --git a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.2.inc b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.2.inc new file mode 100644 index 00000000..c4189ff1 --- /dev/null +++ b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.2.inc @@ -0,0 +1,21 @@ +0'); + +/** */ +define('NUMBER_ONE', 1); + +class Profiler +{ + /** + * Nesting separator. + */ + const NESTING_SEPARATOR = '->'; + + /** + * + */ + const NUMBER_TWO = 2; +} diff --git a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php new file mode 100644 index 00000000..b3042b18 --- /dev/null +++ b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php @@ -0,0 +1,39 @@ + 1, + 8 => 1, + 15 => 1, + 20 => 1 + ]; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 95e91e69..98100eae 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -496,6 +496,10 @@ + + 5 + warning + 5 warning