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 |
||
12 | class NSA |
||
13 | { |
||
14 | /** |
||
15 | * Get a constant of an object. You may provide the class name (including namespace) instead of an object. |
||
16 | * |
||
17 | * @param object|string $objectOrClass |
||
18 | * @param string $constantName |
||
19 | * |
||
20 | * @return mixed |
||
21 | * |
||
22 | * @throws \InvalidArgumentException |
||
23 | * @throws \LogicException |
||
24 | */ |
||
25 | 3 | public static function getConstant($objectOrClass, $constantName) |
|
42 | |||
43 | /** |
||
44 | * Get a property of an object. If the property is static you may provide the class name (including namespace) |
||
45 | * instead of an object. |
||
46 | * |
||
47 | * @param object|string $objectOrClass |
||
48 | * @param string $propertyName |
||
49 | * |
||
50 | * @return mixed |
||
51 | * |
||
52 | * @throws \InvalidArgumentException |
||
53 | * @throws \LogicException |
||
54 | */ |
||
55 | 25 | View Code Duplication | public static function getProperty($objectOrClass, $propertyName) |
66 | |||
67 | /** |
||
68 | * Set a property to an object. If the property is static you may provide the class name (including namespace) |
||
69 | * instead of an object. |
||
70 | * |
||
71 | * @param object|string $objectOrClass |
||
72 | * @param string $propertyName |
||
73 | * @param mixed $value |
||
74 | * |
||
75 | * @throws \InvalidArgumentException |
||
76 | * @throws \LogicException |
||
77 | */ |
||
78 | 9 | View Code Duplication | public static function setProperty($objectOrClass, $propertyName, $value) |
89 | |||
90 | /** |
||
91 | * Invoke a method on a object and get the return values. If the method is static you may provide the class |
||
92 | * name (including namespace) instead of an object. |
||
93 | * |
||
94 | * @param object|string $objectOrClass |
||
95 | * @param string $methodName |
||
96 | * @param mixed ...$params |
||
97 | * |
||
98 | * @return mixed |
||
99 | * |
||
100 | * @throws \InvalidArgumentException |
||
101 | * @throws \LogicException |
||
102 | */ |
||
103 | 16 | public static function invokeMethod() |
|
138 | |||
139 | /** |
||
140 | * Get a reflection class that has this constant. |
||
141 | * |
||
142 | * @param string $class |
||
143 | * @param string $constantName |
||
144 | * |
||
145 | * @return \ReflectionClass|null |
||
146 | * |
||
147 | * @throws \InvalidArgumentException |
||
148 | */ |
||
149 | 3 | View Code Duplication | protected static function getReflectionClassWithConstant($class, $constantName) |
166 | |||
167 | /** |
||
168 | * Get a reflection class that has this property. |
||
169 | * |
||
170 | * @param string $class |
||
171 | * @param string $propertyName |
||
172 | * |
||
173 | * @return \ReflectionClass|null |
||
174 | * |
||
175 | * @throws \InvalidArgumentException |
||
176 | */ |
||
177 | 21 | View Code Duplication | protected static function getReflectionClassWithProperty($class, $propertyName) |
194 | |||
195 | /** |
||
196 | * Get an reflection property that you can access directly. |
||
197 | * |
||
198 | * @param object|string $objectOrClass |
||
199 | * @param string $propertyName |
||
200 | * |
||
201 | * @return \ReflectionProperty |
||
202 | * |
||
203 | * @throws \InvalidArgumentException |
||
204 | * @throws \LogicException if the property is not found on the object |
||
205 | */ |
||
206 | 25 | protected static function getAccessibleReflectionProperty($objectOrClass, $propertyName) |
|
230 | |||
231 | /** |
||
232 | * Get all property names on a class or object. |
||
233 | * |
||
234 | * @param object|string $objectOrClass |
||
235 | * |
||
236 | * @return array of strings |
||
237 | * |
||
238 | * @throws \InvalidArgumentException |
||
239 | */ |
||
240 | 18 | public static function getProperties($objectOrClass) |
|
263 | } |
||
264 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.