ClassHelper   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 169
Duplicated Lines 10.06 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 1
dl 17
loc 169
ccs 52
cts 54
cp 0.963
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getReflectionProperties() 0 6 1
A setReflectionPropertiesAccessible() 0 6 2
A extractCanonicalClassName() 0 4 1
A findConstants() 0 6 1
A findPropertiesNames() 0 16 1
A hasMethod() 0 6 1
A getReflectionProperty() 0 13 3
B excludeProperty() 0 11 9
A filterProperties() 0 19 3
A setGivenExcludedVisivilitiesToKeysWithValueFalse() 0 4 1
A getParameters() 17 17 4

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Hgraca\Helper;
4
5
use Hgraca\Helper\Concept\ReflectionHelperAbstract;
6
use ReflectionClass;
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionProperty;
10
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$reflectionParameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $reflectionParameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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
94
    {
95 10
        $reflectionClass = self::getReflectionClass($classFqcn);
96
97 10
        return $reflectionClass->getProperties();
98
    }
99
100
    /**
101
     * @param ReflectionProperty[] $propertyList
102
     */
103 4
    public static function setReflectionPropertiesAccessible(array &$propertyList)
104
    {
105 4
        foreach ($propertyList as $reflectionProperty) {
106 4
            $reflectionProperty->setAccessible(true);
107
        }
108 4
    }
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
118
    {
119
        try {
120 6
            return $class->getProperty($propertyName);
121 4
        } catch (ReflectionException $e) {
122 4
            $parentClass = $class->getParentClass();
123 4
            if ($parentClass === false) {
124 2
                throw $e;
125
            }
126
127 4
            return self::getReflectionProperty($parentClass, $propertyName);
128
        }
129
    }
130
131 4
    private static function excludeProperty(
132
        array $excludedNames,
133
        array $excludedVisibility,
134
        ReflectionProperty $reflectionProperty
135
    ): bool {
136 4
        return in_array($reflectionProperty->getName(), $excludedNames) ||
137 4
        (!$excludedVisibility['public'] && $reflectionProperty->isPublic()) ||
138 4
        (!$excludedVisibility['protected'] && $reflectionProperty->isProtected()) ||
139 4
        (!$excludedVisibility['private'] && $reflectionProperty->isPrivate()) ||
140 4
        (!$excludedVisibility['static'] && $reflectionProperty->isStatic());
141
    }
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(
151
        array $excludedNames,
152
        array $excludedVisibility,
153
        array $reflectionPropertiesArray
154
    ) {
155 4
        $propertyArray = [];
156
157
        /** @var ReflectionProperty $reflectionProperty */
158 4
        foreach ($reflectionPropertiesArray as $reflectionProperty) {
159 4
            if (self::excludeProperty($excludedNames, $excludedVisibility, $reflectionProperty)) {
160 3
                continue;
161
            }
162
163 4
            $reflectionProperty->setAccessible(true);
164 4
            $propertyArray[] = $reflectionProperty->getName();
165
        }
166
167 4
        return $propertyArray;
168
    }
169
170
    /**
171
     * @param array $excludedVisibility
172
     *
173
     * @return array
174
     */
175 4
    private static function setGivenExcludedVisivilitiesToKeysWithValueFalse(array $excludedVisibility)
176
    {
177 4
        return array_fill_keys(array_keys(array_flip($excludedVisibility)), false);
178
    }
179
}
180