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 |
||
11 | final class ClassHelper extends ReflectionHelperAbstract |
||
12 | { |
||
13 | 1 | public static function extractCanonicalClassName(string $classFqcn): string |
|
14 | { |
||
15 | 1 | return substr($classFqcn, strrpos($classFqcn, '\\') + 1); |
|
16 | } |
||
17 | |||
18 | /** |
||
19 | * Returns a key-value array with all constants in the class |
||
20 | * It uses Reflection to find them |
||
21 | * |
||
22 | * @param string $classFqcn |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | 1 | public static function findConstants(string $classFqcn): array |
|
27 | { |
||
28 | 1 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
29 | |||
30 | 1 | return $reflectionClass->getConstants(); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Returns an array with all properties names of the mixed |
||
35 | * |
||
36 | * @param string $classFqcn |
||
37 | * @param string[] $excludedNames |
||
38 | * @param string[] $excludedVisibility = ['public', 'protected', 'private', 'static'] |
||
39 | * |
||
40 | * @return string[] |
||
41 | */ |
||
42 | 4 | public static function findPropertiesNames( |
|
43 | string $classFqcn, |
||
44 | array $excludedNames = [], |
||
45 | array $excludedVisibility = [] |
||
46 | ) { |
||
47 | 4 | $defaultVisibility = ['public' => true, 'protected' => true, 'private' => true, 'static' => true]; |
|
48 | 4 | $excludedVisibility = array_merge( |
|
49 | $defaultVisibility, |
||
50 | 4 | self::setGivenExcludedVisivilitiesToKeysWithValueFalse($excludedVisibility) |
|
51 | ); |
||
52 | |||
53 | 4 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
54 | 4 | $reflectionPropertiesArray = $reflectionClass->getProperties(); |
|
55 | |||
56 | 4 | return self::filterProperties($excludedNames, $excludedVisibility, $reflectionPropertiesArray); |
|
57 | } |
||
58 | |||
59 | 5 | public static function hasMethod(string $classFqcn, string $method): bool |
|
60 | { |
||
61 | 5 | $reflectionClass = self::getReflectionClass($classFqcn); |
|
62 | |||
63 | 5 | return $reflectionClass->hasMethod($method); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $classFqcn |
||
68 | * @param string $method |
||
69 | * |
||
70 | * @return array [index => [name, class]] |
||
71 | */ |
||
72 | 2 | View Code Duplication | public static function getParameters(string $classFqcn, string $method): array |
|
|||
73 | { |
||
74 | try { |
||
75 | 2 | $reflectionMethod = new ReflectionMethod($classFqcn, $method); |
|
76 | } catch (ReflectionException $e) { |
||
77 | return []; |
||
78 | } |
||
79 | |||
80 | 2 | foreach ($reflectionMethod->getParameters() as $index => $param) { |
|
81 | 1 | $reflectionParameters[$index]['name'] = $param->getName(); |
|
82 | 1 | if (null !== $param->getClass()) { |
|
83 | 1 | $reflectionParameters[$index]['class'] = $param->getClass()->name; |
|
84 | } |
||
85 | } |
||
86 | |||
87 | 2 | return $reflectionParameters ?? []; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return ReflectionProperty[] |
||
92 | */ |
||
93 | 10 | public static function getReflectionProperties(string $classFqcn): array |
|
99 | |||
100 | /** |
||
101 | * @param ReflectionProperty[] $propertyList |
||
102 | */ |
||
103 | 4 | public static function setReflectionPropertiesAccessible(array &$propertyList) |
|
109 | |||
110 | /** |
||
111 | * Gets the property from the current class, when not available will look on parent classes before failing |
||
112 | * |
||
113 | * @throws ReflectionException |
||
114 | * |
||
115 | * @return ReflectionProperty |
||
116 | */ |
||
117 | 6 | public static function getReflectionProperty(ReflectionClass $class, string $propertyName): ReflectionProperty |
|
130 | |||
131 | 4 | private static function excludeProperty( |
|
142 | |||
143 | /** |
||
144 | * @param string[] $excludedNames |
||
145 | * @param bool[] $excludedVisibility |
||
146 | * @param ReflectionProperty[] $reflectionPropertiesArray |
||
147 | * |
||
148 | * @return string[] |
||
149 | */ |
||
150 | 4 | private static function filterProperties( |
|
169 | |||
170 | /** |
||
171 | * @param array $excludedVisibility |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | 4 | private static function setGivenExcludedVisivilitiesToKeysWithValueFalse(array $excludedVisibility) |
|
179 | } |
||
180 |
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.