Total Complexity | 74 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like Attributes 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.
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 Attributes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Attributes extends AbstractRepository implements RenderableInterface |
||
27 | { |
||
28 | /** |
||
29 | * Attributes::setAttributeId |
||
30 | * |
||
31 | * @param string $id |
||
32 | * |
||
33 | * @return static |
||
34 | */ |
||
35 | public function setAttributeId($id) |
||
36 | { |
||
37 | $this->addAttribute('id', $id); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | // ------------------------------------------------------------------------ |
||
43 | |||
44 | /** |
||
45 | * Attributes::addAttribute |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @param string $value |
||
49 | * |
||
50 | * @return static |
||
51 | */ |
||
52 | public function addAttribute($name, $value) |
||
53 | { |
||
54 | if ($name === 'class') { |
||
55 | $this->addAttributeClass($value); |
||
56 | } elseif ($name === 'style') { |
||
57 | $value = rtrim($value, ';') . ';'; |
||
58 | $value = explode(';', $value); |
||
59 | $value = array_filter($value); |
||
60 | |||
61 | foreach ($value as $style) { |
||
62 | if (preg_match_all("/(.*)(: )(.*)/", $style, $match)) { |
||
63 | $this->addAttributeStyle($match[ 1 ][ 0 ], $match[ 3 ][ 0 ]); |
||
64 | } elseif (preg_match_all("/(.*)(:)(.*)/", $style, $match)) { |
||
65 | $this->addAttributeStyle($match[ 1 ][ 0 ], $match[ 3 ][ 0 ]); |
||
66 | } |
||
67 | } |
||
68 | } elseif (is_string($value)) { |
||
|
|||
69 | $this->storage[ $name ] = trim($value); |
||
70 | } else { |
||
71 | $this->storage[ $name ] = $value; |
||
72 | } |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | // ------------------------------------------------------------------------ |
||
78 | |||
79 | /** |
||
80 | * Attributes::addAttributeClass |
||
81 | * |
||
82 | * @param array|string $classes |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | public function addAttributeClass($classes) |
||
87 | { |
||
88 | if (is_string($classes)) { |
||
89 | $classes = explode(',', $classes); |
||
90 | } |
||
91 | |||
92 | $classes = array_map('trim', $classes); |
||
93 | $classes = array_filter($classes); |
||
94 | |||
95 | if ( ! $this->offsetExists('class')) { |
||
96 | $this->storage[ 'class' ] = []; |
||
97 | } |
||
98 | |||
99 | $this->storage[ 'class' ] = array_merge($this->storage[ 'class' ], $classes); |
||
100 | $this->storage[ 'class' ] = array_unique($this->storage[ 'class' ]); |
||
101 | |||
102 | if (in_array('disabled', $this->storage[ 'class' ])) { |
||
103 | $this->removeAttributeClass('active'); |
||
104 | } |
||
105 | |||
106 | return $this; |
||
107 | } |
||
108 | |||
109 | // ------------------------------------------------------------------------ |
||
110 | |||
111 | /** |
||
112 | * Attributes::removeAttributeClass |
||
113 | * |
||
114 | * @param array|string $classes |
||
115 | */ |
||
116 | public function removeAttributeClass($classes) |
||
117 | { |
||
118 | if ($this->offsetExists('class')) { |
||
119 | if (is_string($classes)) { |
||
120 | $classes = explode(',', $classes); |
||
121 | } |
||
122 | |||
123 | $classes = array_map('trim', $classes); |
||
124 | $classes = array_filter($classes); |
||
125 | |||
126 | foreach ($classes as $class) { |
||
127 | if (false !== ($key = array_search($class, $this->storage[ 'class' ]))) { |
||
128 | unset($this->storage[ 'class' ][ $key ]); |
||
129 | } elseif (strpos($class, '*') !== false) { |
||
130 | $class = str_replace('*', '', $class); |
||
131 | foreach ($this->storage[ 'class' ] as $key => $value) { |
||
132 | if (preg_match("/\b$class\b/i", $value)) { |
||
133 | unset($this->storage[ 'class' ][ $key ]); |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if (count($this->storage[ 'class' ]) == 0) { |
||
140 | unset($this->storage[ 'class' ]); |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | |||
145 | // ------------------------------------------------------------------------ |
||
146 | |||
147 | /** |
||
148 | * Attributes::addAttributeStyle |
||
149 | * |
||
150 | * @param string|array $styles |
||
151 | * @param string|null $value |
||
152 | * |
||
153 | * @return static |
||
154 | */ |
||
155 | public function addAttributeStyle($styles, $value = null) |
||
156 | { |
||
157 | if (is_string($styles)) { |
||
158 | $styles = [$styles => $value]; |
||
159 | } |
||
160 | |||
161 | if ( ! $this->offsetExists('style')) { |
||
162 | $this->storage[ 'style' ] = []; |
||
163 | } |
||
164 | |||
165 | foreach ($styles as $key => $value) { |
||
166 | if (empty($value)) { |
||
167 | continue; |
||
168 | } |
||
169 | $styles[ trim($key) ] = trim($value); |
||
170 | } |
||
171 | |||
172 | $this->storage[ 'style' ] = array_merge($this->storage[ 'style' ], $styles); |
||
173 | |||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | // ------------------------------------------------------------------------ |
||
178 | |||
179 | /** |
||
180 | * Attributes::hasAttribute |
||
181 | * |
||
182 | * @param string $name |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function hasAttribute($name) |
||
187 | { |
||
188 | if ($name === 'id') { |
||
189 | return empty($this->storage[ 'id' ]) ? false : true; |
||
190 | } elseif ($name === 'class') { |
||
191 | return empty($this->storage[ 'class' ]) ? false : true; |
||
192 | } else { |
||
193 | return isset($this->storage[ $name ]); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // ------------------------------------------------------------------------ |
||
198 | |||
199 | /** |
||
200 | * Attributes::getAttribute |
||
201 | * |
||
202 | * @param string|null $name |
||
203 | * |
||
204 | * @return array|bool |
||
205 | */ |
||
206 | public function getAttribute($name = null) |
||
207 | { |
||
208 | if (empty($name)) { |
||
209 | return $this->storage; |
||
210 | } elseif (isset($this->storage[ $name ])) { |
||
211 | return $this->storage[ $name ]; |
||
212 | } |
||
213 | |||
214 | return false; |
||
215 | } |
||
216 | |||
217 | // ------------------------------------------------------------------------ |
||
218 | |||
219 | /** |
||
220 | * Attributes::removeAttribute |
||
221 | * |
||
222 | * @param string|array $attributes |
||
223 | */ |
||
224 | public function removeAttribute($attributes) |
||
225 | { |
||
226 | if (is_string($attributes)) { |
||
227 | $attributes = explode(',', $attributes); |
||
228 | } |
||
229 | |||
230 | $attributes = array_map('trim', $attributes); |
||
231 | $attributes = array_filter($attributes); |
||
232 | |||
233 | foreach ($attributes as $attribute) { |
||
234 | if (array_key_exists($attribute, $this->storage)) { |
||
235 | unset($this->storage[ $attribute ]); |
||
236 | } elseif (strpos($attribute, '*') !== false) { |
||
237 | $attribute = str_replace('*', '', $attribute); |
||
238 | foreach ($this->storage as $key => $value) { |
||
239 | if (preg_match("/\b$attribute\b/i", $key)) { |
||
240 | unset($this->storage[ $key ]); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | // ------------------------------------------------------------------------ |
||
248 | |||
249 | /** |
||
250 | * Attributes::getAttributeId |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function getAttributeId() |
||
255 | { |
||
256 | if ($this->hasAttributeId()) { |
||
257 | return $this->storage[ 'id' ]; |
||
258 | } |
||
259 | |||
260 | return false; |
||
261 | } |
||
262 | |||
263 | // ------------------------------------------------------------------------ |
||
264 | |||
265 | /** |
||
266 | * Attributes::hasAttributeId |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function hasAttributeId() |
||
271 | { |
||
272 | return (bool)empty($this->storage[ 'id' ]) ? false : true; |
||
273 | } |
||
274 | |||
275 | // ------------------------------------------------------------------------ |
||
276 | |||
277 | /** |
||
278 | * Attributes::hasAttributeClass |
||
279 | * |
||
280 | * @param string $className |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | public function hasAttributeClass($className) |
||
285 | { |
||
286 | if ( ! $this->offsetExists('class')) { |
||
287 | $this->storage[ 'class' ] = []; |
||
288 | } |
||
289 | |||
290 | return in_array($className, $this->storage[ 'class' ]); |
||
291 | } |
||
292 | |||
293 | // ------------------------------------------------------------------------ |
||
294 | |||
295 | /** |
||
296 | * Attributes::getAttributeClass |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getAttributeClass() |
||
301 | { |
||
302 | if ( ! $this->offsetExists('class')) { |
||
303 | $this->storage[ 'class' ] = []; |
||
304 | } |
||
305 | |||
306 | return implode(', ', $this->storage[ 'class' ]); |
||
307 | } |
||
308 | |||
309 | // ------------------------------------------------------------------------ |
||
310 | |||
311 | /** |
||
312 | * Attributes::replaceAttributeClass |
||
313 | * |
||
314 | * @param string $class |
||
315 | * @param string $replace |
||
316 | */ |
||
317 | public function replaceAttributeClass($class, $replace) |
||
318 | { |
||
319 | if ($this->offsetExists('class')) { |
||
320 | foreach ($this->storage[ 'class' ] as $key => $value) { |
||
321 | if (preg_match("/\b$class\b/i", $value)) { |
||
322 | $this->storage[ 'class' ][ $key ] = str_replace($class, $replace, $value); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | if (count($this->storage[ 'class' ]) == 0) { |
||
327 | unset($this->storage[ 'class' ]); |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | |||
332 | // ------------------------------------------------------------------------ |
||
333 | |||
334 | /** |
||
335 | * Attributes::findAttributeClass |
||
336 | * |
||
337 | * @param string $class |
||
338 | * |
||
339 | * @return array|bool |
||
340 | */ |
||
341 | public function findAttributeClass($class) |
||
359 | } |
||
360 | |||
361 | // ------------------------------------------------------------------------ |
||
362 | |||
363 | /** |
||
364 | * Attributes::__toString |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function __toString() |
||
371 | } |
||
372 | |||
373 | // ------------------------------------------------------------------------ |
||
374 | |||
375 | /** |
||
376 | * Attributes::render |
||
377 | * |
||
378 | * @param array $options |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | public function render(array $options = []) |
||
443 | } |
||
444 | } |