PropertyInfoPropertyMetadataFactory::create()   F
last analyzed

Complexity

Conditions 19
Paths 288

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 48
rs 2.5833
c 0
b 0
f 0
cc 19
nc 288
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property;
15
16
use ApiPlatform\Core\Exception\PropertyNotFoundException;
17
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
19
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
20
21
/**
22
 * PropertyInfo metadata loader decorator.
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class PropertyInfoPropertyMetadataFactory implements PropertyMetadataFactoryInterface
27
{
28
    private $propertyInfo;
29
    private $decorated;
30
31
    public function __construct(PropertyInfoExtractorInterface $propertyInfo, PropertyMetadataFactoryInterface $decorated = null)
32
    {
33
        $this->propertyInfo = $propertyInfo;
34
        $this->decorated = $decorated;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function create(string $resourceClass, string $name, array $options = []): PropertyMetadata
41
    {
42
        if (null === $this->decorated) {
43
            $propertyMetadata = new PropertyMetadata();
44
        } else {
45
            try {
46
                $propertyMetadata = $this->decorated->create($resourceClass, $name, $options);
47
            } catch (PropertyNotFoundException $propertyNotFoundException) {
48
                $propertyMetadata = new PropertyMetadata();
49
            }
50
        }
51
52
        if (null === $propertyMetadata->getType()) {
53
            $types = $this->propertyInfo->getTypes($resourceClass, $name, $options);
54
            if (isset($types[0])) {
55
                $propertyMetadata = $propertyMetadata->withType($types[0]);
56
            }
57
        }
58
59
        if (null === $propertyMetadata->getDescription() && null !== $description = $this->propertyInfo->getShortDescription($resourceClass, $name, $options)) {
60
            $propertyMetadata = $propertyMetadata->withDescription($description);
61
        }
62
63
        if (null === $propertyMetadata->isReadable() && null !== $readable = $this->propertyInfo->isReadable($resourceClass, $name, $options)) {
64
            $propertyMetadata = $propertyMetadata->withReadable($readable);
65
        }
66
67
        if (null === $propertyMetadata->isWritable() && null !== $writable = $this->propertyInfo->isWritable($resourceClass, $name, $options)) {
68
            $propertyMetadata = $propertyMetadata->withWritable($writable);
69
        }
70
71
        if (method_exists($this->propertyInfo, 'isInitializable')) {
72
            if (null === $propertyMetadata->isInitializable() && null !== $initializable = $this->propertyInfo->isInitializable($resourceClass, $name, $options)) {
73
                $propertyMetadata = $propertyMetadata->withInitializable($initializable);
74
            }
75
        } else {
76
            // BC layer for Symfony < 4.2
77
            $ref = new \ReflectionClass($resourceClass);
78
            if ($ref->isInstantiable() && $constructor = $ref->getConstructor()) {
79
                foreach ($constructor->getParameters() as $constructorParameter) {
80
                    if ($constructorParameter->name === $name && null === $propertyMetadata->isInitializable()) {
81
                        $propertyMetadata = $propertyMetadata->withInitializable(true);
82
                    }
83
                }
84
            }
85
        }
86
87
        return $propertyMetadata;
88
    }
89
}
90