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 | final class Definition |
||
20 | { |
||
21 | /** |
||
22 | * The human readable activity name |
||
23 | * @var array |
||
24 | */ |
||
25 | private $name; |
||
26 | |||
27 | /** |
||
28 | * The human readable activity description |
||
29 | * @var array |
||
30 | */ |
||
31 | private $description; |
||
32 | |||
33 | /** |
||
34 | * The type of the {@link Activity} |
||
35 | * @var string |
||
36 | */ |
||
37 | private $type; |
||
38 | |||
39 | public function __construct(array $name, array $description, $type) |
||
45 | |||
46 | /** |
||
47 | * Returns the human readable names. |
||
48 | * |
||
49 | * @return array The name language map |
||
50 | */ |
||
51 | public function getName() |
||
55 | |||
56 | /** |
||
57 | * Returns the human readable descriptions. |
||
58 | * |
||
59 | * @return array The description language map |
||
60 | */ |
||
61 | public function getDescription() |
||
65 | |||
66 | /** |
||
67 | * Returns the {@link Activity} type. |
||
68 | * |
||
69 | * @return string The type |
||
70 | */ |
||
71 | public function getType() |
||
75 | |||
76 | /** |
||
77 | * Checks if another definition is equal. |
||
78 | * |
||
79 | * Two definitions are equal if and only if all of their properties are equal. |
||
80 | * |
||
81 | * @param Definition $definition The definition to compare with |
||
82 | * |
||
83 | * @return bool True if the definitions are equal, false otherwise |
||
84 | */ |
||
85 | public function equals(Definition $definition) |
||
121 | } |
||
122 |
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.