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 | 23 | public function __construct(array $attributes = array()) |
|
| 21 | { |
||
| 22 | 23 | foreach ($attributes as $name => $value) { |
|
| 23 | 1 | $this->storage[$name] = new Attribute( |
|
| 24 | 1 | $name, |
|
| 25 | 1 | $this->ensureString($value) |
|
| 26 | ); |
||
| 27 | } |
||
| 28 | 23 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | 2 | public function &offsetGet($name) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 1 | public function offsetSet($name, $value = null) |
|
| 48 | { |
||
| 49 | 1 | $this->storage[$name] = new Attribute( |
|
| 50 | 1 | $name, |
|
| 51 | 1 | $this->ensureString($value) |
|
| 52 | ); |
||
| 53 | 1 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 1 | public function offsetUnset($name) |
|
| 59 | { |
||
| 60 | 1 | unset($this->storage[$name]); |
|
| 61 | 1 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 2 | public function offsetExists($name) |
|
| 67 | { |
||
| 68 | 2 | return isset($this->storage[$name]); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 14 | public function append($key, $value = null) |
|
| 75 | { |
||
| 76 | $this->storage += array( |
||
| 77 | 14 | $key => new Attribute( |
|
| 78 | 14 | $key, |
|
| 79 | 14 | $this->ensureString($value) |
|
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | 14 | foreach ($this->normalizeValue($value) as $value_item) { |
|
| 84 | 13 | $this->storage[$key]->append($value_item); |
|
| 85 | } |
||
| 86 | |||
| 87 | 14 | return $this; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | 2 | public function remove($key, $value = false) |
|
| 94 | { |
||
| 95 | 2 | if (!isset($this->storage[$key])) { |
|
| 96 | 1 | return $this; |
|
| 97 | } |
||
| 98 | |||
| 99 | 1 | foreach ($this->normalizeValue($value) as $value_item) { |
|
| 100 | 1 | $this->storage[$key]->remove($value_item); |
|
| 101 | } |
||
| 102 | |||
| 103 | 1 | return $this; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | 1 | public function delete($name = array()) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function without($key, $value) |
||
| 122 | { |
||
| 123 | $attributes = clone $this; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | 1 | public function replace($key, $value, $replacement) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | 1 | public function merge(array $data = array()) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 1 | public function exists($key, $value = null) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 3 | public function contains($key, $value) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 7 | public function __toString() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | 12 | public function render() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | 2 | public function toArray() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | 1 | public function getStorage() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | 1 | public function getIterator() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 1 | public function count() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Returns all storage elements as an array. |
||
| 262 | * |
||
| 263 | * @return \drupol\htmltag\Attribute[] |
||
| 264 | * An associative array of attributes. |
||
| 265 | */ |
||
| 266 | 13 | private function prepareValues() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Normalize a value. |
||
| 300 | * |
||
| 301 | * @param mixed $value |
||
| 302 | * The value to normalize. |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | * The value normalized. |
||
| 306 | */ |
||
| 307 | 15 | private function normalizeValue($value) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Todo. |
||
| 314 | * |
||
| 315 | * @param mixed $value |
||
| 316 | * Todo. |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | * The array, flattened. |
||
| 320 | */ |
||
| 321 | 15 | private function ensureFlatArray($value) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Todo. |
||
| 369 | * |
||
| 370 | * @param mixed $value |
||
| 371 | * Todo. |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | * A string. |
||
| 375 | */ |
||
| 376 | 15 | private function ensureString($value) |
|
| 406 | } |
||
| 407 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: