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:
1 | <?php |
||
9 | class Custom extends TableColumn |
||
10 | { |
||
11 | /** |
||
12 | * Callback to render column contents. |
||
13 | * @var Closure |
||
14 | */ |
||
15 | protected $callback; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $view = 'column.custom'; |
||
21 | |||
22 | /** |
||
23 | * A field that can be ordered |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $orderField; |
||
27 | |||
28 | /** |
||
29 | * Custom constructor. |
||
30 | * |
||
31 | * @param null|string $label |
||
32 | * @param Closure $callback |
||
33 | */ |
||
34 | View Code Duplication | public function __construct($label = null, Closure $callback = null) |
|
41 | |||
42 | /** |
||
43 | * @return Closure |
||
44 | */ |
||
45 | public function getCallback() |
||
49 | |||
50 | /** |
||
51 | * @param Closure $callback |
||
52 | * |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function setCallback(Closure $callback) |
||
61 | |||
62 | /** |
||
63 | * @param string $field |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setOrderField($field) |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getOrderField() |
||
81 | |||
82 | /** |
||
83 | * Get value from callback. |
||
84 | * |
||
85 | * @param Model $model |
||
86 | * |
||
87 | * @return mixed |
||
88 | * @throws \Exception |
||
89 | */ |
||
90 | protected function getValue(Model $model) |
||
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | * @throws \Exception |
||
102 | */ |
||
103 | public function toArray() |
||
110 | } |
||
111 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.