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 Relation |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | public $with; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $phpName; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $local; |
||
35 | |||
36 | /** |
||
37 | * @var Column |
||
38 | */ |
||
39 | public $localColumn; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $foreign; |
||
45 | |||
46 | /** |
||
47 | * @var Column |
||
48 | */ |
||
49 | public $foreignColumn; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | public $onDelete = 'RESTRICT'; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | public $onUpdate = 'RESTRICT'; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | public $isForeignKey; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | public $type; |
||
70 | |||
71 | /** |
||
72 | * @var Table |
||
73 | */ |
||
74 | protected $localTable; |
||
75 | |||
76 | /** |
||
77 | * @var Table |
||
78 | */ |
||
79 | protected $relatedTable; |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @param string $with |
||
84 | * @param array $data |
||
85 | * @param bool $isForeignKey |
||
86 | */ |
||
87 | 18 | public function __construct($with, array $data, $isForeignKey = true) |
|
101 | |||
102 | |||
103 | /** |
||
104 | * @return Table |
||
105 | */ |
||
106 | 1 | public function getLocalTable() |
|
110 | |||
111 | /** |
||
112 | * @param Table $table |
||
113 | */ |
||
114 | 3 | public function setLocalTable(Table $table) |
|
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | * |
||
123 | * @codeCoverageIgnore LogicException cannot be reached by a test |
||
124 | */ |
||
125 | View Code Duplication | public function getTypeConstantName() |
|
139 | |||
140 | /** |
||
141 | * @return Table |
||
142 | */ |
||
143 | 1 | public function getRelatedTable() |
|
147 | |||
148 | /** |
||
149 | * @param Table $relatedTable |
||
150 | */ |
||
151 | 3 | public function setRelatedTable(Table $relatedTable) |
|
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | 1 | public function isForeignKey() |
|
164 | |||
165 | /** |
||
166 | * @param bool $firstLetterUpper True if the first letter must be upper case |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | 3 | public function getPhpName($firstLetterUpper = true) |
|
178 | |||
179 | /** |
||
180 | * @return bool |
||
181 | */ |
||
182 | 5 | public function isMany() |
|
189 | |||
190 | /** |
||
191 | * @return Relation |
||
192 | */ |
||
193 | 2 | public function getRelatedRelation() |
|
209 | } |
||
210 |
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.