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 |
||
10 | class Encoder |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private static $messages = [ |
||
16 | JSON_ERROR_NONE => 'No error', |
||
17 | JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
||
18 | JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)', |
||
19 | JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', |
||
20 | JSON_ERROR_SYNTAX => 'Syntax error', |
||
21 | JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * @param array $base64Decoded |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | public function encode($base64Decoded) |
||
33 | |||
34 | /** |
||
35 | * @param array $data |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | View Code Duplication | public function jsonEncode($data) |
|
49 | |||
50 | /** |
||
51 | * @param string $base64Decoded |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function base64Encode($base64Decoded) |
||
62 | } |
||
63 |
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.