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 |
||
20 | class DefaultParser implements ParserInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $quote; |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function setQuote($quote) |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function comparison($field, $comparison, $value) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function normalizeGroupValue($value) |
||
53 | |||
54 | /** |
||
55 | * @param string $comparison |
||
56 | * @param mixed $value |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | protected function transformComparison($comparison, $value) |
||
74 | |||
75 | /** |
||
76 | * Normalize value. |
||
77 | * |
||
78 | * @param mixed $value |
||
79 | * |
||
80 | * @return array|float|int|string |
||
81 | */ |
||
82 | protected function normalizeValue($value) |
||
83 | { |
||
84 | if (null === $value) { |
||
85 | $value = "NULL"; |
||
86 | } elseif (is_array($value)) { |
||
87 | foreach ($value as $index => $val) { |
||
88 | $value[$index] = $this->normalizeValue($val); |
||
89 | } |
||
90 | } elseif (is_string($value) && !preg_match('/^\(/', $value)) { |
||
91 | $value = sprintf('\'%s\'', addslashes($value)); |
||
92 | } elseif (is_int($value)) { |
||
93 | $value = (int) $value; |
||
94 | } elseif (is_float($value)) { |
||
95 | $value = (float) $value; |
||
96 | } elseif (is_bool($value)) { |
||
97 | $value = $value === true ? 'true' : 'false'; |
||
98 | } |
||
99 | |||
100 | return $value; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Normalize collection value. |
||
105 | * |
||
106 | * @param mixed $value |
||
107 | * |
||
108 | * @return array|string |
||
109 | */ |
||
110 | protected function normalizeCollectionValue($value) |
||
126 | |||
127 | /** |
||
128 | * Quoting |
||
129 | * |
||
130 | * @param string $field |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | View Code Duplication | protected function quote($field) |
|
145 | } |
||
146 |
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.