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 |
||
7 | abstract class Filter implements FilterInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $name; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $operator; |
||
18 | |||
19 | /** |
||
20 | * @var Column |
||
21 | */ |
||
22 | protected $column; |
||
23 | |||
24 | /** |
||
25 | * @param array $options |
||
26 | */ |
||
27 | public function __construct(array $options = []) |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | */ |
||
35 | public function getColumn() |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | public function setColumn(Column $column) |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function setOperator($operator) |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | public function getOperator() |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function setName(string $name) |
||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | public function getName() |
||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | public function isEmpty() |
||
95 | |||
96 | /** |
||
97 | * @return array|string |
||
98 | */ |
||
99 | public function getInput() |
||
103 | |||
104 | abstract public function render(); |
||
105 | |||
106 | /** |
||
107 | * @param array $options |
||
108 | */ |
||
109 | protected function setDefaultOptions(array $options) |
||
119 | |||
120 | /** |
||
121 | * @return \Collective\Html\HtmlBuilder |
||
122 | */ |
||
123 | protected function getHtmlBuilder() |
||
127 | |||
128 | /** |
||
129 | * @return \Collective\Html\FormBuilder |
||
130 | */ |
||
131 | protected function getFormBuilder() |
||
135 | |||
136 | /** |
||
137 | * @return \Illuminate\Http\Request |
||
138 | */ |
||
139 | protected function getRequest() |
||
143 | |||
144 | /** |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function hasInput() |
||
151 | |||
152 | /** |
||
153 | * HTTP POST or HTTP GET can't have dots in name. |
||
154 | * |
||
155 | * @param string $name |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function normalizeName($name) |
||
162 | } |
||
163 |
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.