| Conditions | 20 |
| Paths | 68 |
| Total Lines | 157 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 81 | protected function processTokenWithinScope( |
||
| 82 | File $phpcsFile, |
||
| 83 | $stackPtr, |
||
| 84 | $currScope |
||
| 85 | ) { |
||
| 86 | $find = array( |
||
| 87 | T_COMMENT, |
||
| 88 | T_DOC_COMMENT_CLOSE_TAG, |
||
| 89 | T_CLASS, |
||
| 90 | T_CONST, |
||
| 91 | T_FUNCTION, |
||
| 92 | T_VARIABLE, |
||
| 93 | T_OPEN_TAG, |
||
| 94 | ); |
||
| 95 | $tokens = $phpcsFile->getTokens(); |
||
| 96 | |||
| 97 | // Before even checking the docblocks above the current var/const, |
||
| 98 | // check if we have a single line comment after it on the same line, |
||
| 99 | // and if that one is OK. |
||
| 100 | $postComment = $phpcsFile->findNext( |
||
| 101 | array(T_DOC_COMMENT_OPEN_TAG), |
||
| 102 | $stackPtr |
||
| 103 | ); |
||
| 104 | if ($postComment !== false |
||
| 105 | && $tokens[$postComment]['line'] === $tokens[$stackPtr]['line'] |
||
| 106 | ) { |
||
| 107 | if ($tokens[$postComment]['content'] === '/**') { |
||
| 108 | // That's an error already. |
||
| 109 | $phpcsFile->addError( |
||
| 110 | 'no doc blocks are allowed after declaration', |
||
| 111 | $stackPtr, |
||
| 112 | 'NoDocBlockAllowed' |
||
| 113 | ); |
||
| 114 | } else { |
||
| 115 | $postCommentEnd = $tokens[$postComment]['comment_closer']; |
||
| 116 | $postCommentLine = $tokens[$postCommentEnd]['line']; |
||
| 117 | if ($tokens[$postComment]['line'] !== $postCommentLine) { |
||
| 118 | $phpcsFile->addError( |
||
| 119 | 'no multiline comments after declarations allowed', |
||
| 120 | $stackPtr, |
||
| 121 | 'MustBeOneLine' |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Don't do constants for now. |
||
| 128 | if ($tokens[$stackPtr]['code'] === T_CONST) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | |||
| 132 | $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1)); |
||
| 133 | if ($commentEnd === false) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | $commentEnd = (int) $commentEnd; |
||
| 138 | |||
| 139 | $conditions = $tokens[$commentEnd]['conditions']; |
||
| 140 | $lastCondition = array_pop($conditions); |
||
| 141 | if ($lastCondition !== T_CLASS) { |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | $code = $tokens[$commentEnd]['code']; |
||
| 146 | |||
| 147 | if ($code === T_DOC_COMMENT_CLOSE_TAG) { |
||
| 148 | $commentStart = $tokens[$commentEnd]['comment_opener']; |
||
| 149 | |||
| 150 | // Check if this comment is completely in one line, |
||
| 151 | // above the current line, |
||
| 152 | // and has a variable preceding it in the same line. |
||
| 153 | // If yes, it doesn't count. |
||
| 154 | $firstTokenOnLine = $phpcsFile->findFirstOnLine( |
||
| 155 | $this->myTokenTypes, |
||
| 156 | $commentEnd |
||
| 157 | ); |
||
| 158 | if ($firstTokenOnLine !== false |
||
| 159 | && $tokens[$commentStart]['line'] === $tokens[$commentEnd]['line'] |
||
| 160 | && $tokens[$stackPtr]['line'] > $tokens[$commentEnd]['line'] |
||
| 161 | ) { |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | |||
| 165 | $isCommentOneLiner |
||
| 166 | = $tokens[$commentStart]['line'] === $tokens[$commentEnd]['line']; |
||
| 167 | |||
| 168 | $length = ($commentEnd - $commentStart + 1); |
||
| 169 | $tokensAsString = $phpcsFile->getTokensAsString( |
||
| 170 | $commentStart, |
||
| 171 | $length |
||
| 172 | ); |
||
| 173 | |||
| 174 | $varCount = (count(preg_split('/\s+@var\s+/', $tokensAsString)) - 1); |
||
| 175 | if (($varCount === 0) || ($varCount > 1)) { |
||
| 176 | $phpcsFile->addError( |
||
| 177 | 'property doc comment must have exactly one @var annotation', |
||
| 178 | $commentStart, |
||
| 179 | 'MustHaveOneVarAnnotationDefined' |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($varCount === 1) { |
||
| 184 | if ($isCommentOneLiner === true) { |
||
| 185 | $fix = $phpcsFile->addFixableError( |
||
| 186 | 'property doc comment must be multi line', |
||
| 187 | $commentEnd, |
||
| 188 | 'NotMultiLineDocBlock' |
||
| 189 | ); |
||
| 190 | |||
| 191 | if ($fix === true) { |
||
| 192 | $phpcsFile->fixer->beginChangeset(); |
||
| 193 | $phpcsFile->fixer->addContent($commentStart, "\n *"); |
||
| 194 | $phpcsFile->fixer->replaceToken( |
||
| 195 | ($commentEnd - 1), |
||
| 196 | rtrim($tokens[($commentEnd - 1)]['content']) |
||
| 197 | ); |
||
| 198 | $phpcsFile->fixer->addContentBefore($commentEnd, "\n "); |
||
| 199 | $phpcsFile->fixer->endChangeset(); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | if ($isCommentOneLiner === true) { |
||
| 204 | $phpcsFile->addError( |
||
| 205 | 'property doc comment must be multi line', |
||
| 206 | $commentEnd, |
||
| 207 | 'NotMultiLineDocBlock' |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | }//end if |
||
| 211 | } else if ($code === T_COMMENT) { |
||
| 212 | // It seems that when we are in here, |
||
| 213 | // then we have a line comment at $commentEnd. |
||
| 214 | // Now, check if the same comment has |
||
| 215 | // a variable definition on the same line. |
||
| 216 | // If yes, it doesn't count. |
||
| 217 | $firstOnLine = $phpcsFile->findFirstOnLine( |
||
| 218 | $this->myTokenTypes, |
||
| 219 | $commentEnd |
||
| 220 | ); |
||
| 221 | |||
| 222 | if ($firstOnLine === false) { |
||
| 223 | $commentStart = $phpcsFile->findPrevious( |
||
| 224 | T_COMMENT, |
||
| 225 | $commentEnd, |
||
| 226 | null, |
||
| 227 | true |
||
| 228 | ); |
||
| 229 | $phpcsFile->addError( |
||
| 230 | 'property doc comment must begin with /**', |
||
| 231 | ($commentStart + 1), |
||
| 232 | 'NotADocBlock' |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | }//end if |
||
| 236 | |||
| 237 | }//end processTokenWithinScope() |
||
| 238 | |||
| 256 |