| Conditions | 27 |
| Paths | 17856 |
| Total Lines | 180 |
| Code Lines | 117 |
| Lines | 87 |
| Ratio | 48.33 % |
| 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 |
||
| 209 | protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) |
||
| 210 | { |
||
| 211 | $tokens = $phpcsFile->getTokens(); |
||
| 212 | |||
| 213 | $params = array(); |
||
| 214 | $maxType = 0; |
||
| 215 | $maxVar = 0; |
||
| 216 | foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
||
| 217 | if ($tokens[$tag]['content'] !== '@param') { |
||
| 218 | continue; |
||
| 219 | } |
||
| 220 | |||
| 221 | $type = ''; |
||
| 222 | $typeSpace = 0; |
||
| 223 | $var = ''; |
||
| 224 | $varSpace = 0; |
||
| 225 | $comment = ''; |
||
| 226 | if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { |
||
| 227 | $matches = array(); |
||
| 228 | preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); |
||
| 229 | |||
| 230 | if (empty($matches) === false) { |
||
| 231 | $typeLen = strlen($matches[1]); |
||
| 232 | $type = trim($matches[1]); |
||
| 233 | $typeSpace = ($typeLen - strlen($type)); |
||
| 234 | $typeLen = strlen($type); |
||
| 235 | if ($typeLen > $maxType) { |
||
| 236 | $maxType = $typeLen; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | if (isset($matches[2]) === true) { |
||
| 241 | $var = $matches[2]; |
||
| 242 | $varLen = strlen($var); |
||
| 243 | if ($varLen > $maxVar) { |
||
| 244 | $maxVar = $varLen; |
||
| 245 | } |
||
| 246 | |||
| 247 | if (isset($matches[4]) === true) { |
||
| 248 | $varSpace = strlen($matches[3]); |
||
| 249 | $comment = $matches[4]; |
||
| 250 | |||
| 251 | // Any strings until the next tag belong to this comment. |
||
| 252 | if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) { |
||
| 253 | $end = $tokens[$commentStart]['comment_tags'][($pos + 1)]; |
||
| 254 | } else { |
||
| 255 | $end = $tokens[$commentStart]['comment_closer']; |
||
| 256 | } |
||
| 257 | |||
| 258 | for ($i = ($tag + 3); $i < $end; $i++) { |
||
| 259 | if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { |
||
| 260 | $comment .= ' '.$tokens[$i]['content']; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } else { |
||
| 264 | $error = 'Missing parameter comment'; |
||
| 265 | $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | $error = 'Missing parameter name'; |
||
| 269 | $phpcsFile->addError($error, $tag, 'MissingParamName'); |
||
| 270 | }//end if |
||
| 271 | } else { |
||
| 272 | $error = 'Missing parameter type'; |
||
| 273 | $phpcsFile->addError($error, $tag, 'MissingParamType'); |
||
| 274 | }//end if |
||
| 275 | |||
| 276 | $params[] = array( |
||
| 277 | 'tag' => $tag, |
||
| 278 | 'type' => $type, |
||
| 279 | 'var' => $var, |
||
| 280 | 'comment' => $comment, |
||
| 281 | 'type_space' => $typeSpace, |
||
| 282 | 'var_space' => $varSpace, |
||
| 283 | ); |
||
| 284 | }//end foreach |
||
| 285 | |||
| 286 | $realParams = $phpcsFile->getMethodParameters($stackPtr); |
||
| 287 | $foundParams = array(); |
||
| 288 | |||
| 289 | // We want to use ... for all variable length arguments, so added |
||
| 290 | // this prefix to the variable name so comparisons are easier. |
||
| 291 | foreach ($realParams as $pos => $param) { |
||
| 292 | if ($param['variable_length'] === true) { |
||
| 293 | $realParams[$pos]['name'] = '...'.$realParams[$pos]['name']; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | foreach ($params as $pos => $param) { |
||
| 298 | if ($param['var'] === '') { |
||
| 299 | continue; |
||
| 300 | } |
||
| 301 | |||
| 302 | $foundParams[] = $param['var']; |
||
| 303 | |||
| 304 | // Check number of spaces after the type. |
||
| 305 | $spaces = ($maxType - strlen($param['type']) + 1); |
||
| 306 | if ($param['type_space'] !== $spaces) { |
||
| 307 | $error = 'Expected %s spaces after parameter type; %s found'; |
||
| 308 | $data = array( |
||
| 309 | $spaces, |
||
| 310 | $param['type_space'], |
||
| 311 | ); |
||
| 312 | |||
| 313 | $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data); |
||
| 314 | if ($fix === true) { |
||
| 315 | $content = $param['type']; |
||
| 316 | $content .= str_repeat(' ', $spaces); |
||
| 317 | $content .= $param['var']; |
||
| 318 | $content .= str_repeat(' ', $param['var_space']); |
||
| 319 | $content .= $param['comment']; |
||
| 320 | $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | // Make sure the param name is correct. |
||
| 325 | if (isset($realParams[$pos]) === true) { |
||
| 326 | $realName = $realParams[$pos]['name']; |
||
| 327 | if ($realName !== $param['var']) { |
||
| 328 | $code = 'ParamNameNoMatch'; |
||
| 329 | $data = array( |
||
| 330 | $param['var'], |
||
| 331 | $realName, |
||
| 332 | ); |
||
| 333 | |||
| 334 | $error = 'Doc comment for parameter %s does not match '; |
||
| 335 | if (strtolower($param['var']) === strtolower($realName)) { |
||
| 336 | $error .= 'case of '; |
||
| 337 | $code = 'ParamNameNoCaseMatch'; |
||
| 338 | } |
||
| 339 | |||
| 340 | $error .= 'actual variable name %s'; |
||
| 341 | |||
| 342 | $phpcsFile->addError($error, $param['tag'], $code, $data); |
||
| 343 | } |
||
| 344 | } else if (substr($param['var'], -4) !== ',...') { |
||
| 345 | // We must have an extra parameter comment. |
||
| 346 | $error = 'Superfluous parameter comment'; |
||
| 347 | $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment'); |
||
| 348 | }//end if |
||
| 349 | |||
| 350 | if ($param['comment'] === '') { |
||
| 351 | continue; |
||
| 352 | } |
||
| 353 | |||
| 354 | // Check number of spaces after the var name. |
||
| 355 | $spaces = ($maxVar - strlen($param['var']) + 1); |
||
| 356 | if ($param['var_space'] !== $spaces) { |
||
| 357 | $error = 'Expected %s spaces after parameter name; %s found'; |
||
| 358 | $data = array( |
||
| 359 | $spaces, |
||
| 360 | $param['var_space'], |
||
| 361 | ); |
||
| 362 | |||
| 363 | $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data); |
||
| 364 | if ($fix === true) { |
||
| 365 | $content = $param['type']; |
||
| 366 | $content .= str_repeat(' ', $param['type_space']); |
||
| 367 | $content .= $param['var']; |
||
| 368 | $content .= str_repeat(' ', $spaces); |
||
| 369 | $content .= $param['comment']; |
||
| 370 | $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | }//end foreach |
||
| 374 | |||
| 375 | $realNames = array(); |
||
| 376 | foreach ($realParams as $realParam) { |
||
| 377 | $realNames[] = $realParam['name']; |
||
| 378 | } |
||
| 379 | |||
| 380 | // Report missing comments. |
||
| 381 | $diff = array_diff($realNames, $foundParams); |
||
| 382 | foreach ($diff as $neededParam) { |
||
| 383 | $error = 'Doc comment for parameter "%s" missing'; |
||
| 384 | $data = array($neededParam); |
||
| 385 | $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data); |
||
| 386 | } |
||
| 387 | |||
| 388 | }//end processParams() |
||
| 389 | |||
| 392 |