| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 260 |
| 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 |
||
| 192 | protected function processParams(File $phpcsFile, $stackPtr, $commentStart) |
||
| 193 | { |
||
| 194 | $tokens = $phpcsFile->getTokens(); |
||
| 195 | |||
| 196 | $params = []; |
||
| 197 | $maxType = 0; |
||
| 198 | $maxVar = 0; |
||
| 199 | foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
||
| 200 | if ($tokens[$tag]['content'] !== '@param') { |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | |||
| 204 | $type = ''; |
||
| 205 | $typeSpace = 0; |
||
| 206 | $var = ''; |
||
| 207 | $varSpace = 0; |
||
| 208 | $comment = ''; |
||
| 209 | $commentEnd = 0; |
||
| 210 | $commentTokens = []; |
||
| 211 | |||
| 212 | if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
||
| 213 | $matches = []; |
||
| 214 | preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); |
||
| 215 | |||
| 216 | if (empty($matches) === false) { |
||
| 217 | $typeLen = strlen($matches[1]); |
||
| 218 | $type = trim($matches[1]); |
||
| 219 | $typeSpace = ($typeLen - strlen($type)); |
||
| 220 | $typeLen = strlen($type); |
||
| 221 | if ($typeLen > $maxType) { |
||
| 222 | $maxType = $typeLen; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | if (isset($matches[2]) === true) { |
||
| 227 | $var = $matches[2]; |
||
| 228 | $varLen = strlen($var); |
||
| 229 | if ($varLen > $maxVar) { |
||
| 230 | $maxVar = $varLen; |
||
| 231 | } |
||
| 232 | |||
| 233 | if (isset($matches[4]) === true) { |
||
| 234 | $varSpace = strlen($matches[3]); |
||
| 235 | $comment = $matches[4]; |
||
| 236 | |||
| 237 | // Any strings until the next tag belong to this comment. |
||
| 238 | if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) { |
||
| 239 | $end = $tokens[$commentStart]['comment_tags'][($pos + 1)]; |
||
| 240 | } else { |
||
| 241 | $end = $tokens[$commentStart]['comment_closer']; |
||
| 242 | } |
||
| 243 | |||
| 244 | for ($i = ($tag + 3); $i < $end; $i++) { |
||
| 245 | if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
||
| 246 | $comment .= ' '.$tokens[$i]['content']; |
||
| 247 | $commentEnd = $i; |
||
| 248 | $commentTokens[] = $i; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | $error = 'Missing parameter comment'; |
||
| 253 | $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
||
| 254 | }//end if |
||
| 255 | } else { |
||
| 256 | $error = 'Missing parameter name'; |
||
| 257 | $phpcsFile->addError($error, $tag, 'MissingParamName'); |
||
| 258 | }//end if |
||
| 259 | } else { |
||
| 260 | $error = 'Missing parameter type'; |
||
| 261 | $phpcsFile->addError($error, $tag, 'MissingParamType'); |
||
| 262 | }//end if |
||
| 263 | |||
| 264 | $params[] = [ |
||
| 265 | 'tag' => $tag, |
||
| 266 | 'type' => $type, |
||
| 267 | 'var' => $var, |
||
| 268 | 'comment' => $comment, |
||
| 269 | 'comment_end' => $commentEnd, |
||
| 270 | 'comment_tokens' => $commentTokens, |
||
| 271 | 'type_space' => $typeSpace, |
||
| 272 | 'var_space' => $varSpace, |
||
| 273 | ]; |
||
| 274 | }//end foreach |
||
| 275 | |||
| 276 | $realParams = $phpcsFile->getMethodParameters($stackPtr); |
||
| 277 | $foundParams = []; |
||
| 278 | |||
| 279 | // We want to use ... for all variable length arguments, so add |
||
| 280 | // this prefix to the variable name so comparisons are easier. |
||
| 281 | foreach ($realParams as $pos => $param) { |
||
| 282 | if ($param['variable_length'] === true) { |
||
| 283 | $realParams[$pos]['name'] = '...'.$realParams[$pos]['name']; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | foreach ($params as $pos => $param) { |
||
| 288 | if ($param['var'] === '') { |
||
| 289 | continue; |
||
| 290 | } |
||
| 291 | |||
| 292 | $foundParams[] = $param['var']; |
||
| 293 | |||
| 294 | // Check number of spaces after the type. |
||
| 295 | $spaces = ($maxType - strlen($param['type']) + 1); |
||
| 296 | if ($param['type_space'] !== $spaces) { |
||
| 297 | $error = 'Expected %s spaces after parameter type; %s found'; |
||
| 298 | $data = [ |
||
| 299 | $spaces, |
||
| 300 | $param['type_space'], |
||
| 301 | ]; |
||
| 302 | |||
| 303 | $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data); |
||
| 304 | if ($fix === true) { |
||
| 305 | $commentToken = ($param['tag'] + 2); |
||
| 306 | |||
| 307 | $content = $param['type']; |
||
| 308 | $content .= str_repeat(' ', $spaces); |
||
| 309 | $content .= $param['var']; |
||
| 310 | $content .= str_repeat(' ', $param['var_space']); |
||
| 311 | |||
| 312 | $wrapLength = ($tokens[$commentToken]['length'] - $param['type_space'] - $param['var_space'] - strlen($param['type']) - strlen($param['var'])); |
||
| 313 | |||
| 314 | $star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $param['tag']); |
||
| 315 | $spaceLength = (strlen($content) + $tokens[($commentToken - 1)]['length'] + $tokens[($commentToken - 2)]['length']); |
||
| 316 | |||
| 317 | $padding = str_repeat(' ', ($tokens[$star]['column'] - 1)); |
||
| 318 | $padding .= '* '; |
||
| 319 | $padding .= str_repeat(' ', $spaceLength); |
||
| 320 | |||
| 321 | $content .= wordwrap( |
||
| 322 | $param['comment'], |
||
| 323 | $wrapLength, |
||
| 324 | $phpcsFile->eolChar.$padding |
||
| 325 | ); |
||
| 326 | |||
| 327 | $phpcsFile->fixer->replaceToken($commentToken, $content); |
||
| 328 | for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) { |
||
| 329 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 330 | } |
||
| 331 | }//end if |
||
| 332 | }//end if |
||
| 333 | |||
| 334 | // Make sure the param name is correct. |
||
| 335 | if (isset($realParams[$pos]) === true) { |
||
| 336 | $realName = $realParams[$pos]['name']; |
||
| 337 | if ($realName !== $param['var']) { |
||
| 338 | $code = 'ParamNameNoMatch'; |
||
| 339 | $data = [ |
||
| 340 | $param['var'], |
||
| 341 | $realName, |
||
| 342 | ]; |
||
| 343 | |||
| 344 | $error = 'Doc comment for parameter %s does not match '; |
||
| 345 | if (strtolower($param['var']) === strtolower($realName)) { |
||
| 346 | $error .= 'case of '; |
||
| 347 | $code = 'ParamNameNoCaseMatch'; |
||
| 348 | } |
||
| 349 | |||
| 350 | $error .= 'actual variable name %s'; |
||
| 351 | |||
| 352 | $phpcsFile->addError($error, $param['tag'], $code, $data); |
||
| 353 | } |
||
| 354 | } else if (substr($param['var'], -4) !== ',...') { |
||
| 355 | // We must have an extra parameter comment. |
||
| 356 | $error = 'Superfluous parameter comment'; |
||
| 357 | $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment'); |
||
| 358 | }//end if |
||
| 359 | |||
| 360 | if ($param['comment'] === '') { |
||
| 361 | continue; |
||
| 362 | } |
||
| 363 | |||
| 364 | // Check number of spaces after the param name. |
||
| 365 | $spaces = ($maxVar - strlen($param['var']) + 1); |
||
| 366 | if ($param['var_space'] !== $spaces) { |
||
| 367 | $error = 'Expected %s spaces after parameter name; %s found'; |
||
| 368 | $data = [ |
||
| 369 | $spaces, |
||
| 370 | $param['var_space'], |
||
| 371 | ]; |
||
| 372 | |||
| 373 | $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data); |
||
| 374 | if ($fix === true) { |
||
| 375 | $commentToken = ($param['tag'] + 2); |
||
| 376 | |||
| 377 | $content = $param['type']; |
||
| 378 | $content .= str_repeat(' ', $param['type_space']); |
||
| 379 | $content .= $param['var']; |
||
| 380 | $content .= str_repeat(' ', $spaces); |
||
| 381 | |||
| 382 | $wrapLength = ($tokens[$commentToken]['length'] - $param['type_space'] - $param['var_space'] - strlen($param['type']) - strlen($param['var'])); |
||
| 383 | |||
| 384 | $star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $param['tag']); |
||
| 385 | $spaceLength = (strlen($content) + $tokens[($commentToken - 1)]['length'] + $tokens[($commentToken - 2)]['length']); |
||
| 386 | |||
| 387 | $padding = str_repeat(' ', ($tokens[$star]['column'] - 1)); |
||
| 388 | $padding .= '* '; |
||
| 389 | $padding .= str_repeat(' ', $spaceLength); |
||
| 390 | |||
| 391 | $content .= wordwrap( |
||
| 392 | $param['comment'], |
||
| 393 | $wrapLength, |
||
| 394 | $phpcsFile->eolChar.$padding |
||
| 395 | ); |
||
| 396 | |||
| 397 | $phpcsFile->fixer->replaceToken($commentToken, $content); |
||
| 398 | for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) { |
||
| 399 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 400 | } |
||
| 401 | }//end if |
||
| 402 | }//end if |
||
| 403 | |||
| 404 | // Check the alignment of multi-line param comments. |
||
| 405 | if ($param['tag'] !== $param['comment_end']) { |
||
| 406 | $wrapLength = ($tokens[($param['tag'] + 2)]['length'] - $param['type_space'] - $param['var_space'] - strlen($param['type']) - strlen($param['var'])); |
||
| 407 | |||
| 408 | $startColumn = ($tokens[($param['tag'] + 2)]['column'] + $tokens[($param['tag'] + 2)]['length'] - $wrapLength); |
||
| 409 | |||
| 410 | $star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $param['tag']); |
||
| 411 | $expected = ($startColumn - $tokens[$star]['column'] - 1); |
||
| 412 | |||
| 413 | foreach ($param['comment_tokens'] as $commentToken) { |
||
| 414 | if ($tokens[$commentToken]['column'] === $startColumn) { |
||
| 415 | continue; |
||
| 416 | } |
||
| 417 | |||
| 418 | $found = 0; |
||
| 419 | if ($tokens[($commentToken - 1)]['code'] === T_DOC_COMMENT_WHITESPACE) { |
||
| 420 | $found = $tokens[($commentToken - 1)]['length']; |
||
| 421 | } |
||
| 422 | |||
| 423 | $error = 'Parameter comment not aligned correctly; expected %s spaces but found %s'; |
||
| 424 | $data = [ |
||
| 425 | $expected, |
||
| 426 | $found, |
||
| 427 | ]; |
||
| 428 | $fix = $phpcsFile->addFixableError($error, $commentToken, 'ParamCommentAlignment', $data); |
||
| 429 | if ($fix === true) { |
||
| 430 | $padding = str_repeat(' ', $expected); |
||
| 431 | if ($tokens[($commentToken - 1)]['code'] === T_DOC_COMMENT_WHITESPACE) { |
||
| 432 | $phpcsFile->fixer->replaceToken(($commentToken - 1), $padding); |
||
| 433 | } else { |
||
| 434 | $phpcsFile->fixer->addContentBefore($commentToken, $padding); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | }//end foreach |
||
| 438 | }//end if |
||
| 439 | }//end foreach |
||
| 440 | |||
| 441 | $realNames = []; |
||
| 442 | foreach ($realParams as $realParam) { |
||
| 443 | $realNames[] = $realParam['name']; |
||
| 444 | } |
||
| 445 | |||
| 446 | // Report missing comments. |
||
| 447 | $diff = array_diff($realNames, $foundParams); |
||
| 448 | foreach ($diff as $neededParam) { |
||
| 449 | $error = 'Doc comment for parameter "%s" missing'; |
||
| 450 | $data = [$neededParam]; |
||
| 451 | $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); |
||
| 452 | } |
||
| 458 |