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 |
||
5 | class ConfigGenerator |
||
6 | { |
||
7 | /** |
||
8 | * Generate php source text. |
||
9 | * |
||
10 | * @param array $config |
||
11 | * @param string $namespace |
||
12 | * |
||
13 | * @return static |
||
14 | */ |
||
15 | 3 | public static function generateText(array $config, $namespace = null) |
|
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $text; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $indent; |
||
31 | |||
32 | /** |
||
33 | * Generate php source text. |
||
34 | * |
||
35 | * @param array $config |
||
36 | * @param string $namespace |
||
37 | * |
||
38 | * @return string |
||
39 | */ |
||
40 | 5 | public function generate(array $config, $namespace = null) |
|
58 | |||
59 | /** |
||
60 | * Generate php array elements |
||
61 | * |
||
62 | * @param array $config |
||
63 | */ |
||
64 | 5 | private function generateArray(array $config) |
|
114 | |||
115 | /** |
||
116 | * Write a source line. |
||
117 | * |
||
118 | * @param string $line |
||
119 | */ |
||
120 | 5 | private function writeLine($line) |
|
126 | } |
||
127 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: