Completed
Push — master ( c0d282...bc8922 )
by Emily
02:02
created

RawPropertyAccessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Spaark\CompositeUtils\Service;
4
5
use Spaark\CompositeUtils\Exception\CannotWritePropertyException;
6
use Spaark\CompositeUtils\Exception\CannotReadPropertyException;
7
use \ReflectionClass;
8
use \ReflectionProperty;
9
use \ReflectionException;
10
11
/**
12
 * This class is used to access the properties of a class
13
 */
14
class RawPropertyAccessor
15
{
16
    /**
17
     * The object being accessed
18
     *
19
     * @var object
20
     */
21
    protected $object;
22
23
    /**
24
     * A PHP Reflector for access
25
     *
26
     * @var ReflectionClass
27
     */
28
    protected $reflector;
29
30
    /**
31
     * @param object $object The object to access
32
     * @param ReflectionModel|null $reflect A reflection entity
0 ignored issues
show
Bug introduced by
There is no parameter named $reflect. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
     */
34 1
    public function __construct($object)
35
    {
36 1
        $this->object = $object;
37 1
        $this->reflector = new ReflectionClass($object);
38 1
    }
39
40
    /**
41
     * Access a property and sets it with the given value, irrespective
42
     * of access permissions
43
     *
44
     * @param string $key The property to write to
45
     * @param mixed $value The value to set
46
     * @throws CannotWritePropertyException In the event the property
47
     *     does not exist
48
     */
49 2
    public function setRawValue($key, $value)
50
    {
51 2
        $this->getPropertyOrFail
52
        (
53
            $key,
54 2
            CannotWritePropertyException::class
55
        )
56 2
        ->setValue($this->object, $value);
57 2
    }
58
59
    /**
60
     * Access a property and returns its value, irresepective of access
61
     * permissions
62
     *
63
     * @param string $key The property to read
64
     * @return mixed The property's value
65
     * @throws CannotReadPropertyException In the event the property
66
     *     does not exist
67
     */
68 1
    public function getRawValue($key)
69
    {
70
        return
71 1
            $this->getPropertyOrFail
72
            (
73
                $key,
74 1
                CannotReadPropertyException::class
75
            )
76 1
            ->getValue($this->object);
77
    }
78
79
    /**
80
     * Adds a value to a property, irrespective of access permissions
81
     *
82
     * @param string $key The property to write to
83
     * @param mixed $value The value to add
84
     * @throws CannotWritePropertyException In the event the property
85
     *    does not exist
86
     */ 
87 2
    public function rawAddToValue($key, $value)
88
    {
89 2
        $property = $this->getPropertyOrFail
90
        (
91
            $key,
92 2
            CannotWritePropertyException::class
93
        );
94 2
        $property->getValue($this->object)->add($value);
95 2
    }
96
97
    /**
98
     * Checks if a property exists and returns its ReflectionProperty
99
     *
100
     * @param string $key The property to access
101
     * @param string $class The name of the exception to throw on error
102
     * @throws $class If the property does not exist
103
     * @return ReflectionProperty
104
     */
105 4
    private function getPropertyOrFail($key, $class)
106
    {
107
        try
108
        {
109 4
            $prop = $this->reflector->getProperty($key);
110 2
            $prop->setAccessible(true);
111
112 2
            return $prop;
113
        }
114 2
        catch (ReflectionException $e)
115
        {
116 2
            throw new $class
117
            (
118 2
                get_class($this->object),
119
                $key,
120
                $e
121
            );
122
        }
123
    }
124
}
125