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 |
||
10 | trait HasHeader |
||
11 | { |
||
12 | /** |
||
13 | * @var Filter |
||
14 | */ |
||
15 | public $filter; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $headers = []; |
||
21 | |||
22 | /** |
||
23 | * Add contents to column header. |
||
24 | * |
||
25 | * @param string|Renderable|Htmlable $header |
||
26 | * |
||
27 | * @return $this |
||
28 | */ |
||
29 | public function addHeader($header) |
||
40 | |||
41 | /** |
||
42 | * Add a column sortable to column header. |
||
43 | * |
||
44 | * @param string $cast |
||
45 | * |
||
46 | * @return Column|string |
||
47 | */ |
||
48 | protected function addSorter($cast = null) |
||
56 | |||
57 | /** |
||
58 | * Add a help tooltip to column header. |
||
59 | * |
||
60 | * @param string $message |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | protected function addHelp($message) |
||
68 | |||
69 | /** |
||
70 | * Add a filter to column header. |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | protected function addFilter($type = null, $formal = null) |
||
98 | |||
99 | /** |
||
100 | * Add a binding based on filter to the model query. |
||
101 | * |
||
102 | * @param Model $model |
||
103 | */ |
||
104 | public function bindFilterQuery(Model $model) |
||
110 | |||
111 | /** |
||
112 | * Render Column header. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | View Code Duplication | public function renderHeader() |
|
130 | } |
||
131 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: