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

PropertyAccessor::getClass()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 1
crap 4
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