Passed
Branch develop (640e78)
by Herberto
03:54
created

InstanceHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 17
loc 51
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

5 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

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
namespace Hgraca\Helper;
3
4
use Hgraca\Helper\Concept\ReflectionHelperAbstract;
5
use ReflectionProperty;
6
7
final class InstanceHelper extends ReflectionHelperAbstract
8
{
9
    /**
10
     * @param mixed $object
11
     *
12
     * @return ReflectionProperty[]
13
     */
14 8
    public static function getReflectionProperties($object): array
15
    {
16 8
        return ClassHelper::getReflectionProperties(get_class($object));
17
    }
18
19
    /**
20
     * @return mixed
21
     */
22 1
    public static function createInstance(string $fqcn, array $constructorArguments = [])
23
    {
24 1
        $reflectionClass = self::getReflectionClass($fqcn);
25
26 1
        return $reflectionClass->newInstanceArgs($constructorArguments);
27
    }
28
29
    /**
30
     * @return mixed
31
     */
32 3
    public static function createInstanceWithoutConstructor(string $fqcn)
33
    {
34 3
        $reflectionClass = self::getReflectionClass($fqcn);
35
36 3
        return $reflectionClass->newInstanceWithoutConstructor();
37
    }
38
39 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...
40
    {
41 3
        $class = self::getReflectionClass(get_class($object));
42
43 3
        $property = ClassHelper::getReflectionProperty($class, $propertyName);
44 2
        $property->setAccessible(true);
45 2
        $property->setValue($object, $value);
46 2
    }
47
48 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...
49
    {
50 3
        $class = self::getReflectionClass(get_class($object));
51
52 3
        $property = ClassHelper::getReflectionProperty($class, $propertyName);
53 2
        $property->setAccessible(true);
54
55 2
        return $property->getValue($object);
56
    }
57
}
58