| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 180 |
| Code Lines | 105 |
| 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 |
||
| 233 | public function generateFailureMessages(PHP_CodeSniffer_File $file) |
||
| 234 | { |
||
| 235 | $testFile = $file->getFilename(); |
||
| 236 | |||
| 237 | $foundErrors = $file->getErrors(); |
||
| 238 | $foundWarnings = $file->getWarnings(); |
||
| 239 | $expectedErrors = $this->getErrorList(basename($testFile)); |
||
| 240 | $expectedWarnings = $this->getWarningList(basename($testFile)); |
||
| 241 | |||
| 242 | if (is_array($expectedErrors) === false) { |
||
| 243 | throw new PHP_CodeSniffer_Exception('getErrorList() must return an array'); |
||
| 244 | } |
||
| 245 | |||
| 246 | if (is_array($expectedWarnings) === false) { |
||
| 247 | throw new PHP_CodeSniffer_Exception('getWarningList() must return an array'); |
||
| 248 | } |
||
| 249 | |||
| 250 | /* |
||
| 251 | We merge errors and warnings together to make it easier |
||
| 252 | to iterate over them and produce the errors string. In this way, |
||
| 253 | we can report on errors and warnings in the same line even though |
||
| 254 | it's not really structured to allow that. |
||
| 255 | */ |
||
| 256 | |||
| 257 | $allProblems = array(); |
||
| 258 | $failureMessages = array(); |
||
| 259 | |||
| 260 | foreach ($foundErrors as $line => $lineErrors) { |
||
| 261 | foreach ($lineErrors as $column => $errors) { |
||
| 262 | if (isset($allProblems[$line]) === false) { |
||
| 263 | $allProblems[$line] = array( |
||
| 264 | 'expected_errors' => 0, |
||
| 265 | 'expected_warnings' => 0, |
||
| 266 | 'found_errors' => array(), |
||
| 267 | 'found_warnings' => array(), |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | |||
| 271 | $foundErrorsTemp = array(); |
||
| 272 | foreach ($allProblems[$line]['found_errors'] as $foundError) { |
||
| 273 | $foundErrorsTemp[] = $foundError; |
||
| 274 | } |
||
| 275 | |||
| 276 | $errorsTemp = array(); |
||
| 277 | foreach ($errors as $foundError) { |
||
| 278 | $errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')'; |
||
| 279 | } |
||
| 280 | |||
| 281 | $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp); |
||
| 282 | }//end foreach |
||
| 283 | |||
| 284 | if (isset($expectedErrors[$line]) === true) { |
||
| 285 | $allProblems[$line]['expected_errors'] = $expectedErrors[$line]; |
||
| 286 | } else { |
||
| 287 | $allProblems[$line]['expected_errors'] = 0; |
||
| 288 | } |
||
| 289 | |||
| 290 | unset($expectedErrors[$line]); |
||
| 291 | }//end foreach |
||
| 292 | |||
| 293 | foreach ($expectedErrors as $line => $numErrors) { |
||
| 294 | if (isset($allProblems[$line]) === false) { |
||
| 295 | $allProblems[$line] = array( |
||
| 296 | 'expected_errors' => 0, |
||
| 297 | 'expected_warnings' => 0, |
||
| 298 | 'found_errors' => array(), |
||
| 299 | 'found_warnings' => array(), |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | |||
| 303 | $allProblems[$line]['expected_errors'] = $numErrors; |
||
| 304 | } |
||
| 305 | |||
| 306 | foreach ($foundWarnings as $line => $lineWarnings) { |
||
| 307 | foreach ($lineWarnings as $column => $warnings) { |
||
| 308 | if (isset($allProblems[$line]) === false) { |
||
| 309 | $allProblems[$line] = array( |
||
| 310 | 'expected_errors' => 0, |
||
| 311 | 'expected_warnings' => 0, |
||
| 312 | 'found_errors' => array(), |
||
| 313 | 'found_warnings' => array(), |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | $foundWarningsTemp = array(); |
||
| 318 | foreach ($allProblems[$line]['found_warnings'] as $foundWarning) { |
||
| 319 | $foundWarningsTemp[] = $foundWarning; |
||
| 320 | } |
||
| 321 | |||
| 322 | $warningsTemp = array(); |
||
| 323 | foreach ($warnings as $warning) { |
||
| 324 | $warningsTemp[] = $warning['message'].' ('.$warning['source'].')'; |
||
| 325 | } |
||
| 326 | |||
| 327 | $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp); |
||
| 328 | }//end foreach |
||
| 329 | |||
| 330 | if (isset($expectedWarnings[$line]) === true) { |
||
| 331 | $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line]; |
||
| 332 | } else { |
||
| 333 | $allProblems[$line]['expected_warnings'] = 0; |
||
| 334 | } |
||
| 335 | |||
| 336 | unset($expectedWarnings[$line]); |
||
| 337 | }//end foreach |
||
| 338 | |||
| 339 | foreach ($expectedWarnings as $line => $numWarnings) { |
||
| 340 | if (isset($allProblems[$line]) === false) { |
||
| 341 | $allProblems[$line] = array( |
||
| 342 | 'expected_errors' => 0, |
||
| 343 | 'expected_warnings' => 0, |
||
| 344 | 'found_errors' => array(), |
||
| 345 | 'found_warnings' => array(), |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | |||
| 349 | $allProblems[$line]['expected_warnings'] = $numWarnings; |
||
| 350 | } |
||
| 351 | |||
| 352 | // Order the messages by line number. |
||
| 353 | ksort($allProblems); |
||
| 354 | |||
| 355 | foreach ($allProblems as $line => $problems) { |
||
| 356 | $numErrors = count($problems['found_errors']); |
||
| 357 | $numWarnings = count($problems['found_warnings']); |
||
| 358 | $expectedErrors = $problems['expected_errors']; |
||
| 359 | $expectedWarnings = $problems['expected_warnings']; |
||
| 360 | |||
| 361 | $errors = ''; |
||
| 362 | $foundString = ''; |
||
| 363 | |||
| 364 | if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) { |
||
| 365 | $lineMessage = "[LINE $line]"; |
||
| 366 | $expectedMessage = 'Expected '; |
||
| 367 | $foundMessage = 'in '.basename($testFile).' but found '; |
||
| 368 | |||
| 369 | if ($expectedErrors !== $numErrors) { |
||
| 370 | $expectedMessage .= "$expectedErrors error(s)"; |
||
| 371 | $foundMessage .= "$numErrors error(s)"; |
||
| 372 | if ($numErrors !== 0) { |
||
| 373 | $foundString .= 'error(s)'; |
||
| 374 | $errors .= implode(PHP_EOL.' -> ', $problems['found_errors']); |
||
| 375 | } |
||
| 376 | |||
| 377 | if ($expectedWarnings !== $numWarnings) { |
||
| 378 | $expectedMessage .= ' and '; |
||
| 379 | $foundMessage .= ' and '; |
||
| 380 | if ($numWarnings !== 0) { |
||
| 381 | if ($foundString !== '') { |
||
| 382 | $foundString .= ' and '; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | if ($expectedWarnings !== $numWarnings) { |
||
| 389 | $expectedMessage .= "$expectedWarnings warning(s)"; |
||
| 390 | $foundMessage .= "$numWarnings warning(s)"; |
||
| 391 | if ($numWarnings !== 0) { |
||
| 392 | $foundString .= 'warning(s)'; |
||
| 393 | if (empty($errors) === false) { |
||
| 394 | $errors .= PHP_EOL.' -> '; |
||
| 395 | } |
||
| 396 | |||
| 397 | $errors .= implode(PHP_EOL.' -> ', $problems['found_warnings']); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | $fullMessage = "$lineMessage $expectedMessage $foundMessage."; |
||
| 402 | if ($errors !== '') { |
||
| 403 | $fullMessage .= " The $foundString found were:".PHP_EOL." -> $errors"; |
||
| 404 | } |
||
| 405 | |||
| 406 | $failureMessages[] = $fullMessage; |
||
| 407 | }//end if |
||
| 408 | }//end foreach |
||
| 409 | |||
| 410 | return $failureMessages; |
||
| 411 | |||
| 412 | }//end generateFailureMessages() |
||
| 413 | |||
| 456 |