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 namespace Mascame\Artificer\Fields; |
||
8 | class Field |
||
9 | { |
||
10 | use Filterable; |
||
11 | |||
12 | /** |
||
13 | * @var null |
||
14 | */ |
||
15 | public $value; |
||
16 | |||
17 | public static $widgets = array(); |
||
18 | /** |
||
19 | * @var FieldOptions |
||
20 | */ |
||
21 | public $options; |
||
22 | |||
23 | /** |
||
24 | * @var FieldRelation |
||
25 | */ |
||
26 | public $relation = null; |
||
27 | |||
28 | /** |
||
29 | * Sometimes ajax limits output, setting this to true will return all |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | public $showFullField = false; |
||
34 | |||
35 | /** |
||
36 | * @var FieldInterface|TypeInterface |
||
37 | */ |
||
38 | public $field; |
||
39 | |||
40 | /** |
||
41 | * Field constructor. |
||
42 | * @param FieldInterface|TypeInterface $field |
||
43 | * @param null $relation |
||
44 | */ |
||
45 | public function __construct(FieldInterface $field, $relation = null) |
||
55 | |||
56 | /** |
||
57 | * @param $widget |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function addWidget(AbstractWidget $widget) |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Used to load custom assets, widgets, ... |
||
74 | * |
||
75 | */ |
||
76 | public function boot() |
||
92 | |||
93 | /** |
||
94 | * @param null $value |
||
95 | * @return null |
||
96 | */ |
||
97 | // public function display($value = null) |
||
98 | // { |
||
99 | // $this->value = $this->getValue($value); |
||
100 | // |
||
101 | // return $this->show(); |
||
102 | // } |
||
103 | |||
104 | |||
105 | // public function setValue($value) { |
||
106 | // $this->value = $value; |
||
107 | // } |
||
108 | |||
109 | /** |
||
110 | * @param null $value |
||
111 | * @return null |
||
112 | */ |
||
113 | public function show($value = null) |
||
127 | |||
128 | /** |
||
129 | * @return bool|mixed|null|string |
||
130 | */ |
||
131 | public function output() |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function hidden() |
||
148 | |||
149 | /** |
||
150 | * @param $array |
||
151 | * @return bool |
||
152 | */ |
||
153 | protected function isAll($array) |
||
157 | |||
158 | /** |
||
159 | * @param string $list |
||
160 | * @return bool |
||
161 | */ |
||
162 | protected function isListedAs($list = 'visible') |
||
174 | |||
175 | /** |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isListable() |
||
182 | |||
183 | |||
184 | /** |
||
185 | * @param $value |
||
186 | * @param $array |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isInArray($value, $array) |
||
193 | |||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function guarded() |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | View Code Duplication | public function isGuarded() |
|
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | View Code Duplication | public function isHidden() |
|
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isRelation() |
||
234 | |||
235 | public static function get($name) |
||
239 | |||
240 | public function __get($name) { |
||
249 | |||
250 | public function __call($method, $args) { |
||
257 | } |
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.