Passed
Pull Request — master (#1463)
by Marcin
11:42
created

EnumPropertiesDriver::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 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Metadata\Driver;
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 Metadata\ClassMetadata;
13
use Metadata\Driver\DriverInterface;
14
use ReflectionClass;
15
use ReflectionException;
16
17
class EnumPropertiesDriver implements DriverInterface
18
{
19
    /**
20
     * @var DriverInterface
21
     */
22
    protected $delegate;
23
24
    public function __construct(DriverInterface $delegate)
25
    {
26
        $this->delegate = $delegate;
27
    }
28
29
    public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
30
    {
31
        $classMetadata = $this->delegate->loadMetadataForClass($class);
32
        \assert($classMetadata instanceof SerializerClassMetadata);
33
34
        // We base our scan on the internal driver's property list so that we
35
        // respect any internal allow/blocklist like in the AnnotationDriver
36
        foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
37
            // If the inner driver provides a type, don't guess anymore.
38
            if ($this->isVirtualProperty($propertyMetadata)) {
39
                continue;
40
            }
41
42
            if (!isset($propertyMetadata->type['name']) || !enum_exists($propertyMetadata->type['name'])) {
43
                continue;
44
            }
45
46
            try {
47
                $enumReflection = new \ReflectionEnum($propertyMetadata->type['name']);
48
                $serializerType = ['name' => 'enum', 'params' => [$enumReflection->getName(), $enumReflection->isBacked() ? 'value' : 'name']];
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionEnum. ( Ignorable by Annotation )

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

48
                $serializerType = ['name' => 'enum', 'params' => [$enumReflection->/** @scrutinizer ignore-call */ getName(), $enumReflection->isBacked() ? 'value' : 'name']];

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...
Bug introduced by
The method isBacked() does not exist on ReflectionEnum. ( Ignorable by Annotation )

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

48
                $serializerType = ['name' => 'enum', 'params' => [$enumReflection->getName(), $enumReflection->/** @scrutinizer ignore-call */ isBacked() ? 'value' : 'name']];

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...
49
                $propertyMetadata->setType($serializerType);
0 ignored issues
show
introduced by
The method setType() does not exist on Metadata\PropertyMetadata. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

49
                $propertyMetadata->/** @scrutinizer ignore-call */ 
50
                                   setType($serializerType);
Loading history...
50
            } catch (ReflectionException $e) {
51
                continue;
52
            }
53
        }
54
55
        return $classMetadata;
56
    }
57
58
    private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool
59
    {
60
        return $propertyMetadata instanceof VirtualPropertyMetadata
61
            || $propertyMetadata instanceof StaticPropertyMetadata
62
            || $propertyMetadata instanceof ExpressionPropertyMetadata;
63
    }
64
}
65