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 AttributesInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Stores the attribute data. |
||
| 12 | * |
||
| 13 | * @var \drupol\htmltag\Attribute[] |
||
| 14 | */ |
||
| 15 | private $storage = array(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * {@inheritdoc} |
||
| 19 | */ |
||
| 20 | 26 | public function __construct(array $attributes = array()) |
|
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | 26 | public function import($attributes = array()) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 7 | public function set($name, $value = null) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | 2 | public function offsetGet($name) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | 1 | public function offsetSet($name, $value = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | 1 | public function offsetUnset($name) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 2 | public function offsetExists($name) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 14 | public function append($key, $value = '') |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 2 | public function remove($key, $value = '') |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 2 | public function delete($name) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 1 | public function without($key) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 1 | public function replace($key, $value, $replacement) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 1 | public function merge(array $data = array()) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 1 | public function exists($key, $value = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | 3 | public function contains($key, $value) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | 7 | public function __toString() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | 15 | public function render() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | 2 | public function toArray() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | 1 | public function getStorage() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | 1 | public function getIterator() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | 1 | public function count() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Returns all storage elements as an array. |
||
| 277 | * |
||
| 278 | * @return \drupol\htmltag\Attribute[] |
||
| 279 | * An associative array of attributes. |
||
| 280 | */ |
||
| 281 | 16 | private function prepareValues() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Normalize a value. |
||
| 315 | * |
||
| 316 | * @param mixed $value |
||
| 317 | * The value to normalize. |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | * The value normalized. |
||
| 321 | */ |
||
| 322 | 18 | private function normalizeValue($value) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Todo. |
||
| 329 | * |
||
| 330 | * @param mixed $value |
||
| 331 | * Todo. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | * The array, flattened. |
||
| 335 | */ |
||
| 336 | 18 | private function ensureFlatArray($value) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Todo. |
||
| 384 | * |
||
| 385 | * @param mixed $value |
||
| 386 | * Todo. |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | * A string. |
||
| 390 | */ |
||
| 391 | 20 | private function ensureString($value) |
|
| 421 | } |
||
| 422 |