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 |
||
32 | View Code Duplication | abstract class AbstractBeanAnnotation extends AbstractAnnotation |
|
|
|||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The value of the name attribute. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $name; |
||
41 | |||
42 | /** |
||
43 | * The value of the description attribute. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $description; |
||
48 | |||
49 | /** |
||
50 | * The value of the lookup attribute. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $lookup; |
||
55 | |||
56 | /** |
||
57 | * The value of the shared attribute. |
||
58 | * |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $shared = true; |
||
62 | |||
63 | /** |
||
64 | * The constructor the initializes the instance with the |
||
65 | * data passed with the token. |
||
66 | * |
||
67 | * @param array $values The annotation values |
||
68 | */ |
||
69 | public function __construct(array $values = array()) |
||
92 | |||
93 | /** |
||
94 | * Returns the value of the name attribute. |
||
95 | * |
||
96 | * @return string|null The annotations name attribute |
||
97 | */ |
||
98 | public function getName() |
||
102 | |||
103 | /** |
||
104 | * Returns the value of the description attribute. |
||
105 | * |
||
106 | * @return string|null The annotations description attribute |
||
107 | */ |
||
108 | public function getDescription() |
||
112 | |||
113 | /** |
||
114 | * Returns the value of the lookup attribute. |
||
115 | * |
||
116 | * @return string|null The annotations lookup attribute |
||
117 | */ |
||
118 | public function getLookup() |
||
122 | |||
123 | /** |
||
124 | * Returns the value of the shared attribute. |
||
125 | * |
||
126 | * @return boolean The annotations shared attribute |
||
127 | */ |
||
128 | public function getShared() |
||
132 | } |
||
133 |
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.