Test Failed
Push — master ( c747db...f5bd3a )
by Dominik
05:26
created

src/Accessor/PropertyAccessor.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Accessor;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
8
use Doctrine\Common\Persistence\Proxy;
9
10
final class PropertyAccessor implements AccessorInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $property;
16
17
    /**
18
     * @param string $property
19
     */
20 6
    public function __construct(string $property)
21
    {
22 6
        $this->property = $property;
23 6
    }
24
25
    /**
26
     * @param object $object
27
     * @param mixed  $value
28
     */
29 3 View Code Duplication
    public function setValue($object, $value)
0 ignored issues
show
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...
30
    {
31 3
        $class = $this->getClass($object);
32
33 3
        if (!property_exists($class, $this->property)) {
34 1
            throw DeserializerLogicException::createMissingProperty($class, $this->property);
35
        }
36
37 2
        $setter = \Closure::bind(
38 2
            function ($property, $value) {
39 2
                $this->{$property} = $value;
40 2
            },
41 2
            $object,
42 2
            $class
43
        );
44
45 2
        $setter($this->property, $value);
46 2
    }
47
48
    /**
49
     * @param object $object
50
     *
51
     * @return mixed
52
     */
53 3 View Code Duplication
    public function getValue($object)
0 ignored issues
show
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...
54
    {
55 3
        $class = $this->getClass($object);
56
57 3
        if (!property_exists($class, $this->property)) {
58 1
            throw DeserializerLogicException::createMissingProperty($class, $this->property);
59
        }
60
61 2
        $getter = \Closure::bind(
62 2
            function ($property) {
63 2
                return $this->{$property};
64 2
            },
65 2
            $object,
66 2
            $class
67
        );
68
69 2
        return $getter($this->property);
70
    }
71
72
    /**
73
     * @param object $object
74
     *
75
     * @return string
76
     */
77 6
    private function getClass($object): string
78
    {
79 6
        if (interface_exists('Doctrine\Common\Persistence\Proxy') && $object instanceof Proxy) {
80 2
            if (!$object->__isInitialized()) {
81 2
                $object->__load();
82
            }
83
84 2
            $reflectionParentClass = (new \ReflectionObject($object))->getParentClass();
85 2
            if ($reflectionParentClass instanceof \ReflectionClass) {
86 2
                return $reflectionParentClass->getName();
0 ignored issues
show
Consider using $reflectionParentClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
87
            }
88
        }
89
90 4
        return get_class($object);
91
    }
92
}
93