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 |
||
17 | class CharsetFilter |
||
18 | { |
||
19 | /** |
||
20 | * String representations of encodings. |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected static $encodings = [ |
||
25 | Encoding::ISO_8859_1 => 'ISO-8859-1', |
||
26 | Encoding::UTF_8 => 'UTF-8', |
||
27 | Encoding::UTF_16 => 'UTF-16', |
||
28 | Encoding::UTF_16BE => 'UTF-16BE', |
||
29 | Encoding::UTF_16LE => 'UTF-16LE' |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * The internal encoding. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | private $encoding; |
||
38 | |||
39 | /** |
||
40 | * Create charset filter object. |
||
41 | * |
||
42 | * @param int $encoding |
||
43 | */ |
||
44 | public function __construct($encoding = Encoding::UTF_8) |
||
48 | |||
49 | /** |
||
50 | * Encode string. |
||
51 | * |
||
52 | * @param string $string |
||
53 | * @param int $encoding |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | View Code Duplication | public function encode($string, $encoding = Encoding::UTF_8) |
|
65 | |||
66 | /** |
||
67 | * Decode string. |
||
68 | * |
||
69 | * @param string $string |
||
70 | * @param int $encoding |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | View Code Duplication | public function decode($string, $encoding = Encoding::UTF_8) |
|
82 | } |
||
83 |
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.