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 Nip_Form_Element_Abstract 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 Nip_Form_Element_Abstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class Nip_Form_Element_Abstract implements Nip_Form_Element_Interface |
||
|
|||
9 | { |
||
10 | |||
11 | protected $_form; |
||
12 | |||
13 | protected $_attribs; |
||
14 | protected $_options; |
||
15 | |||
16 | /** |
||
17 | * @var null|string |
||
18 | */ |
||
19 | protected $_uniqueID = null; |
||
20 | |||
21 | protected $_isRequired; |
||
22 | protected $_isRendered = false; |
||
23 | protected $_errors = []; |
||
24 | protected $_decorators; |
||
25 | protected $_policies; |
||
26 | |||
27 | protected $_type = 'abstract'; |
||
28 | |||
29 | 1 | public function __construct($form) |
|
34 | |||
35 | 1 | public function init() |
|
38 | |||
39 | public function setId($id) |
||
45 | |||
46 | /** |
||
47 | * @return Nip_Form_Element_Abstract |
||
48 | */ |
||
49 | 1 | public function setAttrib($key, $value) |
|
56 | |||
57 | public function getId() |
||
61 | |||
62 | 1 | View Code Duplication | public function getAttrib($key) |
63 | { |
||
64 | 1 | $key = (string)$key; |
|
65 | 1 | if (!isset($this->_attribs[$key])) { |
|
66 | return null; |
||
67 | } |
||
68 | |||
69 | 1 | return $this->_attribs[$key]; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getJSID() |
||
81 | |||
82 | /** |
||
83 | * @return null|string |
||
84 | */ |
||
85 | 1 | public function getUniqueId() |
|
93 | |||
94 | /** |
||
95 | * @param null|string $uniqueID |
||
96 | */ |
||
97 | 1 | public function setUniqueID($uniqueID) |
|
101 | |||
102 | 1 | protected function initUniqueId() |
|
106 | |||
107 | /** |
||
108 | * @return null|string |
||
109 | */ |
||
110 | 1 | protected function generateUniqueId() |
|
121 | |||
122 | 1 | public function getName() |
|
126 | |||
127 | /** |
||
128 | * @return AbstractForm |
||
129 | */ |
||
130 | 1 | public function getForm() |
|
134 | |||
135 | /** |
||
136 | * @param AbstractForm $form |
||
137 | * @return $this |
||
138 | */ |
||
139 | 1 | public function setForm(AbstractForm $form) |
|
145 | |||
146 | 1 | public function setName($name) |
|
152 | |||
153 | 1 | public function setLabel($label) |
|
159 | |||
160 | /** |
||
161 | * @param $data |
||
162 | * @param string $source |
||
163 | * @return Nip_Form_Element_Abstract |
||
164 | */ |
||
165 | public function getData($data, $source = 'abstract') |
||
173 | |||
174 | /** |
||
175 | * @param $data |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function getDataFromModel($data) |
||
184 | |||
185 | /** |
||
186 | * @param $value |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function setValue($value) |
||
195 | |||
196 | /** |
||
197 | * @param $request |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function getDataFromRequest($request) |
||
207 | |||
208 | /** |
||
209 | * @param $isRequired |
||
210 | * @return $this |
||
211 | */ |
||
212 | 1 | public function setRequired($isRequired) |
|
218 | |||
219 | /** |
||
220 | * @param $isRendered |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function setRendered($isRendered) |
||
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function isRendered() |
||
237 | |||
238 | /** |
||
239 | * @return $this |
||
240 | */ |
||
241 | View Code Duplication | public function addClass() |
|
242 | { |
||
243 | $classes = func_get_args(); |
||
244 | if (is_array($classes)) { |
||
245 | $oldClasses = explode(' ', $this->getAttrib('class')); |
||
246 | $classes = array_merge($classes, $oldClasses); |
||
247 | $this->setAttrib('class', implode(' ', $classes)); |
||
248 | } |
||
249 | |||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return $this |
||
255 | */ |
||
256 | View Code Duplication | public function removeClass() |
|
257 | { |
||
258 | $removeClasses = func_get_args(); |
||
259 | if (is_array($removeClasses)) { |
||
260 | $classes = explode(' ', $this->getAttrib('class')); |
||
261 | foreach ($removeClasses as $class) { |
||
262 | $key = array_search($class, $classes); |
||
263 | if ($key !== false) { |
||
264 | unset($classes[$key]); |
||
265 | } |
||
266 | } |
||
267 | $this->setAttrib('class', implode(' ', $classes)); |
||
268 | } |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | public function validate() |
||
287 | |||
288 | /** |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function isRequired() |
||
295 | |||
296 | /** |
||
297 | * @param string $requester |
||
298 | * @return null |
||
299 | */ |
||
300 | public function getValue($requester = 'abstract') |
||
304 | |||
305 | /** |
||
306 | * @return null |
||
307 | */ |
||
308 | 1 | public function getLabel() |
|
312 | |||
313 | /** |
||
314 | * @param $message |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function addError($message) |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getErrors() |
||
331 | |||
332 | /** |
||
333 | * @return bool |
||
334 | */ |
||
335 | public function isError() |
||
339 | |||
340 | /** |
||
341 | * @return bool |
||
342 | */ |
||
343 | public function isGroup() |
||
347 | |||
348 | /** |
||
349 | * @return string |
||
350 | */ |
||
351 | public function getType() |
||
355 | |||
356 | /** |
||
357 | * @param $key |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function delAttrib($key) |
||
367 | |||
368 | /** |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function getAttribs() |
||
375 | |||
376 | /** |
||
377 | * @param array $attribs |
||
378 | * @return Nip_Form_Element_Abstract |
||
379 | */ |
||
380 | public function setAttribs(array $attribs) |
||
386 | |||
387 | /** |
||
388 | * @return Nip_Form_Element_Abstract |
||
389 | */ |
||
390 | public function clearAttribs() |
||
396 | |||
397 | /** |
||
398 | * @param array $attribs |
||
399 | * @return Nip_Form_Element_Abstract |
||
400 | */ |
||
401 | public function addAttribs(array $attribs) |
||
409 | |||
410 | /** |
||
411 | * @return bool |
||
412 | */ |
||
413 | View Code Duplication | public function removeAttrib($key) |
|
414 | { |
||
415 | if (isset($this->_attribs[$key])) { |
||
416 | unset($this->_attribs[$key]); |
||
417 | |||
418 | return true; |
||
419 | } |
||
420 | |||
421 | return false; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @param $key |
||
426 | * @param $value |
||
427 | * @return $this |
||
428 | */ |
||
429 | public function setOption($key, $value) |
||
436 | |||
437 | /** |
||
438 | * @param $key |
||
439 | * @return null |
||
440 | */ |
||
441 | View Code Duplication | public function getOption($key) |
|
450 | |||
451 | /** |
||
452 | * @param string $type |
||
453 | * @return mixed |
||
454 | */ |
||
455 | public function newDecorator($type = '') |
||
463 | |||
464 | /** |
||
465 | * @param Nip_Form_Decorator_Elements_Abstract $decorator |
||
466 | * @param string $position |
||
467 | * @param bool $name |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function attachDecorator( |
||
481 | |||
482 | /** |
||
483 | * @param $position |
||
484 | * @return mixed |
||
485 | */ |
||
486 | public function getDecoratorsByPosition($position) |
||
490 | |||
491 | /** |
||
492 | * @param $name |
||
493 | * @param bool $position |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function getDecorator($name, $position = false) |
||
510 | |||
511 | /** |
||
512 | * @param $name |
||
513 | * @param bool $position |
||
514 | * @return $this |
||
515 | */ |
||
516 | public function removeDecorator($name, $position = false) |
||
517 | { |
||
518 | View Code Duplication | if ($position) { |
|
519 | unset ($this->_decorators[$position][$name]); |
||
520 | } else { |
||
521 | foreach ($this->_decorators as $position => $decorators) { |
||
522 | if (isset($decorators[$name])) { |
||
523 | unset($decorators[$name]); |
||
524 | |||
525 | return $this; |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | |||
530 | return $this; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @return bool |
||
535 | */ |
||
536 | public function hasCustomRenderer() |
||
540 | |||
541 | /** |
||
542 | * @return mixed |
||
543 | */ |
||
544 | public function render() |
||
548 | |||
549 | /** |
||
550 | * @return mixed |
||
551 | */ |
||
552 | public function getRenderer() |
||
556 | |||
557 | /** |
||
558 | * @return mixed |
||
559 | */ |
||
560 | public function renderElement() |
||
564 | |||
565 | /** |
||
566 | * @return mixed |
||
567 | */ |
||
568 | public function renderErrors() |
||
572 | |||
573 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.