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 Json |
||
19 | { |
||
20 | /** |
||
21 | * Encode a value as json. |
||
22 | * |
||
23 | * @param mixed $value |
||
24 | * |
||
25 | * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException |
||
26 | * @see \json_encode() |
||
27 | */ |
||
28 | View Code Duplication | public static function encode($value, int $options = 0, int $depth = 512): string |
|
38 | |||
39 | /** |
||
40 | * Decode a value from json. |
||
41 | * |
||
42 | * @return mixed[]|\stdClass |
||
43 | * @throws \Gnumoksha\FreeIpa\Infra\Json\JsonException |
||
44 | * @see \json_decode() |
||
45 | */ |
||
46 | View Code Duplication | public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
56 | } |
||
57 |
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.