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 JsonElementWriter 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 JsonElementWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | final class JsonElementWriter implements JsonWritable, JsonSerializable |
||
24 | { |
||
25 | /** |
||
26 | * True if we should serialize nulls |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $serializeNull = false; |
||
31 | |||
32 | /** |
||
33 | * Stack of values to be written |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $stack = []; |
||
38 | |||
39 | /** |
||
40 | * Size of the stack array |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | private $stackSize = 0; |
||
45 | |||
46 | /** |
||
47 | * When serializing an object, store the name that should be serialized |
||
48 | * |
||
49 | * @var |
||
50 | */ |
||
51 | private $pendingName; |
||
52 | |||
53 | /** |
||
54 | * The final result that will be json encoded |
||
55 | * |
||
56 | * @var JsonElement |
||
57 | */ |
||
58 | private $result; |
||
59 | |||
60 | /** |
||
61 | * Begin writing array |
||
62 | * |
||
63 | * @return JsonWritable |
||
64 | * @throws \BadMethodCallException |
||
65 | */ |
||
66 | 6 | View Code Duplication | public function beginArray(): JsonWritable |
79 | |||
80 | /** |
||
81 | * End writing array |
||
82 | * |
||
83 | * @return JsonWritable |
||
84 | * @throws \BadMethodCallException |
||
85 | */ |
||
86 | 4 | public function endArray(): JsonWritable |
|
96 | |||
97 | /** |
||
98 | * Begin writing object |
||
99 | * |
||
100 | * @return JsonWritable |
||
101 | * @throws \BadMethodCallException |
||
102 | */ |
||
103 | 17 | View Code Duplication | public function beginObject(): JsonWritable |
116 | |||
117 | /** |
||
118 | * End writing object |
||
119 | * |
||
120 | * @return JsonWritable |
||
121 | * @throws \BadMethodCallException |
||
122 | */ |
||
123 | 7 | public function endObject(): JsonWritable |
|
133 | |||
134 | /** |
||
135 | * Writes a property name |
||
136 | * |
||
137 | * @param string $name |
||
138 | * @return JsonWritable |
||
139 | * @throws \BadMethodCallException |
||
140 | */ |
||
141 | 5 | public function name(string $name): JsonWritable |
|
151 | |||
152 | /** |
||
153 | * Write an integer value |
||
154 | * |
||
155 | * @param int $value |
||
156 | * @return JsonWritable |
||
157 | * @throws \BadMethodCallException |
||
158 | */ |
||
159 | 4 | public function writeInteger(int $value): JsonWritable |
|
167 | |||
168 | /** |
||
169 | * Write a float value |
||
170 | * |
||
171 | * @param float $value |
||
172 | * @return JsonWritable |
||
173 | * @throws \BadMethodCallException |
||
174 | */ |
||
175 | 3 | public function writeFloat(float $value): JsonWritable |
|
183 | |||
184 | /** |
||
185 | * Write a string value |
||
186 | * |
||
187 | * @param string $value |
||
188 | * @return JsonWritable |
||
189 | * @throws \BadMethodCallException |
||
190 | */ |
||
191 | 4 | public function writeString(string $value): JsonWritable |
|
199 | |||
200 | /** |
||
201 | * Write a boolean value |
||
202 | * |
||
203 | * @param boolean $value |
||
204 | * @return JsonWritable |
||
205 | * @throws \BadMethodCallException |
||
206 | */ |
||
207 | 3 | public function writeBoolean(bool $value): JsonWritable |
|
215 | |||
216 | /** |
||
217 | * Write a null value if we are serializing nulls, otherwise |
||
218 | * skip the value. If this is a property value, that property |
||
219 | * should be skippped as well. |
||
220 | * |
||
221 | * @return JsonWritable |
||
222 | * @throws \BadMethodCallException |
||
223 | */ |
||
224 | 5 | View Code Duplication | public function writeNull(): JsonWritable |
241 | |||
242 | /** |
||
243 | * Sets whether nulls are serialized |
||
244 | * |
||
245 | * @param bool $serializeNull |
||
246 | */ |
||
247 | 2 | public function setSerializeNull(bool $serializeNull): void |
|
251 | |||
252 | /** |
||
253 | * Specify data which should be serialized to JSON |
||
254 | * |
||
255 | * @return mixed |
||
256 | */ |
||
257 | 17 | public function jsonSerialize() |
|
265 | |||
266 | /** |
||
267 | * Get the result as a json element |
||
268 | * |
||
269 | * @return JsonElement |
||
270 | */ |
||
271 | 1 | public function toJsonElement(): JsonElement |
|
275 | |||
276 | /** |
||
277 | * Get the last index of the stack |
||
278 | * |
||
279 | * @return int |
||
280 | */ |
||
281 | 20 | private function last(): int |
|
285 | |||
286 | /** |
||
287 | * Push a value to the result or current array/object |
||
288 | * |
||
289 | * @param JsonElement $value |
||
290 | * @return JsonWritable |
||
291 | * @throws \BadMethodCallException |
||
292 | */ |
||
293 | 29 | private function push(JsonElement $value): JsonWritable |
|
316 | |||
317 | /** |
||
318 | * Remove the last element of the stack |
||
319 | */ |
||
320 | 7 | private function pop(): void |
|
325 | |||
326 | /** |
||
327 | * Returns true if an object is the top element of the stack and we haven't called name() yet |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | 30 | private function topIsObjectStart(): bool |
|
339 | |||
340 | /** |
||
341 | * Returns true if an object is the top element of the stack |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 7 | private function topIsObject() |
|
353 | |||
354 | /** |
||
355 | * Returns true if an array is the top element of the stack |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | 8 | private function topIsArray() |
|
367 | } |
||
368 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.