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 Column 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 Column, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Column |
||
39 | { |
||
40 | const SELECT_COLUMN_NAME = '__row_selector__'; |
||
41 | |||
42 | const ACTION_COLUMN_NAME = '__actions__'; |
||
43 | |||
44 | /** |
||
45 | * @var Grid |
||
46 | */ |
||
47 | protected $grid; |
||
48 | |||
49 | /** |
||
50 | * Name of column. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $name; |
||
55 | |||
56 | /** |
||
57 | * Label of column. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $label; |
||
62 | |||
63 | /** |
||
64 | * Original value of column. |
||
65 | * |
||
66 | * @var mixed |
||
67 | */ |
||
68 | protected $original; |
||
69 | |||
70 | /** |
||
71 | * Is column sortable. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $sortable = false; |
||
76 | |||
77 | /** |
||
78 | * Sort arguments. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $sort; |
||
83 | |||
84 | /** |
||
85 | * Help message. |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $help = ''; |
||
90 | |||
91 | /** |
||
92 | * Cast Name. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $cast; |
||
97 | |||
98 | /** |
||
99 | * Attributes of column. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $attributes = []; |
||
104 | |||
105 | /** |
||
106 | * Relation name. |
||
107 | * |
||
108 | * @var bool |
||
109 | */ |
||
110 | protected $relation = false; |
||
111 | |||
112 | /** |
||
113 | * Relation column. |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $relationColumn; |
||
118 | |||
119 | /** |
||
120 | * Original grid data. |
||
121 | * |
||
122 | * @var Collection |
||
123 | */ |
||
124 | protected static $originalGridModels; |
||
125 | |||
126 | /** |
||
127 | * @var []Closure |
||
128 | */ |
||
129 | protected $displayCallbacks = []; |
||
130 | |||
131 | /** |
||
132 | * Displayers for grid column. |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | public static $displayers = []; |
||
137 | |||
138 | /** |
||
139 | * Defined columns. |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | public static $defined = []; |
||
144 | |||
145 | /** |
||
146 | * @var array |
||
147 | */ |
||
148 | protected static $htmlAttributes = []; |
||
149 | |||
150 | /** |
||
151 | * @var Model |
||
152 | */ |
||
153 | protected static $model; |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @param string $label |
||
158 | */ |
||
159 | public function __construct($name, $label) |
||
165 | |||
166 | /** |
||
167 | * Extend column displayer. |
||
168 | * |
||
169 | * @param $name |
||
170 | * @param $displayer |
||
171 | */ |
||
172 | public static function extend($name, $displayer) |
||
176 | |||
177 | /** |
||
178 | * Define a column globally. |
||
179 | * |
||
180 | * @param string $name |
||
181 | * @param mixed $definition |
||
182 | */ |
||
183 | public static function define($name, $definition) |
||
187 | |||
188 | /** |
||
189 | * Set grid instance for column. |
||
190 | * |
||
191 | * @param Grid $grid |
||
192 | */ |
||
193 | public function setGrid(Grid $grid) |
||
199 | |||
200 | /** |
||
201 | * Set model for column. |
||
202 | * |
||
203 | * @param $model |
||
204 | */ |
||
205 | public function setModel($model) |
||
211 | |||
212 | /** |
||
213 | * Set original data for column. |
||
214 | * |
||
215 | * @param Collection $collection |
||
216 | */ |
||
217 | public static function setOriginalGridModels(Collection $collection) |
||
221 | |||
222 | /** |
||
223 | * Set column attributes. |
||
224 | * |
||
225 | * @param array $attributes |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setAttributes($attributes = []) |
||
235 | |||
236 | /** |
||
237 | * Get column attributes. |
||
238 | * |
||
239 | * @param string $name |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public static function getAttributes($name) |
||
247 | |||
248 | /** |
||
249 | * Set style of this column. |
||
250 | * |
||
251 | * @param string $style |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function style($style) |
||
259 | |||
260 | /** |
||
261 | * Set the width of column. |
||
262 | * |
||
263 | * @param int $width |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function width(int $width) |
||
271 | |||
272 | /** |
||
273 | * Set the color of column. |
||
274 | * |
||
275 | * @param string $color |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function color($color) |
||
283 | |||
284 | /** |
||
285 | * Get name of this column. |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | public function getName() |
||
293 | |||
294 | /** |
||
295 | * Format label. |
||
296 | * |
||
297 | * @param $label |
||
298 | * |
||
299 | * @return mixed |
||
300 | */ |
||
301 | protected function formatLabel($label) |
||
311 | |||
312 | /** |
||
313 | * Get label of the column. |
||
314 | * |
||
315 | * @return mixed |
||
316 | */ |
||
317 | public function getLabel() |
||
321 | |||
322 | /** |
||
323 | * Set relation. |
||
324 | * |
||
325 | * @param string $relation |
||
326 | * @param string $relationColumn |
||
327 | * |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function setRelation($relation, $relationColumn = null) |
||
337 | |||
338 | /** |
||
339 | * If this column is relation column. |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function isRelation() |
||
347 | |||
348 | /** |
||
349 | * Set sort value. |
||
350 | * |
||
351 | * @param bool $sort |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function sort($sort) |
||
361 | |||
362 | /** |
||
363 | * Mark this column as sortable. |
||
364 | * |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function sortable() |
||
371 | |||
372 | /** |
||
373 | * Set cast name for sortable. |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | public function cast($cast) |
||
383 | |||
384 | /** |
||
385 | * Add a display callback. |
||
386 | * |
||
387 | * @param Closure $callback |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | public function display(Closure $callback) |
||
397 | |||
398 | /** |
||
399 | * Display using display abstract. |
||
400 | * |
||
401 | * @param string $abstract |
||
402 | * @param array $arguments |
||
403 | * |
||
404 | * @return $this |
||
405 | */ |
||
406 | public function displayUsing($abstract, $arguments = []) |
||
419 | |||
420 | /** |
||
421 | * Display column using array value map. |
||
422 | * |
||
423 | * @param array $values |
||
424 | * @param null $default |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | View Code Duplication | public function using(array $values, $default = null) |
|
438 | |||
439 | /** |
||
440 | * Render this column with the given view. |
||
441 | * |
||
442 | * @param string $view |
||
443 | * |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function view($view) |
||
454 | |||
455 | /** |
||
456 | * Hide this column by default. |
||
457 | * |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function hide() |
||
466 | |||
467 | /** |
||
468 | * Add column to total-row. |
||
469 | * |
||
470 | * @param null $display |
||
471 | * |
||
472 | * @return $this |
||
473 | */ |
||
474 | public function totalRow($display = null) |
||
480 | |||
481 | /** |
||
482 | * Convert file size to a human readable format like `100mb`. |
||
483 | * |
||
484 | * @return $this |
||
485 | */ |
||
486 | public function filesize() |
||
492 | |||
493 | /** |
||
494 | * Display the fields in the email format as gavatar. |
||
495 | * |
||
496 | * @param int $size |
||
497 | * |
||
498 | * @return $this |
||
499 | */ |
||
500 | public function gravatar($size = 30) |
||
512 | |||
513 | /** |
||
514 | * Display field as a loading icon. |
||
515 | * |
||
516 | * @param array $values |
||
517 | * @param array $others |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function loading($values = [], $others = []) |
||
534 | |||
535 | /** |
||
536 | * Display column as an font-awesome icon based on it's value. |
||
537 | * |
||
538 | * @param array $setting |
||
539 | * @param string $default |
||
540 | * |
||
541 | * @return $this |
||
542 | */ |
||
543 | public function icon(array $setting, $default = '') |
||
558 | |||
559 | /** |
||
560 | * Return a human readable format time. |
||
561 | * |
||
562 | * @param null $locale |
||
563 | * |
||
564 | * @return $this |
||
565 | */ |
||
566 | public function diffForHumans($locale = null) |
||
576 | |||
577 | /** |
||
578 | * If has display callbacks. |
||
579 | * |
||
580 | * @return bool |
||
581 | */ |
||
582 | protected function hasDisplayCallbacks() |
||
586 | |||
587 | /** |
||
588 | * Call all of the "display" callbacks column. |
||
589 | * |
||
590 | * @param mixed $value |
||
591 | * @param int $key |
||
592 | * |
||
593 | * @return mixed |
||
594 | */ |
||
595 | protected function callDisplayCallbacks($value, $key) |
||
613 | |||
614 | /** |
||
615 | * Set original grid data to column. |
||
616 | * |
||
617 | * @param Closure $callback |
||
618 | * @param int $key |
||
619 | * |
||
620 | * @return Closure |
||
621 | */ |
||
622 | protected function bindOriginalRowModel(Closure $callback, $key) |
||
628 | |||
629 | /** |
||
630 | * Fill all data to every column. |
||
631 | * |
||
632 | * @param array $data |
||
633 | * |
||
634 | * @return mixed |
||
635 | */ |
||
636 | public function fill(array $data) |
||
657 | |||
658 | /** |
||
659 | * If current column is a defined column. |
||
660 | * |
||
661 | * @return bool |
||
662 | */ |
||
663 | protected function isDefinedColumn() |
||
667 | |||
668 | /** |
||
669 | * Use a defined column. |
||
670 | * |
||
671 | * @throws \Exception |
||
672 | */ |
||
673 | protected function useDefinedColumn() |
||
700 | |||
701 | /** |
||
702 | * Convert characters to HTML entities recursively. |
||
703 | * |
||
704 | * @param array|string $item |
||
705 | * |
||
706 | * @return mixed |
||
707 | */ |
||
708 | protected function htmlEntityEncode($item) |
||
720 | |||
721 | /** |
||
722 | * Create the column sorter. |
||
723 | * |
||
724 | * @return string |
||
725 | */ |
||
726 | public function sorter() |
||
753 | |||
754 | /** |
||
755 | * Determine if this column is currently sorted. |
||
756 | * |
||
757 | * @return bool |
||
758 | */ |
||
759 | protected function isSorted() |
||
769 | |||
770 | /** |
||
771 | * Set help message for column. |
||
772 | * |
||
773 | * @param string $help |
||
774 | * |
||
775 | * @return $this|string |
||
776 | */ |
||
777 | public function help($help = '') |
||
809 | |||
810 | /** |
||
811 | * Find a displayer to display column. |
||
812 | * |
||
813 | * @param string $abstract |
||
814 | * @param array $arguments |
||
815 | * |
||
816 | * @return $this |
||
817 | */ |
||
818 | protected function resolveDisplayer($abstract, $arguments) |
||
826 | |||
827 | /** |
||
828 | * Call Illuminate/Support displayer. |
||
829 | * |
||
830 | * @param string $abstract |
||
831 | * @param array $arguments |
||
832 | * |
||
833 | * @return $this |
||
834 | */ |
||
835 | protected function callSupportDisplayer($abstract, $arguments) |
||
849 | |||
850 | /** |
||
851 | * Call Builtin displayer. |
||
852 | * |
||
853 | * @param string $abstract |
||
854 | * @param array $arguments |
||
855 | * |
||
856 | * @return $this |
||
857 | */ |
||
858 | protected function callBuiltinDisplayer($abstract, $arguments) |
||
880 | |||
881 | /** |
||
882 | * Passes through all unknown calls to builtin displayer or supported displayer. |
||
883 | * |
||
884 | * Allow fluent calls on the Column object. |
||
885 | * |
||
886 | * @param string $method |
||
887 | * @param array $arguments |
||
888 | * |
||
889 | * @return $this |
||
890 | */ |
||
891 | public function __call($method, $arguments) |
||
904 | } |
||
905 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: