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 |
||
11 | class EncodingHandler implements RobotsTxtInterface |
||
12 | { |
||
13 | /** |
||
14 | * Errors |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $errors = []; |
||
18 | |||
19 | /** |
||
20 | * String to convert |
||
21 | * @var string |
||
22 | */ |
||
23 | private $string; |
||
24 | |||
25 | /** |
||
26 | * String encoding |
||
27 | * @var string |
||
28 | */ |
||
29 | private $encoding; |
||
30 | |||
31 | /** |
||
32 | * EncodingHandler constructor. |
||
33 | * |
||
34 | * @param string $string |
||
35 | * @param string $encoding |
||
36 | */ |
||
37 | public function __construct($string, $encoding) |
||
42 | |||
43 | /** |
||
44 | * Auto mode |
||
45 | * |
||
46 | * @return string|false |
||
47 | */ |
||
48 | public function auto() |
||
74 | |||
75 | /** |
||
76 | * intl |
||
77 | * @link http://php.net/manual/en/uconverter.convert.php |
||
78 | * |
||
79 | * @return string|false |
||
80 | */ |
||
81 | View Code Duplication | public function intl() |
|
92 | |||
93 | /** |
||
94 | * iconv |
||
95 | * @link http://php.net/manual/en/function.iconv.php |
||
96 | * |
||
97 | * @param string $outSuffix |
||
98 | * @return string|false |
||
99 | */ |
||
100 | View Code Duplication | public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
|
110 | |||
111 | /** |
||
112 | * xml |
||
113 | * @link http://php.net/manual/en/function.utf8-encode.php |
||
114 | * |
||
115 | * @return string|false |
||
116 | */ |
||
117 | View Code Duplication | public function xml() |
|
130 | |||
131 | /** |
||
132 | * mbstring |
||
133 | * @link http://php.net/manual/en/function.mb-convert-encoding.php |
||
134 | * |
||
135 | * @param array|string|null $fromOverride |
||
136 | * @return string|false |
||
137 | */ |
||
138 | public function mbstring($fromOverride = null) |
||
148 | |||
149 | /** |
||
150 | * Custom error handler |
||
151 | * |
||
152 | * @param int $errNo |
||
153 | * @param string $errStr |
||
154 | * @param string $errFile |
||
155 | * @param string $errLine |
||
156 | * @return bool |
||
157 | */ |
||
158 | protected function customErrorHandler($errNo, $errStr, $errFile, $errLine) |
||
168 | } |
||
169 |
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.