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 |
||
13 | View Code Duplication | trait MetadataContainer |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * Metadata attributes |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $metadata = []; |
||
21 | |||
22 | /** |
||
23 | * Set attribute of metadata |
||
24 | * |
||
25 | * @param string $name |
||
26 | * @param $value |
||
27 | */ |
||
28 | public function setMetadataAttribute(string $name, $value) |
||
32 | |||
33 | /** |
||
34 | * Has attribute of metadata |
||
35 | * |
||
36 | * @param string $name |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function hasMetadataAttribute(string $name): bool |
||
43 | |||
44 | /** |
||
45 | * Get attribute of metadata |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function getMetadataAttribute(string $name) |
||
54 | |||
55 | /** |
||
56 | * Contains any metadata ? |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function hasMetadata(): bool |
||
64 | |||
65 | /** |
||
66 | * Get all attributes of metadata |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function getMetadata(): array |
||
74 | } |
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.