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