InstanceHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 34.34 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 34
loc 99
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getReflectionProperties() 0 4 1
A createInstance() 0 6 1
A createInstanceWithoutConstructor() 0 6 1
A setProtectedProperty() 8 8 1
A getProtectedProperty() 9 9 1
A getParameters() 17 17 4
B getCallableReflectionMethod() 0 17 5

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 Closure;
6
use Hgraca\Helper\Concept\ReflectionHelperAbstract;
7
use InvalidArgumentException;
8
use ReflectionException;
9
use ReflectionFunction;
10
use ReflectionMethod;
11
use ReflectionProperty;
12
13
final class InstanceHelper extends ReflectionHelperAbstract
14
{
15
    /**
16
     * @param mixed $object
17
     *
18
     * @return ReflectionProperty[]
19
     */
20 8
    public static function getReflectionProperties($object): array
21
    {
22 8
        return ClassHelper::getReflectionProperties(get_class($object));
23
    }
24
25
    /**
26
     * @return mixed
27
     */
28 1
    public static function createInstance(string $fqcn, array $constructorArguments = [])
29
    {
30 1
        $reflectionClass = self::getReflectionClass($fqcn);
31
32 1
        return $reflectionClass->newInstanceArgs($constructorArguments);
33
    }
34
35
    /**
36
     * @return mixed
37
     */
38 3
    public static function createInstanceWithoutConstructor(string $fqcn)
39
    {
40 3
        $reflectionClass = self::getReflectionClass($fqcn);
41
42 3
        return $reflectionClass->newInstanceWithoutConstructor();
43
    }
44
45 3 View Code Duplication
    public static function setProtectedProperty($object, string $propertyName, $value)
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...
46
    {
47 3
        $class = self::getReflectionClass(get_class($object));
48
49 3
        $property = ClassHelper::getReflectionProperty($class, $propertyName);
50 2
        $property->setAccessible(true);
51 2
        $property->setValue($object, $value);
52 2
    }
53
54 3 View Code Duplication
    public static function getProtectedProperty($object, string $propertyName)
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...
55
    {
56 3
        $class = self::getReflectionClass(get_class($object));
57
58 3
        $property = ClassHelper::getReflectionProperty($class, $propertyName);
59 2
        $property->setAccessible(true);
60
61 2
        return $property->getValue($object);
62
    }
63
64
    /**
65
     * @param callable $input Can be a callable array (method defaults to '__construct'), callable object or Closure
66
     *
67
     * @throws InvalidArgumentException
68
     *
69
     * @return array [index => [name, class]]
70
     */
71 4 View Code Duplication
    public static function getParameters($input): 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...
72
    {
73
        try {
74 4
            $reflectionMethod = self::getCallableReflectionMethod($input);
75 1
        } catch (ReflectionException $e) {
76
            return [];
77
        }
78
79 3
        foreach ($reflectionMethod->getParameters() as $index => $param) {
80 3
            $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...
81 3
            if (null !== $param->getClass()) {
82 3
                $reflectionParameters[$index]['class'] = $param->getClass()->name;
83
            }
84
        }
85
86 3
        return $reflectionParameters ?? [];
87
    }
88
89
    /**
90
     * @throws InvalidArgumentException
91
     *
92
     * @return ReflectionFunction|ReflectionMethod
93
     */
94 4
    private static function getCallableReflectionMethod($input)
95
    {
96 4
        if (is_array($input)) {
97 1
            $dependentClass  = is_string($input[0]) ? $input[0] : get_class($input[0]);
98 1
            $dependentMethod = $input[1] ?? '__construct';
99
100 1
            return new ReflectionMethod($dependentClass, $dependentMethod);
101
        } elseif ($input instanceof Closure) {
102
103 1
            return new ReflectionFunction($input);
104 2
        } elseif (is_callable($input)) {
105
106 1
            return new ReflectionMethod(get_class($input), '__invoke');
107
        } else {
108 1
            throw new InvalidArgumentException('$input needs to be a callable');
109
        }
110
    }
111
}
112