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 |
||
25 | class Select extends OptionElement |
||
26 | { |
||
27 | /** |
||
28 | * Pre-selected values |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $value = array(); |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * |
||
37 | * @param string|array $caption Caption or array of all attributes |
||
38 | * @param string $name name" attribute |
||
39 | * @param mixed $value Pre-selected value (or array of them). |
||
40 | * @param integer $size Number or rows. "1" makes a drop-down-list |
||
41 | * @param boolean $multiple Allow multiple selections? |
||
42 | */ |
||
43 | 12 | public function __construct($caption, $name = null, $value = null, $size = 1, $multiple = false) |
|
44 | { |
||
45 | 12 | if (is_array($caption)) { |
|
46 | 2 | parent::__construct($caption); |
|
47 | 2 | $this->setIfNotSet('size', 1); |
|
48 | } else { |
||
49 | 11 | $this->setWithDefaults('caption', $caption, ''); |
|
50 | 11 | $this->setWithDefaults('name', $name, 'name_error'); |
|
51 | 11 | $this->set('value', $value); |
|
52 | 11 | $this->setWithDefaults('size', $size, 1); |
|
53 | 11 | if ($multiple) { |
|
54 | $this->set('multiple'); |
||
55 | } |
||
56 | } |
||
57 | 12 | } |
|
58 | |||
59 | /** |
||
60 | * Are multiple selections allowed? |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | 1 | public function isMultiple() |
|
68 | |||
69 | /** |
||
70 | * Get the size |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | 1 | public function getSize() |
|
78 | |||
79 | /** |
||
80 | * Add multiple optgroup |
||
81 | * |
||
82 | * @param string $name name attribute |
||
83 | * @param array $optgroup Associative array of value->name pairs |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | 1 | public function addOptionGroup($name, $optgroup) |
|
91 | |||
92 | /** |
||
93 | * render a single option |
||
94 | * |
||
95 | * @param string $optionValue option element value |
||
96 | * @param string $optionDisplay displayed text |
||
97 | * @param string[] $selected selected option values |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 2 | protected function renderOption($optionValue, $optionDisplay, $selected) |
|
111 | |||
112 | /** |
||
113 | * Prepare HTML for output |
||
114 | * |
||
115 | * @return string HTML |
||
116 | */ |
||
117 | 16 | public function render() |
|
144 | |||
145 | /** |
||
146 | * Render custom javascript validation code |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 2 | View Code Duplication | public function renderValidationJS() |
171 | } |
||
172 |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.