diff --git a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php index e3ed49b8..d890147d 100644 --- a/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php @@ -56,23 +56,8 @@ public function process(File $phpcsFile, $stackPtr) $namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true); - $commentStartPtr = $phpcsFile->findPrevious( - [ - T_WHITESPACE, - T_DOC_COMMENT_STAR, - T_DOC_COMMENT_WHITESPACE, - T_DOC_COMMENT_TAG, - T_DOC_COMMENT_STRING, - T_DOC_COMMENT_CLOSE_TAG - ], - $stackPtr - 1, - null, - true, - null, - true - ); - - if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) { + $commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile); + if ($commentStartPtr === -1) { return; } diff --git a/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php index 205dcdf9..a4b9e71b 100644 --- a/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php @@ -59,8 +59,8 @@ public function process(File $phpcsFile, $stackPtr) true ); - $commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true); - if ($commentStartPtr === false) { + $commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile); + if ($commentStartPtr === -1) { return; } diff --git a/Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php b/Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php index 4112f675..5be093ae 100644 --- a/Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php +++ b/Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php @@ -6,11 +6,47 @@ */ namespace Magento2\Sniffs\Commenting; +use PHP_CodeSniffer\Files\File; + /** * Helper class for common DocBlock validations */ class PHPDocFormattingValidator { + /** + * Finds matching PHPDoc for current pointer + * + * @param int $startPtr + * @param File $phpcsFile + * @return int + */ + public function findPHPDoc($startPtr, $phpcsFile) + { + $tokens = $phpcsFile->getTokens(); + + $commentStartPtr = $phpcsFile->findPrevious( + [ + T_WHITESPACE, + T_DOC_COMMENT_STAR, + T_DOC_COMMENT_WHITESPACE, + T_DOC_COMMENT_TAG, + T_DOC_COMMENT_STRING, + T_DOC_COMMENT_CLOSE_TAG + ], + $startPtr - 1, + null, + true, + null, + true + ); + + if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) { + return -1; + } + + return $commentStartPtr; + } + /** * Determines if the comment identified by $commentStartPtr provides additional meaning to origin at $namePtr * diff --git a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.3.inc b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.3.inc new file mode 100644 index 00000000..ba6686ef --- /dev/null +++ b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.3.inc @@ -0,0 +1,9 @@ +