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 |
||
8 | class PivotTableMethodsDescriptor implements MethodDescriptorInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var Table |
||
12 | */ |
||
13 | private $pivotTable; |
||
14 | |||
15 | private $useAlternateName = false; |
||
16 | |||
17 | /** |
||
18 | * @var ForeignKeyConstraint |
||
19 | */ |
||
20 | private $localFk; |
||
21 | |||
22 | /** |
||
23 | * @var ForeignKeyConstraint |
||
24 | */ |
||
25 | private $remoteFk; |
||
26 | /** |
||
27 | * @var NamingStrategyInterface |
||
28 | */ |
||
29 | private $namingStrategy; |
||
30 | |||
31 | /** |
||
32 | * @param Table $pivotTable The pivot table |
||
33 | * @param ForeignKeyConstraint $localFk |
||
34 | * @param ForeignKeyConstraint $remoteFk |
||
35 | * @param NamingStrategyInterface $namingStrategy |
||
36 | */ |
||
37 | public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
||
44 | |||
45 | /** |
||
46 | * Requests the use of an alternative name for this method. |
||
47 | */ |
||
48 | public function useAlternativeName() |
||
52 | |||
53 | /** |
||
54 | * Returns the name of the method to be generated. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getName() : string |
||
66 | |||
67 | /** |
||
68 | * Returns the name of the class that will be returned by the getter (short name). |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getBeanClassName(): string |
||
76 | |||
77 | /** |
||
78 | * Returns the plural name. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | View Code Duplication | private function getPluralName() : string |
|
90 | |||
91 | /** |
||
92 | * Returns the singular name. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | View Code Duplication | private function getSingularName() : string |
|
104 | |||
105 | /** |
||
106 | * Returns the code of the method. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getCode() : string |
||
189 | |||
190 | /** |
||
191 | * Returns an array of classes that needs a "use" for this method. |
||
192 | * |
||
193 | * @return string[] |
||
194 | */ |
||
195 | public function getUsedClasses() : array |
||
199 | |||
200 | /** |
||
201 | * Returns the code to past in jsonSerialize. |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getJsonSerializeCode() : string |
||
217 | } |
||
218 |
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.