PropertyAccessor::validateProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Isolate\LazyObjects\Object;
4
5
use Isolate\LazyObjects\Exception\InvalidArgumentException;
6
use Isolate\LazyObjects\Exception\NotExistingPropertyException;
7
use Isolate\LazyObjects\Object\Value\Assembler;
8
use Isolate\LazyObjects\Object\Value\AssemblerFactory;
9
10
/**
11
 * @api
12
 */
13
class PropertyAccessor
14
{
15
    /**
16
     * @param $object
17
     * @param $propertyName
18
     * @param $value
19
     * @throws InvalidArgumentException
20
     * @throws NotExistingPropertyException
21
     * 
22
     * @api
23
     */
24 View Code Duplication
    public function set($object, $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...
25
    {
26
        $this->validateObject($object);
27
        $reflection = new \ReflectionClass($object);
28
        $this->validateProperty($reflection, $object, $propertyName);
29
        $property = $reflection->getProperty($propertyName);
30
        $property->setAccessible(true);
31
        $property->setValue($object, $value);
32
    }
33
34
    /**
35
     * @param $object
36
     * @param $propertyName
37
     * @return mixed
38
     * @throws InvalidArgumentException
39
     * @throws NotExistingPropertyException
40
     * 
41
     * @api
42
     */
43 View Code Duplication
    public function get($object, $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...
44
    {
45
        $this->validateObject($object);
46
        $reflection = new \ReflectionClass($object);
47
        $this->validateProperty($reflection, $object, $propertyName);
48
        $property = $reflection->getProperty($propertyName);
49
        $property->setAccessible(true);
50
51
        return $property->getValue($object);
52
    }
53
54
    /**
55
     * @param $value
56
     * @throws InvalidArgumentException
57
     */
58
    private function validateObject($value)
59
    {
60
        if (!is_object($value)) {
61
            throw new InvalidArgumentException(sprintf(
62
                "PropertyAccessor require object to access property, \"%s\" passed.",
63
                gettype($value)
64
            ));
65
        }
66
    }
67
68
    /**
69
     * @param \ReflectionClass $reflection
70
     * @param $object
71
     * @param $propertyName
72
     * @throws NotExistingPropertyException
73
     */
74
    private function validateProperty(\ReflectionClass $reflection, $object, $propertyName)
75
    {
76
        if (!$reflection->hasProperty($propertyName)) {
77
            throw new NotExistingPropertyException(sprintf(
78
                "Property \"%s\" does not exists in \"%s\" class.",
79
                $propertyName,
80
                get_class($object)
81
            ));
82
        }
83
    }
84
}
85