Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DotKey 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 DotKey, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | |||
9 | /** |
||
10 | * Access objects and arrays through dot notation. |
||
11 | * |
||
12 | * @template TSubject |
||
13 | */ |
||
14 | class DotKey |
||
15 | { |
||
16 | /** |
||
17 | * @var object|array<string,mixed> |
||
18 | * @phpstan-var TSubject&(object|array<string,mixed>) |
||
19 | */ |
||
20 | protected $subject; |
||
21 | |||
22 | /** |
||
23 | * Class constructor. |
||
24 | * |
||
25 | * @param object|array $subject |
||
26 | * |
||
27 | * @phpstan-param TSubject&(object|array<string,mixed>) $subject |
||
28 | */ |
||
29 | 109 | public function __construct($subject) |
|
30 | { |
||
31 | 109 | if (!\is_object($subject) && !\is_array($subject)) { |
|
1 ignored issue
–
show
|
|||
32 | 1 | $type = \gettype($subject); |
|
33 | 1 | throw new \InvalidArgumentException("Subject should be an array or object; $type given"); |
|
34 | } |
||
35 | |||
36 | 108 | $this->subject = $subject; |
|
37 | 108 | } |
|
38 | |||
39 | |||
40 | /** |
||
41 | * Check if path exists in subject. |
||
42 | */ |
||
43 | 10 | public function exists(string $path, string $delimiter = '.'): bool |
|
44 | { |
||
45 | 10 | $subject = $this->subject; |
|
46 | 10 | $index = $this->splitPath($path, $delimiter); |
|
47 | ; |
||
48 | |||
49 | 10 | foreach ($index as $key) { |
|
50 | 10 | if (!\is_array($subject) && !\is_object($subject)) { |
|
51 | 4 | return false; |
|
52 | } |
||
53 | |||
54 | 10 | $subject = $this->descend($subject, $key, $exists, true); |
|
55 | |||
56 | 10 | if (!$exists) { |
|
57 | 10 | return false; |
|
58 | } |
||
59 | } |
||
60 | |||
61 | 10 | return true; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get a value from subject by path. |
||
66 | * |
||
67 | * @param string $path |
||
68 | * @param string $delimiter |
||
69 | * @return mixed |
||
70 | * @throws ResolveException |
||
71 | */ |
||
72 | 19 | public function get(string $path, string $delimiter = '.') |
|
73 | { |
||
74 | 19 | $subject = $this->subject; |
|
75 | 19 | $index = $this->splitPath($path, $delimiter); |
|
76 | |||
77 | 17 | while ($index !== []) { |
|
78 | 17 | $key = \array_shift($index); |
|
79 | |||
80 | 17 | if (!\is_array($subject) && !\is_object($subject)) { |
|
81 | 5 | $msg = "Unable to get '$path': '%s' is of type " . \gettype($subject); |
|
82 | 5 | throw $this->unresolved($msg, $path, $delimiter, $index, $key); |
|
83 | } |
||
84 | |||
85 | try { |
||
86 | 17 | $subject = $this->descend($subject, $key, $exists); |
|
87 | 3 | } catch (\Error $error) { |
|
88 | 3 | $msg = "Unable to get '$path': error at '%s'"; |
|
89 | 3 | throw $this->unresolved($msg, $path, $delimiter, $index, null, $error); |
|
90 | } |
||
91 | |||
92 | 15 | if (!$exists) { |
|
93 | 9 | return null; |
|
94 | } |
||
95 | } |
||
96 | |||
97 | 9 | return $subject; |
|
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Set a value within the subject by path. |
||
103 | * |
||
104 | * @param string $path |
||
105 | * @param mixed $value |
||
106 | * @param string $delimiter |
||
107 | * @return array|object |
||
108 | * @throws ResolveException |
||
109 | * |
||
110 | * @phpstan-return TSubject&(object|array<string,mixed>) |
||
111 | */ |
||
112 | 23 | public function set(string $path, $value, string $delimiter = '.') |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Set a value, creating a structure if needed. |
||
154 | * |
||
155 | * @param string $path |
||
156 | * @param mixed $value |
||
157 | * @param string $delimiter |
||
158 | * @param bool|null $assoc Create new structure as array. Omit to base upon subject type. |
||
159 | * @return array|object |
||
160 | * @throws ResolveException |
||
161 | * |
||
162 | * @phpstan-return TSubject&(object|array<string,mixed>) |
||
163 | */ |
||
164 | 30 | public function put(string $path, $value, string $delimiter = '.', ?bool $assoc = null) |
|
165 | { |
||
166 | 30 | $result = $this->subject; |
|
167 | 30 | $subject =& $result; |
|
168 | |||
169 | 30 | $index = $this->splitPath($path, $delimiter); |
|
170 | |||
171 | 29 | while (count($index) > 1) { |
|
172 | 28 | $key = \array_shift($index); |
|
173 | |||
174 | try { |
||
175 | 28 | $subject =& $this->descend($subject, $key, $exists); |
|
176 | 1 | } catch (\Error $error) { |
|
177 | 1 | $msg = "Unable to put '$path': error at '%s'"; |
|
178 | 1 | throw $this->unresolved($msg, $path, $delimiter, $index, null, $error); |
|
179 | } |
||
180 | |||
181 | 27 | if (!$exists) { |
|
182 | 13 | array_unshift($index, $key); |
|
183 | 13 | break; |
|
184 | } |
||
185 | |||
186 | 27 | if (!\is_array($subject) && !\is_object($subject)) { |
|
187 | 4 | break; |
|
188 | } |
||
189 | } |
||
190 | |||
191 | try { |
||
192 | 28 | $this->setValueCreate($subject, $index, $value, $assoc); |
|
193 | 2 | } catch (\Error $error) { |
|
194 | 2 | $msg = "Unable to put '$path': error at '%s'"; |
|
195 | 2 | throw $this->unresolved($msg, $path, $delimiter, array_slice($index, 0, -1), null, $error); |
|
196 | } |
||
197 | |||
198 | 26 | return $result; |
|
199 | } |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Create property and set the value. |
||
204 | * |
||
205 | * @param mixed $subject |
||
206 | * @param string[] $index Part or the path that doesn't exist |
||
207 | * @param mixed $value |
||
208 | * @param bool|null $assoc |
||
209 | */ |
||
210 | 28 | protected function setValueCreate(&$subject, array $index, $value, ?bool $assoc = null): void |
|
230 | 26 | } |
|
231 | |||
232 | /** |
||
233 | * Get a particular value back from the config array |
||
234 | * |
||
235 | * @return object|array<string,mixed> |
||
236 | * |
||
237 | * @phpstan-return TSubject&(object|array<string,mixed>) |
||
238 | */ |
||
239 | 26 | public function remove(string $path, string $delimiter = '.') |
|
240 | { |
||
241 | 26 | $result = $this->subject; |
|
242 | 26 | $subject =& $result; |
|
243 | |||
244 | 26 | $index = $this->splitPath($path, $delimiter); |
|
245 | |||
246 | 25 | while (\count($index) > 1) { |
|
247 | 24 | $key = \array_shift($index); |
|
248 | |||
249 | try { |
||
250 | 24 | $subject =& $this->descend($subject, $key, $exists); |
|
251 | 1 | } catch (\Error $error) { |
|
252 | 1 | $msg = "Unable to remove '$path': error at '%s'"; |
|
253 | 1 | throw $this->unresolved($msg, $path, $delimiter, $index, null, $error); |
|
413 |