Passed
Pull Request — master (#11)
by Dominik
04:20 queued 01:29
created

PropertyAccessor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 9.64 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 8
loc 83
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setValue() 0 6 1
A getValue() 0 7 1
B getClass() 0 24 4
A getReflectionProperty() 8 8 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
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Accessor;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
8
use Chubbyphp\Deserialization\Doctrine\Accessor\PropertyAccessor as DoctrinePropertyAccessor;
9
use Doctrine\Common\Persistence\Proxy;
10
11
final class PropertyAccessor implements AccessorInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $property;
17
18
    /**
19
     * @param string $property
20
     */
21 6
    public function __construct(string $property)
22
    {
23 6
        $this->property = $property;
24 6
    }
25
26
    /**
27
     * @param object $object
28
     * @param mixed  $value
29
     */
30 3
    public function setValue($object, $value)
31
    {
32 3
        $reflectionProperty = $this->getReflectionProperty($this->getClass($object));
33 2
        $reflectionProperty->setAccessible(true);
34 2
        $reflectionProperty->setValue($object, $value);
35 2
    }
36
37
    /**
38
     * @param object $object
39
     *
40
     * @return mixed
41
     */
42 4
    public function getValue($object)
43
    {
44 4
        $reflectionProperty = $this->getReflectionProperty($this->getClass($object));
45 3
        $reflectionProperty->setAccessible(true);
46
47 3
        return $reflectionProperty->getValue($object);
48
    }
49
50
    /**
51
     * @param object $object
52
     *
53
     * @return string
54
     */
55 6
    private function getClass($object): string
56
    {
57 6
        if (interface_exists('Doctrine\Common\Persistence\Proxy') && $object instanceof Proxy) {
58 2
            $parentClass = (new \ReflectionClass($object))->getParentClass()->name;
59 2
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60 2
                sprintf(
61 2
                    'Use "%s" instead of "%s" for "%s:%s"',
62 2
                    DoctrinePropertyAccessor::class,
63 2
                    self::class,
64 2
                    $parentClass,
65 2
                    $this->property
66
                ),
67 2
                E_USER_DEPRECATED
68
            );
69
70 2
            if (!$object->__isInitialized()) {
71 2
                $object->__load();
72
            }
73
74 2
            return $parentClass;
75
        }
76
77 4
        return get_class($object);
78
    }
79
80
    /**
81
     * @param string $class
82
     *
83
     * @return \ReflectionProperty
84
     */
85 6 View Code Duplication
    private function getReflectionProperty(string $class): \ReflectionProperty
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...
86
    {
87
        try {
88 6
            return new \ReflectionProperty($class, $this->property);
89 2
        } catch (\ReflectionException $e) {
90 2
            throw DeserializerLogicException::createMissingProperty($class, $this->property);
91
        }
92
    }
93
}
94