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 Show 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 Show, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Show implements Renderable |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The Eloquent model to show. |
||
| 27 | * |
||
| 28 | * @var Model |
||
| 29 | */ |
||
| 30 | protected $model; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Show panel builder. |
||
| 34 | * |
||
| 35 | * @var callable |
||
| 36 | */ |
||
| 37 | protected $builder; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Resource path for this show page. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $resource; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Fields to be show. |
||
| 48 | * |
||
| 49 | * @var Collection |
||
| 50 | */ |
||
| 51 | protected $fields; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Relations to be show. |
||
| 55 | * |
||
| 56 | * @var Collection |
||
| 57 | */ |
||
| 58 | protected $relations; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Enable overwrite field and relation. |
||
| 62 | * |
||
| 63 | * @var Overwrite |
||
| 64 | */ |
||
| 65 | protected $overWrite = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Panel |
||
| 69 | */ |
||
| 70 | protected $panel; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Extended fields. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | public static $extendedFields = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Closure |
||
| 81 | */ |
||
| 82 | protected static $initCallback; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Show constructor. |
||
| 86 | * |
||
| 87 | * @param Model $model |
||
| 88 | * @param mixed $builder |
||
| 89 | */ |
||
| 90 | public function __construct($model, $builder = null) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Initialize with user pre-defined default disables, etc. |
||
| 105 | * |
||
| 106 | * @param \Closure $callback |
||
| 107 | */ |
||
| 108 | public static function init(\Closure $callback = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Register custom field. |
||
| 115 | * |
||
| 116 | * @param string $abstract |
||
| 117 | * @param string $class |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public static function extend($abstract, $class) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Initialize the contents to show. |
||
| 128 | */ |
||
| 129 | protected function initContents() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Initialize panel. |
||
| 137 | */ |
||
| 138 | protected function initPanel() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get panel instance. |
||
| 145 | * |
||
| 146 | * @return Panel |
||
| 147 | */ |
||
| 148 | public function panel() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Overwrite properties for field and relation. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function overWrite() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add a model field to show. |
||
| 167 | * |
||
| 168 | * @param string $name |
||
| 169 | * @param string $label |
||
| 170 | * |
||
| 171 | * @return Field |
||
| 172 | */ |
||
| 173 | public function field($name, $label = '') |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Add multiple fields. |
||
| 180 | * |
||
| 181 | * @param array $fields |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function fields(array $fields = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Show all fields. |
||
| 200 | * |
||
| 201 | * @return Show |
||
| 202 | */ |
||
| 203 | public function all() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Add a relation to show. |
||
| 212 | * |
||
| 213 | * @param string $name |
||
| 214 | * @param string|\Closure $label |
||
| 215 | * @param null|\Closure $builder |
||
| 216 | * |
||
| 217 | * @return Relation |
||
| 218 | */ |
||
| 219 | public function relation($name, $label, $builder = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add a model field to show. |
||
| 231 | * |
||
| 232 | * @param string $name |
||
| 233 | * @param string $label |
||
| 234 | * |
||
| 235 | * @return Field |
||
| 236 | */ |
||
| 237 | protected function addField($name, $label = '') |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add a relation panel to show. |
||
| 253 | * |
||
| 254 | * @param string $name |
||
| 255 | * @param \Closure $builder |
||
| 256 | * @param string $label |
||
| 257 | * |
||
| 258 | * @return Relation |
||
| 259 | */ |
||
| 260 | protected function addRelation($name, $builder, $label = '') |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Overwrite existing field. |
||
| 277 | * |
||
| 278 | * @param string $name |
||
| 279 | */ |
||
| 280 | protected function overwriteExistingField($name) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Overwrite existing relation. |
||
| 295 | * |
||
| 296 | * @param string $name |
||
| 297 | */ |
||
| 298 | protected function overwriteExistingRelation($name) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Show a divider. |
||
| 313 | */ |
||
| 314 | public function divider() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set resource path. |
||
| 321 | * |
||
| 322 | * @param string $resource |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setResource($resource) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get resource path. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getResourcePath() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Set field and label width in fields. |
||
| 354 | * |
||
| 355 | * @param int $fieldWidth |
||
| 356 | * @param int $labelWidth |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set the model instance. |
||
| 371 | * |
||
| 372 | * @param Model $model |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function setModel($model) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the model instance being queried. |
||
| 385 | * |
||
| 386 | * @return Model |
||
| 387 | */ |
||
| 388 | public function getModel() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Add field and relation dynamically. |
||
| 395 | * |
||
| 396 | * @param string $method |
||
| 397 | * @param array $arguments |
||
| 398 | * |
||
| 399 | * @return bool|mixed |
||
| 400 | */ |
||
| 401 | public function __call($method, $arguments = []) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Handle the get mutator field. |
||
| 418 | * |
||
| 419 | * @param string $method |
||
| 420 | * @param string $label |
||
| 421 | * |
||
| 422 | * @return bool|Field |
||
| 423 | */ |
||
| 424 | protected function handleGetMutatorField($method, $label) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Handle relation field. |
||
| 439 | * |
||
| 440 | * @param string $method |
||
| 441 | * @param array $arguments |
||
| 442 | * |
||
| 443 | * @return $this|bool|Relation|Field |
||
| 444 | */ |
||
| 445 | protected function handleRelationField($method, $arguments) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param string $relation |
||
| 497 | * @param string $label |
||
| 498 | * |
||
| 499 | * @return Field |
||
| 500 | */ |
||
| 501 | protected function showRelationAsField($relation = '', $label = '') |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Handle model field. |
||
| 508 | * |
||
| 509 | * @param string $method |
||
| 510 | * @param string $label |
||
| 511 | * |
||
| 512 | * @return bool|Field |
||
| 513 | */ |
||
| 514 | protected function handleModelField($method, $label) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Render the show panels. |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function render() |
||
| 552 | } |
||
| 553 |
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..