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 |
||
19 | final class JsonDecodeReader extends JsonReader |
||
20 | { |
||
21 | /** |
||
22 | * Constructor |
||
23 | * |
||
24 | * @param string $json |
||
25 | * @throws \Tebru\Gson\Exception\MalformedJsonException If the json cannot be decoded |
||
26 | */ |
||
27 | 83 | public function __construct(string $json) |
|
35 | |||
36 | /** |
||
37 | * Consumes the next token and asserts it's the beginning of a new array |
||
38 | * |
||
39 | * @return void |
||
40 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BEGIN_ARRAY |
||
41 | */ |
||
42 | 24 | View Code Duplication | public function beginArray(): void |
50 | |||
51 | /** |
||
52 | * Consumes the next token and asserts it's the beginning of a new object |
||
53 | * |
||
54 | * @return void |
||
55 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BEGIN_OBJECT |
||
56 | */ |
||
57 | 24 | public function beginObject(): void |
|
63 | |||
64 | /** |
||
65 | * Consumes the value of the next token, asserts it's a boolean and returns it |
||
66 | * |
||
67 | * @return bool |
||
68 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not BOOLEAN |
||
69 | */ |
||
70 | 6 | public function nextBoolean(): bool |
|
80 | |||
81 | /** |
||
82 | * Consumes the value of the next token, asserts it's a double and returns it |
||
83 | * |
||
84 | * @return double |
||
85 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NUMBER |
||
86 | */ |
||
87 | 3 | public function nextDouble(): float |
|
97 | |||
98 | /** |
||
99 | * Consumes the value of the next token, asserts it's an int and returns it |
||
100 | * |
||
101 | * @return int |
||
102 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NUMBER |
||
103 | */ |
||
104 | 15 | public function nextInteger(): int |
|
114 | |||
115 | /** |
||
116 | * Consumes the value of the next token, asserts it's a string and returns it |
||
117 | * |
||
118 | * @return string |
||
119 | * @throws \Tebru\Gson\Exception\UnexpectedJsonTokenException If the next token is not NAME or STRING |
||
120 | */ |
||
121 | 13 | View Code Duplication | public function nextString(): string |
136 | |||
137 | /** |
||
138 | * Returns an enum representing the type of the next token without consuming it |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 81 | public function peek(): string |
|
203 | |||
204 | /** |
||
205 | * Get the current read path in json xpath format |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 23 | View Code Duplication | public function getPath(): string |
224 | |||
225 | /** |
||
226 | * Push an element onto the stack |
||
227 | * |
||
228 | * @param mixed $element |
||
229 | * @param string $type |
||
230 | */ |
||
231 | 83 | View Code Duplication | protected function push($element, $type = null): void |
242 | } |
||
243 |
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.