Total Complexity | 53 |
Total Lines | 388 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | 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 |
||
16 | class NestedAccessor implements NestedAccessorInterface |
||
17 | { |
||
18 | public const OPERATOR_FOR_EACH = '*'; |
||
19 | public const OPERATOR_PIPE = '|'; |
||
20 | |||
21 | /** |
||
22 | * @var array<mixed>|object |
||
23 | */ |
||
24 | protected $source; |
||
25 | /** |
||
26 | * @var non-empty-string |
||
|
|||
27 | */ |
||
28 | protected string $pathDelimiter; |
||
29 | |||
30 | /** |
||
31 | * NestedAccessor constructor. |
||
32 | * |
||
33 | * @param array<mixed>|object $source |
||
34 | * @param non-empty-string $pathDelimiter |
||
35 | */ |
||
36 | public function __construct(&$source, string $pathDelimiter = '.') |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | public function exist($path, bool $strict = true): bool |
||
46 | { |
||
47 | [, $allExist, $someExist] = $this->getInternal($path, false); |
||
48 | |||
49 | if ($strict) { |
||
50 | return $allExist; |
||
51 | } |
||
52 | |||
53 | return $someExist; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritDoc} |
||
58 | */ |
||
59 | public function isset($path, bool $strict = true): bool |
||
60 | { |
||
61 | [$result, $allExist, $someExist] = $this->getInternal($path, false); |
||
62 | |||
63 | if ($result === null) { |
||
64 | return false; |
||
65 | } |
||
66 | |||
67 | if ($strict) { |
||
68 | return $allExist; |
||
69 | } |
||
70 | |||
71 | return $someExist; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | public function get($path = null, bool $strict = true) |
||
78 | { |
||
79 | [$result] = $this->getInternal($path, $strict); |
||
80 | return $result; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | */ |
||
86 | public function set($path, $value): self |
||
87 | { |
||
88 | $source = &$this->getRef($this->getPathStack($path)); |
||
89 | $source = $value; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | public function update($path, $value): self |
||
98 | { |
||
99 | $this->checkExist($path); |
||
100 | |||
101 | return $this->set($path, $value); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | public function append($path, $value): self |
||
108 | { |
||
109 | $this->checkIsArrayAccessible($path); |
||
110 | |||
111 | /** @var array<mixed> $source */ |
||
112 | $source = &$this->getRef($this->getPathStack($path)); |
||
113 | $source[] = $value; |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function delete($path, bool $strict = true): self |
||
122 | { |
||
123 | try { |
||
124 | $this->checkExist($path); |
||
125 | } catch (PathNotExistException $e) { |
||
126 | if ($strict) { |
||
127 | throw $e; |
||
128 | } |
||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | [$key, $path] = $this->cutPathTail($path); |
||
133 | $source = &$this->getRef($this->getPathStack($path)); |
||
134 | |||
135 | try { |
||
136 | ContainerAccessHelper::delete($source, $key); |
||
137 | } catch (\InvalidArgumentException $e) { |
||
138 | throw new PathNotWritableException($key, $path, $this->pathDelimiter); |
||
139 | } |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Checks if given path exist in container. |
||
146 | * |
||
147 | * @param string|string[]|null $path |
||
148 | * |
||
149 | * @throws PathNotExistException when path does not exist in container. |
||
150 | * @throws \InvalidArgumentException when invalid path passed. |
||
151 | */ |
||
152 | protected function checkExist($path): void |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Check if value by given path is array or ArrayAccess instance. |
||
159 | * |
||
160 | * @param string|string[]|null $path |
||
161 | * |
||
162 | * @return void |
||
163 | * |
||
164 | * @throws PathNotExistException when path does not exist in container. |
||
165 | * @throws PathNotArrayException if path is not an array or ArrayAccess instance. |
||
166 | * @throws \InvalidArgumentException when invalid path passed. |
||
167 | */ |
||
168 | protected function checkIsArrayAccessible($path): void |
||
169 | { |
||
170 | if (!$this->exist($path)) { |
||
171 | [$key, $path] = $this->cutPathTail($path); |
||
172 | throw new PathNotExistException($key, $path, $this->pathDelimiter); |
||
173 | } |
||
174 | |||
175 | if (!ContainerAccessHelper::isArrayAccessible($this->get($path))) { |
||
176 | [$key, $path] = $this->cutPathTail($path); |
||
177 | throw new PathNotArrayException($key, $path, $this->pathDelimiter); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Cuts last key from given path. |
||
183 | * |
||
184 | * Returns array of last key and truncated path. |
||
185 | * |
||
186 | * @param string|string[]|null $path |
||
187 | * |
||
188 | * @return array{string, string[]} [lastKey, truncatedPath] |
||
189 | * |
||
190 | * @throws \InvalidArgumentException when invalid path passed. |
||
191 | */ |
||
192 | protected function cutPathTail($path): array |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Returns ref to value stored by given path. |
||
200 | * |
||
201 | * Creates path if it does not exist. |
||
202 | * |
||
203 | * @param string[] $pathStack |
||
204 | * |
||
205 | * @return mixed |
||
206 | * |
||
207 | * @throws PathNotWritableException when path is not writable. |
||
208 | */ |
||
209 | protected function &getRef(array $pathStack) |
||
210 | { |
||
211 | $source = &$this->source; |
||
212 | $traveledPath = []; |
||
213 | |||
214 | while (count($pathStack)) { |
||
215 | $pathItem = array_pop($pathStack); |
||
216 | $traveledPath[] = $pathItem; |
||
217 | |||
218 | try { |
||
219 | $source = &ContainerAccessHelper::getRef($source, $pathItem, []); |
||
220 | } catch (\InvalidArgumentException $e) { |
||
221 | throw new PathNotWritableException($pathItem, $traveledPath, $this->pathDelimiter); |
||
222 | } |
||
223 | |||
224 | if (count($pathStack) && is_scalar($source)) { |
||
225 | $source = []; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return $source; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Converts given path to stack array. |
||
234 | * |
||
235 | * @param string|string[]|null $path |
||
236 | * |
||
237 | * @return string[] |
||
238 | * |
||
239 | * @throws \InvalidArgumentException when invalid path passed. |
||
240 | */ |
||
241 | protected function getPathStack($path): array |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Converts given path to array. |
||
248 | * |
||
249 | * @param string|string[]|mixed|null $path |
||
250 | * |
||
251 | * @return string[] |
||
252 | * |
||
253 | * @throws \InvalidArgumentException when invalid path passed. |
||
254 | */ |
||
255 | protected function getPathList($path): array |
||
256 | { |
||
257 | if ($path === null) { |
||
258 | return []; |
||
259 | } |
||
260 | |||
261 | if (is_string($path)) { |
||
262 | $path = explode($this->pathDelimiter, $path); |
||
263 | } |
||
264 | |||
265 | if (is_numeric($path)) { |
||
266 | $path = [strval($path)]; |
||
267 | } |
||
268 | |||
269 | if (!is_array($path)) { |
||
270 | $type = gettype($path); |
||
271 | throw new \InvalidArgumentException("Path must be numeric, string or array, {$type} given"); |
||
272 | } |
||
273 | |||
274 | return $path; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Handle path errors. |
||
279 | * |
||
280 | * @param string $key |
||
281 | * @param string[] $path |
||
282 | * @param bool $isResultMultiple |
||
283 | * @param bool $strict |
||
284 | * |
||
285 | * @return null|array{} |
||
286 | * |
||
287 | * @throws PathNotExistException always in strict mode. |
||
288 | */ |
||
289 | protected function handleError(string $key, array $path, bool $isResultMultiple, bool $strict): ?array |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param string|string[]|null $path |
||
300 | * @param bool $strict |
||
301 | * @return array{mixed, bool, bool, bool} |
||
302 | */ |
||
303 | public function getInternal($path = null, bool $strict = true): array |
||
404 | ]; |
||
405 | } |
||
406 | } |
||
407 |