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 |
||
14 | class MixinMetadata implements Interfaces\AttributeInterface, Interfaces\EmbedInterface, Interfaces\RelationshipInterface |
||
15 | { |
||
16 | /** |
||
17 | * Uses attributes. |
||
18 | */ |
||
19 | use Traits\AttributesTrait; |
||
20 | |||
21 | /** |
||
22 | * Uses embeds. |
||
23 | */ |
||
24 | use Traits\EmbedsTrait; |
||
25 | |||
26 | /** |
||
27 | * Uses merged properties. |
||
28 | */ |
||
29 | use Traits\PropertiesTrait; |
||
30 | |||
31 | /** |
||
32 | * Uses relationships. |
||
33 | */ |
||
34 | use Traits\RelationshipsTrait; |
||
35 | |||
36 | /** |
||
37 | * The mixin name/key. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $name; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param string $name The mixin name. |
||
47 | */ |
||
48 | public function __construct($name) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function getProperties() |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | View Code Duplication | protected function applyMixinProperties(MixinMetadata $mixin) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | View Code Duplication | protected function validateAttribute(AttributeMetadata $attribute) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | View Code Duplication | protected function validateEmbed(EmbeddedPropMetadata $embed) |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | View Code Duplication | protected function validateRelationship(RelationshipMetadata $relationship) |
|
124 | } |
||
125 |
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.