PropertyAccessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 26.39 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 19
loc 72
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 9 9 1
A get() 10 10 1
A validateObject() 0 9 2
A validateProperty() 0 10 2

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 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