Completed
Push — master ( bd9389...da8524 )
by Herberto
02:43
created

InstanceHelper::getProtectedProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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