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 |
||
14 | class Decoder extends Converter |
||
15 | { |
||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $ignoreByteOrderMark = true; |
||
20 | |||
21 | /** |
||
22 | * Decoder constructor. |
||
23 | * |
||
24 | * @param bool $ignoreByteOrderMark |
||
25 | * |
||
26 | * @throws \Crossjoin\Json\Exception\InvalidArgumentException |
||
27 | */ |
||
28 | public function __construct($ignoreByteOrderMark = true) |
||
32 | |||
33 | /** |
||
34 | * @return boolean |
||
35 | */ |
||
36 | public function getIgnoreByteOrderMark() |
||
40 | |||
41 | /** |
||
42 | * @param boolean $ignoreByteOrderMark |
||
43 | * |
||
44 | * @throws \Crossjoin\Json\Exception\InvalidArgumentException |
||
45 | */ |
||
46 | public function setIgnoreByteOrderMark($ignoreByteOrderMark) |
||
60 | |||
61 | /** |
||
62 | * Gets the encoding of the JSON text. |
||
63 | * |
||
64 | * @param string $json |
||
65 | * |
||
66 | * @return string |
||
67 | * @throws \Crossjoin\Json\Exception\InvalidArgumentException |
||
68 | * @throws \Crossjoin\Json\Exception\EncodingNotSupportedException |
||
69 | */ |
||
70 | public function getEncoding($json) |
||
119 | |||
120 | /** @noinspection MoreThanThreeArgumentsInspection */ |
||
121 | /** |
||
122 | * Parses a valid JSON text that is encoded as UTF-8, UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE |
||
123 | * and returns the data as UTF-8. |
||
124 | * |
||
125 | * @param string $json |
||
126 | * @param bool $assoc |
||
127 | * @param int $depth |
||
128 | * @param int $options |
||
129 | * |
||
130 | * @return mixed |
||
131 | * @throws \Crossjoin\Json\Exception\NativeJsonErrorException |
||
132 | * @throws \Crossjoin\Json\Exception\ConversionFailedException |
||
133 | * @throws \Crossjoin\Json\Exception\InvalidArgumentException |
||
134 | * @throws \Crossjoin\Json\Exception\EncodingNotSupportedException |
||
135 | * @throws \Crossjoin\Json\Exception\ExtensionRequiredException |
||
136 | */ |
||
137 | public function decode($json, $assoc = false, $depth = 512, $options = 0) |
||
162 | |||
163 | /** @noinspection MoreThanThreeArgumentsInspection */ |
||
164 | /** |
||
165 | * @param string $json |
||
166 | * @param bool $assoc |
||
167 | * @param int $depth |
||
168 | * @param int $options |
||
169 | * |
||
170 | * @return mixed |
||
171 | * @throws \Crossjoin\Json\Exception\NativeJsonErrorException |
||
172 | */ |
||
173 | View Code Duplication | private function decodePhpGte54($json, $assoc, $depth, $options) |
|
183 | |||
184 | /** |
||
185 | * @param string $json |
||
186 | * @param bool $assoc |
||
187 | * @param int $depth |
||
188 | * |
||
189 | * @return mixed |
||
190 | * @throws \Crossjoin\Json\Exception\NativeJsonErrorException |
||
191 | */ |
||
192 | View Code Duplication | private function decodePhpLt54($json, $assoc, $depth) |
|
202 | |||
203 | /** |
||
204 | * @param string $json |
||
205 | * |
||
206 | * @return string |
||
207 | * @throws \Crossjoin\Json\Exception\InvalidArgumentException |
||
208 | */ |
||
209 | private function getEncodingBytes($json) |
||
222 | |||
223 | /** |
||
224 | * @param string $json |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | private function prepareJson($json) |
||
249 | } |
||
250 |
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.