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 |
||
8 | trait PropertyTrait |
||
9 | { |
||
10 | protected $properties = []; |
||
11 | |||
12 | /** |
||
13 | * Gets the fully qualified entity name (with the namespace). |
||
14 | * |
||
15 | * Must be implemented by classes using this trait |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | abstract public function getName(); |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Gets the filename of the file in which the class has been defined. |
||
24 | * |
||
25 | * Must be implemented by classes using this trait. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | abstract public function getFileName(); |
||
30 | |||
31 | /** |
||
32 | * Gets an array of properties, with inherited ones. |
||
33 | * |
||
34 | * @param int $filter Any combination of ReflectionProperty::IS_STATIC, ReflectionProperty::IS_PUBLIC, ReflectionProperty::IS_PROTECTED, ReflectionProperty::IS_PRIVATE. |
||
35 | * |
||
36 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty[] |
||
37 | */ |
||
38 | 195 | public function getProperties($filter = null) |
|
60 | |||
61 | /** |
||
62 | * Gets an array of static properties, with inherited ones. |
||
63 | * |
||
64 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty[] |
||
65 | */ |
||
66 | 51 | public function getStaticProperties() |
|
70 | |||
71 | /** |
||
72 | * Gets an array of properties, without inherited ones. |
||
73 | * |
||
74 | * @param int $filter Any combination of ReflectionProperty::IS_STATIC, ReflectionProperty::IS_PUBLIC, ReflectionProperty::IS_PROTECTED, ReflectionProperty::IS_PRIVATE. |
||
75 | * |
||
76 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty[] |
||
77 | */ |
||
78 | 207 | public function getSelfProperties($filter = null) |
|
87 | |||
88 | /** |
||
89 | * Gets a ReflectionProperty for an entity. |
||
90 | * |
||
91 | * @param string $propertySearchedName The property name to reflect |
||
92 | * |
||
93 | * @throws \ReflectionException If the property does not exist |
||
94 | * |
||
95 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty |
||
96 | */ |
||
97 | 63 | public function getProperty($propertySearchedName) |
|
107 | |||
108 | /** |
||
109 | * Checks if a property is defined. |
||
110 | * |
||
111 | * @param string $propertySearchedName Name of the property being checked for |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | 9 | public function hasProperty($propertySearchedName) |
|
125 | |||
126 | /** |
||
127 | * Gets default properties values, with inherited ones. |
||
128 | * |
||
129 | * @return mixed[] |
||
130 | */ |
||
131 | 12 | public function getDefaultProperties() |
|
140 | |||
141 | /** |
||
142 | * Gets static property value. |
||
143 | * |
||
144 | * @param string $name The name of the static property |
||
145 | * @param mixed $default Optional default value to return if the property does not exist |
||
146 | * |
||
147 | * @throws \ReflectionException If the property does not exist and default value is omitted. |
||
148 | * |
||
149 | * @return mixed The static property value or the default value if provided and if the property does not exist |
||
150 | */ |
||
151 | 36 | public function getStaticPropertyValue($name) |
|
166 | |||
167 | /** |
||
168 | * Sets static property value. |
||
169 | * |
||
170 | * @param string $name The property name |
||
171 | * @param string $value The new value |
||
172 | * |
||
173 | * @throws \ReflectionException If the property does not exist or is not public |
||
174 | */ |
||
175 | 15 | public function setStaticPropertyValue($name, $value) |
|
184 | |||
185 | /** |
||
186 | * Add a property to the reflected class. |
||
187 | * |
||
188 | * @param ReflectionProperty $property |
||
189 | */ |
||
190 | 786 | public function addProperty(ReflectionProperty $property) |
|
198 | |||
199 | /** |
||
200 | * Filter an array of properties. |
||
201 | * |
||
202 | * @param \Benoth\StaticReflection\Reflection\ReflectionProperty[] $properties |
||
203 | * @param int $filter Any combination of ReflectionProperty::IS_STATIC, ReflectionProperty::IS_PUBLIC, ReflectionProperty::IS_PROTECTED, ReflectionProperty::IS_PRIVATE. |
||
204 | * |
||
205 | * @return \Benoth\StaticReflection\Reflection\ReflectionProperty[] |
||
206 | */ |
||
207 | 207 | protected function filterProperties(array $properties, $filter) |
|
227 | } |
||
228 |
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.