Total Complexity | 60 |
Total Lines | 430 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 1 |
Complex classes like NestedAccessor 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 NestedAccessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class NestedAccessor implements NestedAccessorInterface |
||
18 | { |
||
19 | public const OPERATOR_FOR_EACH = '*'; |
||
20 | public const OPERATOR_PIPE = '|'; |
||
21 | |||
22 | /** |
||
23 | * @var array<mixed>|object |
||
24 | */ |
||
25 | protected $source; |
||
26 | /** |
||
27 | * @var non-empty-string |
||
|
|||
28 | */ |
||
29 | protected string $pathDelimiter; |
||
30 | |||
31 | /** |
||
32 | * NestedAccessor constructor. |
||
33 | * |
||
34 | * @param array<mixed>|object $source |
||
35 | * @param non-empty-string $pathDelimiter |
||
36 | */ |
||
37 | public function __construct(&$source, string $pathDelimiter = '.') |
||
38 | { |
||
39 | $this->source = &$source; |
||
40 | $this->pathDelimiter = $pathDelimiter; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritDoc} |
||
45 | */ |
||
46 | public function exist($path, bool $strict = true): bool |
||
47 | { |
||
48 | [, $allExist, $someExist] = $this->getInternal($path, false); |
||
49 | |||
50 | if ($strict) { |
||
51 | return $allExist; |
||
52 | } |
||
53 | |||
54 | return $someExist; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritDoc} |
||
59 | */ |
||
60 | public function isset($path, bool $strict = true): bool |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | public function get($path = null, bool $strict = true) |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public function set($path, $value): self |
||
88 | { |
||
89 | $source = &$this->getRef($this->getPathStack($path)); |
||
90 | |||
91 | if ($source instanceof ProxyInterface) { |
||
92 | try { |
||
93 | $source->setValue($value); |
||
94 | } catch (\BadMethodCallException $e) { |
||
95 | [$key, $path] = $this->cutPathTail($path); |
||
96 | throw new PathNotWritableException($key, $path, $this->pathDelimiter); |
||
97 | } |
||
98 | } else { |
||
99 | $source = $value; |
||
100 | } |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | public function update($path, $value): self |
||
109 | { |
||
110 | $this->checkExist($path); |
||
111 | |||
112 | return $this->set($path, $value); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | public function append($path, $value): self |
||
119 | { |
||
120 | $this->checkIsArrayAccessible($path); |
||
121 | |||
122 | /** @var array<mixed> $source */ |
||
123 | $source = &$this->getRef($this->getPathStack($path)); |
||
124 | $source[] = $value; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritDoc} |
||
131 | */ |
||
132 | public function delete($path, bool $strict = true): self |
||
133 | { |
||
134 | try { |
||
135 | $this->checkExist($path); |
||
136 | } catch (PathNotExistException $e) { |
||
137 | if ($strict) { |
||
138 | throw $e; |
||
139 | } |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | [$key, $path] = $this->cutPathTail($path); |
||
144 | $source = &$this->getRef($this->getPathStack($path)); |
||
145 | |||
146 | try { |
||
147 | ContainerAccessHelper::delete($source, $key); |
||
148 | } catch (\InvalidArgumentException $e) { |
||
149 | throw new PathNotWritableException($key, $path, $this->pathDelimiter); |
||
150 | } |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Checks if given path exist in container. |
||
157 | * |
||
158 | * @param string|string[]|null $path |
||
159 | * |
||
160 | * @throws PathNotExistException when path does not exist in container. |
||
161 | * @throws \InvalidArgumentException when invalid path passed. |
||
162 | */ |
||
163 | protected function checkExist($path): void |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Check if value by given path is array or ArrayAccess instance. |
||
170 | * |
||
171 | * @param string|string[]|null $path |
||
172 | * |
||
173 | * @return void |
||
174 | * |
||
175 | * @throws PathNotExistException when path does not exist in container. |
||
176 | * @throws PathNotArrayAccessibleException if path is not an array or ArrayAccess instance. |
||
177 | * @throws \InvalidArgumentException when invalid path passed. |
||
178 | */ |
||
179 | protected function checkIsArrayAccessible($path): void |
||
180 | { |
||
181 | if (!$this->exist($path)) { |
||
182 | [$key, $path] = $this->cutPathTail($path); |
||
183 | throw new PathNotExistException($key, $path, $this->pathDelimiter); |
||
184 | } |
||
185 | |||
186 | if (!ContainerAccessHelper::isArrayAccessible($this->get($path))) { |
||
187 | [$key, $path] = $this->cutPathTail($path); |
||
188 | throw new PathNotArrayAccessibleException($key, $path, $this->pathDelimiter); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Cuts last key from given path. |
||
194 | * |
||
195 | * Returns array of last key and truncated path. |
||
196 | * |
||
197 | * @param string|string[]|null $path |
||
198 | * |
||
199 | * @return array{string, string[]} [lastKey, truncatedPath] |
||
200 | * |
||
201 | * @throws \InvalidArgumentException when invalid path passed. |
||
202 | */ |
||
203 | protected function cutPathTail($path): array |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Returns ref to value stored by given path. |
||
211 | * |
||
212 | * Creates path if it does not exist. |
||
213 | * |
||
214 | * @param string[] $pathStack |
||
215 | * |
||
216 | * @return mixed|ProxyInterface<object> |
||
217 | * |
||
218 | * @throws PathNotWritableException when path is not writable. |
||
219 | */ |
||
220 | protected function &getRef(array $pathStack) |
||
221 | { |
||
222 | $source = &$this->source; |
||
223 | $traveledPath = []; |
||
224 | |||
225 | while (count($pathStack)) { |
||
226 | $pathItem = array_pop($pathStack); |
||
227 | $traveledPath[] = $pathItem; |
||
228 | |||
229 | try { |
||
230 | $source = &ContainerAccessHelper::getRef($source, $pathItem, []); |
||
231 | } catch (\InvalidArgumentException $e) { |
||
232 | throw new PathNotWritableException($pathItem, $traveledPath, $this->pathDelimiter); |
||
233 | } |
||
234 | |||
235 | if (count($pathStack) && is_scalar($source)) { |
||
236 | $source = []; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | return $source; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Converts given path to stack array. |
||
245 | * |
||
246 | * @param string|string[]|null $path |
||
247 | * |
||
248 | * @return string[] |
||
249 | * |
||
250 | * @throws \InvalidArgumentException when invalid path passed. |
||
251 | */ |
||
252 | protected function getPathStack($path): array |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Converts given path to array. |
||
259 | * |
||
260 | * @param string|string[]|mixed|null $path |
||
261 | * |
||
262 | * @return string[] |
||
263 | * |
||
264 | * @throws \InvalidArgumentException when invalid path passed. |
||
265 | */ |
||
266 | protected function getPathList($path): array |
||
267 | { |
||
268 | if ($path === null) { |
||
269 | return []; |
||
270 | } |
||
271 | |||
272 | if (is_string($path)) { |
||
273 | $path = explode($this->pathDelimiter, $path); |
||
274 | } |
||
275 | |||
276 | if (is_numeric($path)) { |
||
277 | $path = [strval($path)]; |
||
278 | } |
||
279 | |||
280 | if (!is_array($path)) { |
||
281 | $type = gettype($path); |
||
282 | throw new \InvalidArgumentException("Path must be numeric, string or array, {$type} given"); |
||
283 | } |
||
284 | |||
285 | return $path; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Handle path errors. |
||
290 | * |
||
291 | * @param string $key |
||
292 | * @param string[] $path |
||
293 | * @param bool $isResultMultiple |
||
294 | * @param bool $strict |
||
295 | * |
||
296 | * @return null|array{} |
||
297 | * |
||
298 | * @throws PathNotExistException always in strict mode. |
||
299 | */ |
||
300 | protected function handleError(string $key, array $path, bool $isResultMultiple, bool $strict): ?array |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param string|string[]|null $path |
||
311 | * @param bool $strict |
||
312 | * @return array{mixed, bool, bool, bool} |
||
313 | */ |
||
314 | public function getInternal($path = null, bool $strict = true): array |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * {@inheritDoc} |
||
416 | */ |
||
417 | public function offsetExists($offset): bool |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * {@inheritDoc} |
||
424 | * |
||
425 | * @return mixed |
||
426 | */ |
||
427 | #[\ReturnTypeWillChange] |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * {@inheritDoc} |
||
435 | */ |
||
436 | public function offsetSet($offset, $value): void |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * {@inheritDoc} |
||
443 | */ |
||
444 | public function offsetUnset($offset): void |
||
449 |