|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Ongr_Sniffs_Commenting_EmptyCatchCommentSniff. |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category PHP |
| 8 | + * @package PHP_CodeSniffer |
| 9 | + * @author Greg Sherwood <[email protected]> |
| 10 | + * @author Marc McIntyre <[email protected]> |
| 11 | + * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
| 12 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 13 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Ongr_Sniffs_Commenting_DocCommentAlignmentSniff. |
| 18 | + * |
| 19 | + * Tests that the stars in a doc comment align correctly. |
| 20 | + * |
| 21 | + * @category PHP |
| 22 | + * @package PHP_CodeSniffer |
| 23 | + * @author Greg Sherwood <[email protected]> |
| 24 | + * @author Marc McIntyre <[email protected]> |
| 25 | + * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) |
| 26 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 27 | + * @version Release: @package_version@ |
| 28 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 29 | + */ |
| 30 | +class Ongr_Sniffs_Commenting_DocCommentAlignmentSniff implements PHP_CodeSniffer_Sniff |
| 31 | +{ |
| 32 | + |
| 33 | + /** |
| 34 | + * A list of tokenizers this sniff supports. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + public $supportedTokenizers = array( |
| 39 | + 'PHP', |
| 40 | + 'JS', |
| 41 | + ); |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns an array of tokens this test wants to listen for. |
| 46 | + * |
| 47 | + * @return array |
| 48 | + */ |
| 49 | + public function register() |
| 50 | + { |
| 51 | + return array(T_DOC_COMMENT_OPEN_TAG); |
| 52 | + |
| 53 | + }//end register() |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Processes this test, when one of its tokens is encountered. |
| 58 | + * |
| 59 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 60 | + * @param int $stackPtr The position of the current token |
| 61 | + * in the stack passed in $tokens. |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 66 | + { |
| 67 | + $tokens = $phpcsFile->getTokens(); |
| 68 | + |
| 69 | + // We are only interested in function/class/interface doc block comments. |
| 70 | + $ignore = PHP_CodeSniffer_Tokens::$emptyTokens; |
| 71 | + if ($phpcsFile->tokenizerType === 'JS') { |
| 72 | + $ignore[] = T_EQUAL; |
| 73 | + $ignore[] = T_STRING; |
| 74 | + $ignore[] = T_OBJECT_OPERATOR; |
| 75 | + } |
| 76 | + |
| 77 | + $nextToken = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true); |
| 78 | + $ignore = array( |
| 79 | + T_CLASS => true, |
| 80 | + T_INTERFACE => true, |
| 81 | + T_FUNCTION => true, |
| 82 | + T_PUBLIC => true, |
| 83 | + T_PRIVATE => true, |
| 84 | + T_PROTECTED => true, |
| 85 | + T_STATIC => true, |
| 86 | + T_ABSTRACT => true, |
| 87 | + T_PROPERTY => true, |
| 88 | + T_OBJECT => true, |
| 89 | + T_PROTOTYPE => true, |
| 90 | + ); |
| 91 | + |
| 92 | + if (isset($ignore[$tokens[$nextToken]['code']]) === false) { |
| 93 | + // Could be a file comment. |
| 94 | + $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
| 95 | + if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) { |
| 96 | + return; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // There must be one space after each star (unless it is an empty comment line) |
| 101 | + // and all the stars must be aligned correctly. |
| 102 | + $requiredColumn = ($tokens[$stackPtr]['column'] + 1); |
| 103 | + $endComment = $tokens[$stackPtr]['comment_closer']; |
| 104 | + for ($i = ($stackPtr + 1); $i <= $endComment; $i++) { |
| 105 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR |
| 106 | + && $tokens[$i]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
| 107 | + ) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if ($tokens[$i]['code'] === T_DOC_COMMENT_CLOSE_TAG) { |
| 112 | + // Can't process the close tag if it is not the first thing on the line. |
| 113 | + $prev = $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE, ($i - 1), $stackPtr, true); |
| 114 | + if ($tokens[$prev]['line'] === $tokens[$i]['line']) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if ($tokens[$i]['column'] !== $requiredColumn) { |
| 120 | + $error = 'Expected %s space(s) before asterisk; %s found'; |
| 121 | + $data = array( |
| 122 | + ($requiredColumn - 1), |
| 123 | + ($tokens[$i]['column'] - 1), |
| 124 | + ); |
| 125 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); |
| 126 | + if ($fix === true) { |
| 127 | + $padding = str_repeat(' ', ($requiredColumn - 1)); |
| 128 | + if ($tokens[$i]['column'] === 1) { |
| 129 | + $phpcsFile->fixer->addContentBefore($i, $padding); |
| 130 | + } else { |
| 131 | + $phpcsFile->fixer->replaceToken(($i - 1), $padding); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + if ($tokens[($i + 2)]['line'] !== $tokens[$i]['line']) { |
| 141 | + // Line is empty. |
| 142 | + continue; |
| 143 | + } |
| 144 | + //ONGR get last character on previous line. |
| 145 | + $lastVarEnding = substr($tokens[$i - 3]['content'], -1); |
| 146 | + if ($tokens[($i + 1)]['code'] !== T_DOC_COMMENT_WHITESPACE) { |
| 147 | + $error = 'Expected 1 space after asterisk; 0 found'; |
| 148 | + $fix = $phpcsFile->addFixableError($error, $i, 'NoSpaceAfterStar'); |
| 149 | + if ($fix === true) { |
| 150 | + $phpcsFile->fixer->addContent($i, ' '); |
| 151 | + } |
| 152 | + } else if ($tokens[($i + 2)]['code'] === T_DOC_COMMENT_TAG |
| 153 | + && $tokens[($i + 1)]['content'] !== ' ' |
| 154 | + //ONGR we allow more spaces after asterisk if tags represented as in array. |
| 155 | + && $lastVarEnding !== '{' |
| 156 | + && $lastVarEnding !== ',' |
| 157 | + ) { |
| 158 | + $error = 'Expected 1 space after asterisk; %s found 1 '.$lastVarEnding; |
| 159 | + $data = array(strlen($tokens[($i + 1)]['content'])); |
| 160 | + $fix = $phpcsFile->addFixableError($error, $i, 'SpaceAfterStar', $data); |
| 161 | + if ($fix === true) { |
| 162 | + $phpcsFile->fixer->replaceToken(($i + 1), ' '); |
| 163 | + } |
| 164 | + } |
| 165 | + }//end for |
| 166 | + |
| 167 | + }//end process() |
| 168 | + |
| 169 | + |
| 170 | +}//end class |
0 commit comments