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

src/Accessor/PropertyAccessor.php (1 issue)

Labels
Severity

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)
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)
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) {
0 ignored issues
show
The class Doctrine\Common\Persistence\Proxy does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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();
87
            }
88
        }
89
90 4
        return get_class($object);
91
    }
92
}
93