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 |
||
21 | final class DefaultPropertyMetadata implements PropertyMetadata |
||
22 | { |
||
23 | /** |
||
24 | * The property name |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $name; |
||
29 | |||
30 | /** |
||
31 | * The property's serialized name |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $serializedName; |
||
36 | |||
37 | /** |
||
38 | * The property type |
||
39 | * |
||
40 | * @var PhpType |
||
41 | */ |
||
42 | private $type; |
||
43 | |||
44 | /** |
||
45 | * The property modifiers (public, private, etc) |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | private $modifiers; |
||
50 | |||
51 | /** |
||
52 | * The property declaring class metadata |
||
53 | * |
||
54 | * @var ClassMetadata |
||
55 | */ |
||
56 | private $classMetadata; |
||
57 | |||
58 | /** |
||
59 | * The property's annotations |
||
60 | * |
||
61 | * @var AnnotationSet |
||
62 | */ |
||
63 | private $annotations; |
||
64 | |||
65 | /** |
||
66 | * If the property is a virtual property |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $virtual; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * |
||
75 | * @param string $name |
||
76 | * @param string $serializedName |
||
77 | * @param PhpType $type |
||
78 | * @param int $modifiers |
||
79 | * @param ClassMetadata $classMetadata |
||
80 | * @param AnnotationSet $annotations |
||
81 | * @param bool $virtual |
||
82 | */ |
||
83 | 5 | View Code Duplication | public function __construct( |
101 | |||
102 | /** |
||
103 | * Get the property name |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 1 | public function getName(): string |
|
111 | |||
112 | /** |
||
113 | * Get the property serialized name |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 1 | public function getSerializedName(): string |
|
121 | |||
122 | /** |
||
123 | * Get the full php type object |
||
124 | * |
||
125 | * @return PhpType |
||
126 | */ |
||
127 | 1 | public function getType(): PhpType |
|
131 | |||
132 | /** |
||
133 | * Get the property type as a string |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | public function getTypeName(): string |
|
141 | |||
142 | /** |
||
143 | * Get the property modifiers as a bitmap of [@see \ReflectionProperty] constants |
||
144 | * |
||
145 | * @return int |
||
146 | */ |
||
147 | 1 | public function getModifiers(): int |
|
151 | |||
152 | /** |
||
153 | * Get full declaring class metadata |
||
154 | * |
||
155 | * @return ClassMetadata |
||
156 | */ |
||
157 | 1 | public function getDeclaringClassMetadata(): ClassMetadata |
|
161 | |||
162 | /** |
||
163 | * Get the declaring class name |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | 1 | public function getDeclaringClassName(): string |
|
171 | |||
172 | /** |
||
173 | * Get property annotations |
||
174 | * |
||
175 | * @return AnnotationSet |
||
176 | */ |
||
177 | 1 | public function getAnnotations(): AnnotationSet |
|
181 | |||
182 | /** |
||
183 | * Get a single annotation, returns null if the annotation doesn't exist |
||
184 | * |
||
185 | * @param string $annotationName |
||
186 | * @return null|object |
||
187 | */ |
||
188 | 4 | public function getAnnotation(string $annotationName) |
|
194 | |||
195 | /** |
||
196 | * Returns true if the property is virtual |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | 1 | public function isVirtual(): bool |
|
204 | } |
||
205 |
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.