Passed
Pull Request — master (#1192)
by Asmir
15:09
created

TypedPropertiesDriver::getReflection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer;
6
7
use JMS\Serializer\Metadata\ClassMetadata as SerializerClassMetadata;
8
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
9
use JMS\Serializer\Metadata\PropertyMetadata;
10
use JMS\Serializer\Metadata\StaticPropertyMetadata;
11
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
12
use JMS\Serializer\Type\Parser;
13
use JMS\Serializer\Type\ParserInterface;
14
use Metadata\ClassMetadata;
15
use Metadata\Driver\DriverInterface;
16
use ReflectionClass;
17
use ReflectionException;
18
use ReflectionProperty;
19
20
class TypedPropertiesDriver implements DriverInterface
21
{
22
    /**
23
     * @var DriverInterface
24
     */
25
    protected $delegate;
26
27
    /**
28
     * @var ParserInterface
29
     */
30
    protected $typeParser;
31
32
    public function __construct(DriverInterface $delegate, ?ParserInterface $typeParser = null)
33
    {
34
        $this->delegate = $delegate;
35
        $this->typeParser = $typeParser ?: new Parser();
36
    }
37
38
    public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
39
    {
40
        /** @var SerializerClassMetadata $classMetadata */
41
        $classMetadata = $this->delegate->loadMetadataForClass($class);
42
43
        if (null === $classMetadata) {
44
            return null;
45
        }
46
        // We base our scan on the internal driver's property list so that we
47
        // respect any internal white/blacklisting like in the AnnotationDriver
48
        foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
49
            /** @var $propertyMetadata PropertyMetadata */
50
51
            // If the inner driver provides a type, don't guess anymore.
52
            if ($propertyMetadata->type || $this->isVirtualProperty($propertyMetadata)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyMetadata->type of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
                continue;
54
            }
55
56
            try {
57
                $propertyReflection = $this->getReflection($propertyMetadata);
58
                if (null !== $propertyReflection->getType()) {
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
                if (null !== $propertyReflection->/** @scrutinizer ignore-call */ getType()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
                    $propertyMetadata->setType($this->typeParser->parse((string) $propertyReflection->getType()));
60
                }
61
            } catch (ReflectionException $e) {
62
                continue;
63
            }
64
        }
65
66
        return $classMetadata;
67
    }
68
69
    private function getReflection(PropertyMetadata $propertyMetadata): ReflectionProperty
70
    {
71
        return new ReflectionProperty($propertyMetadata->class, $propertyMetadata->name);
72
    }
73
74
    private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool
75
    {
76
        return $propertyMetadata instanceof VirtualPropertyMetadata
77
            || $propertyMetadata instanceof StaticPropertyMetadata
78
            || $propertyMetadata instanceof ExpressionPropertyMetadata;
79
    }
80
}
81