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 |
||
5 | abstract class Nip_Form_Button_Abstract |
||
|
|||
6 | { |
||
7 | protected $_form; |
||
8 | protected $_attribs; |
||
9 | protected $_uniqueID; |
||
10 | |||
11 | protected $_type = 'abstract'; |
||
12 | |||
13 | public function __construct($form) |
||
14 | { |
||
15 | $this->setForm($form); |
||
16 | $this->init(); |
||
17 | } |
||
18 | |||
19 | public function init() |
||
20 | { |
||
21 | $this->addClass('btn', 'btn-primary'); |
||
22 | } |
||
23 | |||
24 | public function addClass() |
||
25 | { |
||
26 | $classes = func_get_args(); |
||
27 | if (is_array($classes)) { |
||
28 | $oldClasses = explode(' ', $this->getAttrib('class')); |
||
29 | $classes = array_merge($classes, $oldClasses); |
||
30 | $this->setAttrib('class', implode(' ', $classes)); |
||
31 | } |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param string $key |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function getAttrib($key) |
||
42 | { |
||
43 | $key = (string) $key; |
||
44 | if (!isset($this->_attribs[$key])) { |
||
45 | return null; |
||
46 | } |
||
47 | |||
48 | return $this->_attribs[$key]; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return Nip_Form_Button_Abstract |
||
53 | */ |
||
54 | public function setAttrib($key, $value) |
||
55 | { |
||
56 | $key = (string) $key; |
||
57 | $this->_attribs[$key] = $value; |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function setId($id) |
||
63 | { |
||
64 | $this->setAttrib('id', $id); |
||
65 | |||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | public function getId() |
||
70 | { |
||
71 | return $this->getAttrib('id'); |
||
72 | } |
||
73 | |||
74 | public function setName($name) |
||
75 | { |
||
76 | $this->setAttrib('name', $name); |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getName() |
||
85 | { |
||
86 | return $this->getAttrib('name'); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $label |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setLabel($label) |
||
94 | { |
||
95 | $this->setAttrib('label', $label); |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getLabel() |
||
104 | { |
||
105 | return $this->getAttrib('label'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param $value |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setValue($value) |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param string $requester |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getValue($requester = 'abstract') |
||
124 | { |
||
125 | return $this->getAttrib('value'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param $key |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function delAttrib($key) |
||
133 | { |
||
134 | $key = (string) $key; |
||
135 | unset($this->_attribs[$key]); |
||
136 | |||
137 | return true; |
||
138 | } |
||
139 | |||
140 | public function getAttribs() |
||
141 | { |
||
142 | return $this->_attribs; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param array $attribs |
||
147 | * @return Nip_Form_Button_Abstract |
||
148 | */ |
||
149 | public function setAttribs(array $attribs) |
||
150 | { |
||
151 | $this->clearAttribs(); |
||
152 | |||
153 | return $this->addAttribs($attribs); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return Nip_Form_Button_Abstract |
||
158 | */ |
||
159 | public function clearAttribs() |
||
160 | { |
||
161 | $this->_attribs = []; |
||
162 | |||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param array $attribs |
||
168 | * @return Nip_Form_Button_Abstract |
||
169 | */ |
||
170 | public function addAttribs(array $attribs) |
||
171 | { |
||
172 | foreach ($attribs as $key => $value) { |
||
173 | $this->setAttrib($key, $value); |
||
174 | } |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function removeAttrib($key) |
||
183 | { |
||
184 | if (isset($this->_attribs[$key])) { |
||
185 | unset($this->_attribs[$key]); |
||
186 | |||
187 | return true; |
||
188 | } |
||
189 | |||
190 | return false; |
||
191 | } |
||
192 | |||
193 | public function render() |
||
196 | } |
||
197 | |||
198 | public function getRenderer() |
||
199 | { |
||
200 | return $this->getForm()->getRenderer()->getButtonRenderer($this); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return AbstractForm |
||
205 | */ |
||
206 | public function getForm() |
||
207 | { |
||
208 | return $this->_form; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param AbstractForm $form |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setForm(AbstractForm $form) |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getType() |
||
226 | { |
||
227 | return $this->_type; |
||
228 | } |
||
229 | } |
||
230 |
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.