Complex classes like ArrayMap 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 ArrayMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ArrayMap implements MapInterface |
||
9 | { |
||
10 | /** @var array */ |
||
11 | protected $items; |
||
12 | |||
13 | /** |
||
14 | * @param array $items |
||
15 | */ |
||
16 | 24 | public function __construct(array $items = []) |
|
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | 1 | public function __toString() |
|
25 | { |
||
26 | 1 | $string = static::class . " [\n"; |
|
27 | 1 | foreach ($this->items as $key => $item) { |
|
28 | 1 | $string .= " \"{$key}\" => "; |
|
29 | 1 | if (is_string($item)) { |
|
30 | 1 | $string .= "\"{$item}\",\n"; |
|
31 | 1 | } elseif (is_scalar($item)) { |
|
32 | 1 | $string .= "{$item},\n"; |
|
33 | 1 | } elseif (is_null($item)) { |
|
34 | 1 | $string .= "null,\n"; |
|
35 | 1 | } elseif (is_array($item)) { |
|
36 | 1 | $string .= "[array],\n"; |
|
37 | 1 | } elseif (is_object($item)) { |
|
38 | 1 | $string .= "[" . get_class($item) . "],\n"; |
|
39 | } else { |
||
40 | 1 | $string .= "[unknown],\n"; |
|
41 | } |
||
42 | } |
||
43 | 1 | return $string . ']'; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function toArray() |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 1 | public function all() |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function isEmpty() |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 3 | public function count() |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | function jsonSerialize() |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 1 | public function offsetExists($offset) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 3 | public function offsetGet($offset) |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 2 | public function offsetSet($offset, $value) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 1 | public function offsetUnset($offset) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 1 | public function getIterator() |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | 1 | public function serialize() |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 1 | public function unserialize($serialized) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function clear() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 1 | public function contains(...$values) |
|
160 | { |
||
161 | 1 | foreach ($values as $value) { |
|
162 | 1 | if (!in_array($value, $this->items, true)) { |
|
163 | 1 | return false; |
|
164 | } |
||
165 | } |
||
166 | 1 | return true; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 4 | public function get($key, $default = null) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 4 | public function set($key, $value) |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 2 | public function remove(...$keys) |
|
190 | { |
||
191 | 2 | foreach ($keys as $key) { |
|
192 | 2 | unset($this->items[$key]); |
|
193 | } |
||
194 | 2 | } |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | 1 | public function has(...$keys) |
|
200 | { |
||
201 | 1 | foreach ($keys as $key) { |
|
202 | 1 | if (!array_key_exists($key, $this->items)) { |
|
203 | 1 | return false; |
|
204 | } |
||
205 | } |
||
206 | 1 | return true; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 1 | public function keys() |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 1 | public function values() |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 1 | public function map(callable $handler) |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 1 | public function reduce(callable $handler, $initial = null) |
|
238 | { |
||
239 | 1 | foreach ($this->items as $key => $item) { |
|
240 | 1 | $initial = $handler($initial, $item, $key); |
|
241 | } |
||
242 | 1 | return $initial; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param mixed $value |
||
247 | * @param string $method |
||
248 | * @param int $order |
||
249 | */ |
||
250 | 4 | private function assertIsNotNull($value, $method, $order = 1) |
|
256 | } |
||
257 |