Passed
Push — master ( 80d03b...782137 )
by Herberto
01:55
created

InstanceHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 28.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 23
loc 82
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

6 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
C getParameters() 6 23 7

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