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:
1 | <?php |
||
18 | final class JsonEncodeWriter implements JsonWritable |
||
19 | { |
||
20 | /** |
||
21 | * True if we should serialize nulls |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $serializeNull = false; |
||
26 | |||
27 | /** |
||
28 | * Stack of values to be written |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private $stack = []; |
||
33 | |||
34 | /** |
||
35 | * Size of the stack array |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | private $stackSize = 0; |
||
40 | |||
41 | /** |
||
42 | * When serializing an object, store the name that should be serialized |
||
43 | * |
||
44 | * @var |
||
45 | */ |
||
46 | private $pendingName; |
||
47 | |||
48 | /** |
||
49 | * The final result that will be json encoded |
||
50 | * |
||
51 | * @var mixed |
||
52 | */ |
||
53 | private $result; |
||
54 | |||
55 | /** |
||
56 | * Begin writing array |
||
57 | * |
||
58 | * @return JsonWritable |
||
59 | * @throws \BadMethodCallException |
||
60 | */ |
||
61 | 6 | View Code Duplication | public function beginArray(): JsonWritable |
74 | |||
75 | /** |
||
76 | * End writing array |
||
77 | * |
||
78 | * @return JsonWritable |
||
79 | * @throws \BadMethodCallException |
||
80 | */ |
||
81 | 4 | public function endArray(): JsonWritable |
|
91 | |||
92 | /** |
||
93 | * Begin writing object |
||
94 | * |
||
95 | * @return JsonWritable |
||
96 | * @throws \BadMethodCallException |
||
97 | */ |
||
98 | 16 | View Code Duplication | public function beginObject(): JsonWritable |
111 | |||
112 | /** |
||
113 | * End writing object |
||
114 | * |
||
115 | * @return JsonWritable |
||
116 | * @throws \BadMethodCallException |
||
117 | */ |
||
118 | 6 | public function endObject(): JsonWritable |
|
128 | |||
129 | /** |
||
130 | * Writes a property name |
||
131 | * |
||
132 | * @param string $name |
||
133 | * @return JsonWritable |
||
134 | * @throws \BadMethodCallException |
||
135 | */ |
||
136 | 5 | public function name(string $name): JsonWritable |
|
146 | |||
147 | /** |
||
148 | * Write an integer value |
||
149 | * |
||
150 | * @param int $value |
||
151 | * @return JsonWritable |
||
152 | * @throws \BadMethodCallException |
||
153 | */ |
||
154 | 4 | public function writeInteger(int $value): JsonWritable |
|
162 | |||
163 | /** |
||
164 | * Write a float value |
||
165 | * |
||
166 | * @param float $value |
||
167 | * @return JsonWritable |
||
168 | * @throws \BadMethodCallException |
||
169 | */ |
||
170 | 3 | public function writeFloat(float $value): JsonWritable |
|
178 | |||
179 | /** |
||
180 | * Write a string value |
||
181 | * |
||
182 | * @param string $value |
||
183 | * @return JsonWritable |
||
184 | * @throws \BadMethodCallException |
||
185 | */ |
||
186 | 4 | public function writeString(string $value): JsonWritable |
|
194 | |||
195 | /** |
||
196 | * Write a boolean value |
||
197 | * |
||
198 | * @param boolean $value |
||
199 | * @return JsonWritable |
||
200 | * @throws \BadMethodCallException |
||
201 | */ |
||
202 | 3 | public function writeBoolean(bool $value): JsonWritable |
|
210 | |||
211 | /** |
||
212 | * Write a null value if we are serializing nulls, otherwise |
||
213 | * skip the value. If this is a property value, that property |
||
214 | * should be skippped as well. |
||
215 | * |
||
216 | * @return JsonWritable |
||
217 | * @throws \BadMethodCallException |
||
218 | */ |
||
219 | 5 | View Code Duplication | public function writeNull(): JsonWritable |
237 | |||
238 | /** |
||
239 | * Sets whether nulls are serialized |
||
240 | * |
||
241 | * @param bool $serializeNull |
||
242 | */ |
||
243 | 2 | public function setSerializeNull(bool $serializeNull): void |
|
247 | |||
248 | /** |
||
249 | * Convert the writer to json |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 17 | public function __toString(): string |
|
257 | |||
258 | /** |
||
259 | * Get the last index of the stack |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | 19 | private function last(): int |
|
267 | |||
268 | /** |
||
269 | * Push a value to the result or current array/object |
||
270 | * |
||
271 | * @param mixed $value |
||
272 | * @return JsonWritable |
||
273 | * @throws \BadMethodCallException |
||
274 | */ |
||
275 | 28 | private function push(&$value): JsonWritable |
|
298 | |||
299 | /** |
||
300 | * Remove the last element of the stack |
||
301 | */ |
||
302 | 6 | private function pop(): void |
|
307 | |||
308 | /** |
||
309 | * Returns true if an object is the top element of the stack and we haven't called name() yet |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 29 | View Code Duplication | private function topIsObjectStart(): bool |
321 | |||
322 | /** |
||
323 | * Returns true if an object is the top element of the stack |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | 6 | View Code Duplication | private function topIsObject() |
335 | |||
336 | /** |
||
337 | * Returns true if an array is the top element of the stack |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 8 | private function topIsArray() |
|
349 | } |
||
350 |
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.