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 | public static function getProperty($objectOrClass, $propertyName) |
|
59 | |||
60 | /** |
||
61 | * Set a property to an object. If the property is static you may provide the class name (including namespace) |
||
62 | * instead of an object. |
||
63 | * |
||
64 | * @param object|string $objectOrClass |
||
65 | * @param string $propertyName |
||
66 | * @param mixed $value |
||
67 | * |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @throws \LogicException |
||
70 | */ |
||
71 | 9 | public static function setProperty($objectOrClass, $propertyName, $value) |
|
75 | |||
76 | /** |
||
77 | * Invoke a method on a object and get the return values. If the method is static you may provide the class |
||
78 | * name (including namespace) instead of an object. |
||
79 | * |
||
80 | * @param object|string $objectOrClass |
||
81 | * @param string $methodName |
||
82 | * @param mixed ...$params |
||
83 | * |
||
84 | * @return mixed |
||
85 | * |
||
86 | * @throws \InvalidArgumentException |
||
87 | * @throws \LogicException |
||
88 | */ |
||
89 | 16 | public static function invokeMethod() |
|
124 | |||
125 | /** |
||
126 | * Get a reflection class that has this constant. |
||
127 | * |
||
128 | * @param string $class |
||
129 | * @param string $constantName |
||
130 | * |
||
131 | * @return \ReflectionClass|null |
||
132 | * |
||
133 | * @throws \InvalidArgumentException |
||
134 | */ |
||
135 | 3 | View Code Duplication | protected static function getReflectionClassWithConstant($class, $constantName) |
152 | /** |
||
153 | * Get a reflection class that has this property. |
||
154 | * |
||
155 | * @param string $class |
||
156 | * @param string $propertyName |
||
157 | * |
||
158 | * @return \ReflectionClass|null |
||
159 | * |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | 21 | View Code Duplication | protected static function getReflectionClassWithProperty($class, $propertyName) |
179 | |||
180 | /** |
||
181 | * Get an reflection property that you can access directly. |
||
182 | * |
||
183 | * @param object|string $objectOrClass |
||
184 | * @param string $propertyName |
||
185 | * |
||
186 | * @return \ReflectionProperty |
||
187 | * |
||
188 | * @throws \InvalidArgumentException |
||
189 | * @throws \LogicException if the property is not found on the object |
||
190 | */ |
||
191 | 25 | protected static function getAccessibleReflectionProperty($objectOrClass, $propertyName) |
|
215 | |||
216 | /** |
||
217 | * Get all property names on a class or object |
||
218 | * |
||
219 | * @param object|string $objectOrClass |
||
220 | * |
||
221 | * @return array of strings |
||
222 | * |
||
223 | * @throws \InvalidArgumentException |
||
224 | */ |
||
225 | 18 | public static function getProperties($objectOrClass) |
|
248 | } |
||
249 |
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.