Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BaseCoverFishValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseCoverFishValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class BaseCoverFishValidator implements BaseCoverFishValidatorInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var ArrayCollection |
||
| 28 | */ |
||
| 29 | protected $validatorCollection; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $coversToken = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $result = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var CoverFishHelper |
||
| 43 | */ |
||
| 44 | protected $coverFishHelper; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function getResult() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @codeCoverageIgnore |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getValidationInfo() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @codeCoverageIgnore |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getValidationTag() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @codeCoverageIgnore |
||
| 80 | * |
||
| 81 | * @param CoverFishPHPUnitFile $phpUnitFile |
||
| 82 | * |
||
| 83 | * @return CoverFishMapping |
||
| 84 | */ |
||
| 85 | public function getMapping(CoverFishPHPUnitFile $phpUnitFile) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $mappingOptions |
||
| 92 | * |
||
| 93 | * @return CoverFishMapping |
||
| 94 | */ |
||
| 95 | public function setMapping(array $mappingOptions) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function validate() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param CoverFishMapping $coverMapping |
||
| 120 | * @param CoverFishResult $coverFishResult |
||
| 121 | * |
||
| 122 | * @return CoverFishResult |
||
| 123 | */ |
||
| 124 | public function validateDefaultCoverClassMapping(CoverFishMapping $coverMapping, CoverFishResult $coverFishResult) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @todo: mappingResult could be false, set a specific/real coverFishError here ... |
||
| 140 | * |
||
| 141 | * @param CoverFishMapping $coverMapping |
||
| 142 | * @param CoverFishResult $coverFishResult |
||
| 143 | * |
||
| 144 | * @return CoverFishResult |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function validateClassFQNMapping(CoverFishMapping $coverMapping, CoverFishResult $coverFishResult) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param CoverFishMapping $coverMapping |
||
| 161 | * @param CoverFishResult $coverFishResult |
||
| 162 | * |
||
| 163 | * @return CoverFishResult |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function validateClassAccessorVisibility(CoverFishMapping $coverMapping, CoverFishResult $coverFishResult) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param CoverFishMapping $coverMapping |
||
| 180 | * @param CoverFishResult $coverFishResult |
||
| 181 | * |
||
| 182 | * @return CoverFishResult |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function validateClassMethod(CoverFishMapping $coverMapping, CoverFishResult $coverFishResult) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param CoverFishMapping $coverMapping |
||
| 199 | * @param CoverFishResult $coverFishResult |
||
| 200 | * |
||
| 201 | * @return CoverFishResult |
||
| 202 | */ |
||
| 203 | public function validateEmptyDocBlock(CoverFishMapping $coverMapping, CoverFishResult $coverFishResult) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * main validator mapping "engine", if any of our cover validator checks will fail, |
||
| 217 | * return corresponding result immediately ... |
||
| 218 | * |
||
| 219 | * @param CoverFishMapping $coverMapping |
||
| 220 | * |
||
| 221 | * @return CoverFishResult |
||
| 222 | */ |
||
| 223 | public function validateMapping(CoverFishMapping $coverMapping) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param CoverFishResult $coverFishResult |
||
| 243 | * |
||
| 244 | * @return CoverFishResult |
||
| 245 | */ |
||
| 246 | public function prepareCoverFishResult(CoverFishResult $coverFishResult) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $classFQN |
||
| 256 | * |
||
| 257 | * @return CoverFishMessageError|\ReflectionClass |
||
| 258 | */ |
||
| 259 | public function validateReflectionClass($classFQN) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $classFQN |
||
| 266 | * @param string $method |
||
| 267 | * |
||
| 268 | * @return CoverFishMessageError|\ReflectionMethod|false |
||
| 269 | */ |
||
| 270 | public function validateReflectionMethod($classFQN, $method) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param \ReflectionClass $reflectionClass |
||
| 287 | * |
||
| 288 | * @return bool|CoverFishMessageError |
||
| 289 | */ |
||
| 290 | View Code Duplication | public function validateReflectionClassForAccessorPublic(\ReflectionClass $reflectionClass) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param \ReflectionClass $reflectionClass |
||
| 303 | * |
||
| 304 | * @return bool|CoverFishMessageError |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function validateReflectionClassForAccessorNotPublic(\ReflectionClass $reflectionClass) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param \ReflectionClass $reflectionClass |
||
| 324 | * |
||
| 325 | * @return bool|CoverFishMessageError |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function validateReflectionClassForAccessorProtected(\ReflectionClass $reflectionClass) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param \ReflectionClass $reflectionClass |
||
| 340 | * |
||
| 341 | * @return bool|CoverFishMessageError |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function validateReflectionClassForAccessorNotProtected(\ReflectionClass $reflectionClass) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param \ReflectionClass $reflectionClass |
||
| 361 | * |
||
| 362 | * @return bool|CoverFishMessageError |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function validateReflectionClassForAccessorPrivate(\ReflectionClass $reflectionClass) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param \ReflectionClass $reflectionClass |
||
| 377 | * |
||
| 378 | * @return bool|CoverFishMessageError |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function validateReflectionClassForAccessorNotPrivate(\ReflectionClass $reflectionClass) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $classFQN |
||
| 398 | * @param string $accessor |
||
| 399 | * |
||
| 400 | * @return bool|CoverFishMessageError |
||
| 401 | */ |
||
| 402 | public function validateReflectionClassForAccessorVisibility($classFQN, $accessor) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param CoverFishResult $coverFishResult |
||
| 449 | * |
||
| 450 | * @return CoverFishResult |
||
| 451 | */ |
||
| 452 | public function clearValidationErrors(CoverFishResult $coverFishResult) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param CoverFishResult $coverFishResult |
||
| 462 | * |
||
| 463 | * @return CoverFishResult |
||
| 464 | */ |
||
| 465 | public function clearValidationWarnings(CoverFishResult $coverFishResult) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param CoverFishResult $coverFishResult |
||
| 475 | * @param int $errorCode |
||
| 476 | * @param string|null $errorMessage |
||
| 477 | * |
||
| 478 | * @return CoverFishResult |
||
| 479 | */ |
||
| 480 | public function setValidationError(CoverFishResult $coverFishResult, $errorCode, $errorMessage = null) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param CoverFishResult $coverFishResult |
||
| 495 | * @param int $warningCode |
||
| 496 | * @param string|null $warningMessage |
||
| 497 | * |
||
| 498 | * @return CoverFishResult |
||
| 499 | */ |
||
| 500 | public function setValidationWarning(CoverFishResult $coverFishResult, $warningCode, $warningMessage = null) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $classFQN |
||
| 509 | * |
||
| 510 | * @return CoverFishMessageError|\ReflectionClass |
||
| 511 | */ |
||
| 512 | public function getReflectionClass($classFQN) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $coversToken |
||
| 525 | */ |
||
| 526 | public function __construct($coversToken) |
||
| 532 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.