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. 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 Attributes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Attributes implements \ArrayAccess, \IteratorAggregate |
||
9 | { |
||
10 | /** |
||
11 | * Stores the attribute data. |
||
12 | * |
||
13 | * @var \drupol\htmltag\Attribute[] |
||
14 | */ |
||
15 | private $storage = array(); |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 13 | public function __construct(array $attributes = array()) |
|
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 2 | public function &offsetGet($name) |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function offsetSet($name, $value = null) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function offsetUnset($name) |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function offsetExists($name) |
||
70 | |||
71 | /** |
||
72 | * Append a value into an attribute. |
||
73 | * |
||
74 | * @param string $key |
||
75 | * The attribute's name. |
||
76 | * @param string|array|bool $value |
||
77 | * The attribute's value. |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | 7 | public function append($key, $value = null) |
|
82 | { |
||
83 | $this->storage += array( |
||
84 | 7 | $key => new Attribute( |
|
85 | 7 | $key, |
|
86 | 7 | $this->ensureString($value) |
|
87 | ) |
||
88 | ); |
||
89 | |||
90 | 7 | foreach ($this->normalizeValue($value) as $value_item) { |
|
91 | 6 | $this->storage[$key]->append($value_item); |
|
92 | } |
||
93 | |||
94 | 7 | return $this; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Remove a value from a specific attribute. |
||
99 | * |
||
100 | * @param string $key |
||
101 | * The attribute's name. |
||
102 | * @param string|array|bool $value |
||
103 | * The attribute's value. |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | 2 | public function remove($key, $value = false) |
|
119 | |||
120 | /** |
||
121 | * Delete an attribute. |
||
122 | * |
||
123 | * @param string|array $name |
||
124 | * The name of the attribute key to delete. |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | 1 | public function delete($name = array()) |
|
136 | |||
137 | /** |
||
138 | * Return the attributes. |
||
139 | * |
||
140 | * @param string $key |
||
141 | * The attributes's name. |
||
142 | * @param array|string $value |
||
143 | * The attribute's value. |
||
144 | * |
||
145 | * @return \drupol\htmltag\Attributes |
||
146 | */ |
||
147 | public function without($key, $value) |
||
153 | |||
154 | /** |
||
155 | * Replace a value with another. |
||
156 | * |
||
157 | * @param string $key |
||
158 | * The attributes's name. |
||
159 | * @param string $value |
||
160 | * The attribute's value. |
||
161 | * @param array|string $replacement |
||
162 | * The replacement value. |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | 1 | public function replace($key, $value, $replacement) |
|
167 | { |
||
168 | $this->storage += array( |
||
169 | 1 | $key => new Attribute( |
|
170 | 1 | $key, |
|
171 | 1 | $this->ensureString($value) |
|
172 | ) |
||
173 | ); |
||
174 | |||
175 | 1 | $this->storage[$key]->remove($value); |
|
176 | 1 | foreach ($this->normalizeValue($replacement) as $replacement_value) { |
|
177 | 1 | $this->storage[$key]->append($replacement_value); |
|
178 | } |
||
179 | |||
180 | 1 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Merge attributes. |
||
185 | * |
||
186 | * @param array $data |
||
187 | * The data to merge. |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | 1 | public function merge(array $data = array()) |
|
192 | { |
||
193 | 1 | foreach ($data as $key => $value) { |
|
194 | $this->storage += array( |
||
195 | 1 | $key => new Attribute( |
|
196 | 1 | $key |
|
197 | ) |
||
198 | ); |
||
199 | |||
200 | 1 | $this->storage[$key]->merge( |
|
201 | 1 | $this->normalizeValue($value) |
|
202 | ); |
||
203 | } |
||
204 | |||
205 | 1 | return $this; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Check if an attribute exists and if a value if provided check it as well. |
||
210 | * |
||
211 | * @param string $key |
||
212 | * Attribute name. |
||
213 | * @param string $value |
||
214 | * Todo. |
||
215 | * |
||
216 | * @return bool |
||
217 | * Whereas an attribute exists. |
||
218 | */ |
||
219 | 1 | public function exists($key, $value = null) |
|
220 | { |
||
221 | 1 | if (!isset($this->storage[$key])) { |
|
222 | 1 | return false; |
|
223 | } |
||
224 | |||
225 | 1 | if (null !== $value) { |
|
226 | 1 | return $this->contains($key, $value); |
|
227 | } |
||
228 | |||
229 | 1 | return true; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Check if attribute contains a value. |
||
234 | * |
||
235 | * @param string $key |
||
236 | * Attribute name. |
||
237 | * @param string $value |
||
238 | * Attribute value. |
||
239 | * |
||
240 | * @return bool |
||
241 | * Whereas an attribute contains a value. |
||
242 | */ |
||
243 | 2 | public function contains($key, $value) |
|
244 | { |
||
245 | 2 | if (!isset($this->storage[$key])) { |
|
246 | 1 | return false; |
|
247 | } |
||
248 | |||
249 | 2 | return $this->storage[$key]->contains($value); |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 8 | public function __toString() |
|
259 | |||
260 | /** |
||
261 | * Render the attributes. |
||
262 | */ |
||
263 | 8 | public function render() |
|
274 | |||
275 | /** |
||
276 | * Returns all storage elements as an array. |
||
277 | * |
||
278 | * @return array |
||
279 | * An associative array of attributes. |
||
280 | */ |
||
281 | public function toArray() |
||
298 | |||
299 | /** |
||
300 | * Get storage. |
||
301 | * |
||
302 | * @return \drupol\htmltag\Attribute[] |
||
303 | * Todo. |
||
304 | */ |
||
305 | public function getStorage() |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function getIterator() |
||
317 | |||
318 | /** |
||
319 | * Returns all storage elements as an array. |
||
320 | * |
||
321 | * @return \drupol\htmltag\Attribute[] |
||
322 | * An associative array of attributes. |
||
323 | */ |
||
324 | 7 | private function prepareValues() |
|
355 | |||
356 | /** |
||
357 | * Normalize a value. |
||
358 | * |
||
359 | * @param mixed $value |
||
360 | * The value to normalize. |
||
361 | * |
||
362 | * @return array |
||
363 | * The value normalized. |
||
364 | */ |
||
365 | 7 | private function normalizeValue($value) |
|
369 | |||
370 | /** |
||
371 | * Todo. |
||
372 | * |
||
373 | * @param mixed $value |
||
374 | * Todo. |
||
375 | * |
||
376 | * @return array |
||
377 | * The array, flattened. |
||
378 | */ |
||
379 | 7 | private function ensureFlatArray($value) |
|
424 | |||
425 | /** |
||
426 | * Todo. |
||
427 | * |
||
428 | * @param mixed $value |
||
429 | * Todo. |
||
430 | * |
||
431 | * @return string |
||
432 | * A string. |
||
433 | */ |
||
434 | 7 | private function ensureString($value) |
|
464 | } |
||
465 |