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 |
||
19 | class Column |
||
20 | { |
||
21 | /** |
||
22 | * @var Table |
||
23 | */ |
||
24 | protected $table; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $name; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $phpName; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | public $type; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | public $size; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | public $decimal; |
||
50 | |||
51 | /** |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $default; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | public $isRequired = true; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | public $isPrimaryKey = false; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | public $isAutoIncrement = false; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | public $values; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | public $description; |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @param string $name |
||
84 | * @param array $data |
||
85 | */ |
||
86 | 36 | public function __construct($name, array $data) |
|
100 | |||
101 | /** |
||
102 | * @return Table |
||
103 | */ |
||
104 | 1 | public function getTable() |
|
108 | |||
109 | /** |
||
110 | * @param Table $table |
||
111 | */ |
||
112 | 1 | public function setTable(Table $table) |
|
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 19 | View Code Duplication | public function getTypeConstantName() |
133 | |||
134 | /** |
||
135 | * @param bool $raw |
||
136 | * |
||
137 | * @return int|mixed |
||
138 | */ |
||
139 | 2 | public function getDefault($raw = false) |
|
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | 1 | public function getAttributePhpDoc() |
|
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | 11 | public function getTypeAsPhpDoc() |
|
192 | |||
193 | /** |
||
194 | * @param bool $firstLetterUpper |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 3 | public function getPhpName($firstLetterUpper = false) |
|
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getMethodName() |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 9 | public function getTypeAsString() |
|
230 | } |
||
231 |
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.