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 Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Builder implements LoggerAwareInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $buildPath; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string[] |
||
| 28 | */ |
||
| 29 | public $ignore = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string[] |
||
| 33 | */ |
||
| 34 | public $binaryPath = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string[] |
||
| 38 | */ |
||
| 39 | public $priorityPath = 'local'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $directory; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|null |
||
| 48 | */ |
||
| 49 | protected $currentStage = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $verbose = true; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \PHPCensor\Model\Build |
||
| 58 | */ |
||
| 59 | protected $build; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var LoggerInterface |
||
| 63 | */ |
||
| 64 | protected $logger; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $config = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $lastOutput; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var BuildInterpolator |
||
| 78 | */ |
||
| 79 | protected $interpolator; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \Fabrica\Tools\Store\BuildStore |
||
| 83 | */ |
||
| 84 | protected $store; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var \PHPCensor\Plugin\Util\Executor |
||
| 88 | */ |
||
| 89 | protected $pluginExecutor; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var Helper\CommandExecutorInterface |
||
| 93 | */ |
||
| 94 | protected $commandExecutor; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Logging\BuildLogger |
||
| 98 | */ |
||
| 99 | protected $buildLogger; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var BuildErrorWriter |
||
| 103 | */ |
||
| 104 | private $buildErrorWriter; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set up the builder. |
||
| 108 | * |
||
| 109 | * @param \PHPCensor\Model\Build $build |
||
| 110 | * @param LoggerInterface $logger |
||
| 111 | */ |
||
| 112 | public function __construct(Build $build, LoggerInterface $logger = null) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return BuildLogger |
||
| 134 | */ |
||
| 135 | public function getBuildLogger() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return null|string |
||
| 142 | */ |
||
| 143 | public function getCurrentStage() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the config array, as read from .php-censor.yml |
||
| 150 | * |
||
| 151 | * @param array $config |
||
| 152 | * |
||
| 153 | * @throws \Exception |
||
| 154 | */ |
||
| 155 | public function setConfig(array $config) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Access a variable from the .php-censor.yml file. |
||
| 162 | * |
||
| 163 | * @param string $key |
||
| 164 | * |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | public function getConfig($key = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Access a variable from the config.yml |
||
| 181 | * |
||
| 182 | * @param string $key |
||
| 183 | * |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | public function getSystemConfig($key) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string The title of the project being built. |
||
| 193 | * |
||
| 194 | * @throws Exception\HttpException |
||
| 195 | */ |
||
| 196 | public function getBuildProjectTitle() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @throws Exception\HttpException |
||
| 203 | * @throws Exception\InvalidArgumentException |
||
| 204 | */ |
||
| 205 | public function execute() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @throws Exception\HttpException |
||
| 308 | * @throws Exception\InvalidArgumentException |
||
| 309 | */ |
||
| 310 | protected function setErrorTrend() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Used by this class, and plugins, to execute shell commands. |
||
| 336 | * |
||
| 337 | * @param array ...$params |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function executeCommand(...$params) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns the output from the last command run. |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getLastOutput() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Specify whether exec output should be logged. |
||
| 358 | * |
||
| 359 | * @param bool $enableLog |
||
| 360 | */ |
||
| 361 | public function logExecOutput($enableLog = true) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Find a binary required by a plugin. |
||
| 368 | * |
||
| 369 | * @param array|string $binary |
||
| 370 | * @param string $priorityPath |
||
| 371 | * @param string $binaryPath |
||
| 372 | * @param array $binaryName |
||
| 373 | * @return string |
||
| 374 | * |
||
| 375 | * @throws \Exception when no binary has been found. |
||
| 376 | */ |
||
| 377 | public function findBinary($binary, $priorityPath = 'local', $binaryPath = '', $binaryName = []) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Replace every occurrence of the interpolation vars in the given string |
||
| 384 | * Example: "This is build %PHPCI_BUILD%" => "This is build 182" |
||
| 385 | * |
||
| 386 | * @param string $input |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function interpolate($input) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set up a working copy of the project for building. |
||
| 397 | * |
||
| 398 | * @throws \Exception |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | protected function setupBuild() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Sets a logger instance on the object |
||
| 468 | * |
||
| 469 | * @param LoggerInterface $logger |
||
| 470 | */ |
||
| 471 | public function setLogger(LoggerInterface $logger) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Write to the build log. |
||
| 478 | * |
||
| 479 | * @param string $message |
||
| 480 | * @param string $level |
||
| 481 | * @param array $context |
||
| 482 | */ |
||
| 483 | public function log($message, $level = LogLevel::INFO, $context = []) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Add a warning-coloured message to the log. |
||
| 490 | * |
||
| 491 | * @param string $message |
||
| 492 | */ |
||
| 493 | public function logWarning($message) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Add a success-coloured message to the log. |
||
| 500 | * |
||
| 501 | * @param string $message |
||
| 502 | */ |
||
| 503 | public function logSuccess($message) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Add a failure-coloured message to the log. |
||
| 510 | * |
||
| 511 | * @param string $message |
||
| 512 | * @param \Exception $exception The exception that caused the error. |
||
| 513 | */ |
||
| 514 | public function logFailure($message, \Exception $exception = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Add a debug-coloured message to the log. |
||
| 521 | * |
||
| 522 | * @param string $message |
||
| 523 | */ |
||
| 524 | public function logDebug($message) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns a configured instance of the plugin factory. |
||
| 531 | * |
||
| 532 | * @param Build $build |
||
| 533 | * |
||
| 534 | * @return PluginFactory |
||
| 535 | */ |
||
| 536 | private function buildPluginFactory(Build $build) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return BuildErrorWriter |
||
| 580 | */ |
||
| 581 | public function getBuildErrorWriter() |
||
| 585 | } |
||
| 586 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..