Completed
Push — standalone ( be1b50...88d8ec )
by Philip
04:15 queued 01:02
created

YamlDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
6
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
7
use Metadata\Driver\AbstractFileDriver;
8
use Metadata\Driver\DriverInterface;
9
use Metadata\Driver\FileLocatorInterface;
10
use Symfony\Component\Yaml\Yaml;
11
12
class YamlDriver extends AbstractFileDriver
13
{
14
    /**
15
     * @var DriverInterface
16
     */
17
    private $doctrineDriver;
18
19 39
    public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver)
20
    {
21 39
        parent::__construct($locator);
22 39
        $this->doctrineDriver = $doctrineDriver;
23 39
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 8
    protected function loadMetadataFromFile(\ReflectionClass $class, $file)
29
    {
30
        /** @var ClassMetadata $ddrRestClassMetadata */
31 8
        $classMetadata = $this->doctrineDriver->loadMetadataForClass($class);
32 8
        if (null === $classMetadata) {
33
            $classMetadata = new ClassMetadata($class->getName());
34
        }
35
36 8
        $config = Yaml::parse(file_get_contents($file));
37 8
        $className = key($config);
38
39 8
        if ($className !== $class->name) {
40
            throw new \RuntimeException(
41
                sprintf('Class definition mismatch for "%s" in "%s": %s', $class->getName(), $file, key($config))
42
            );
43
        }
44
45 8
        $config = $config[$className];
46 8
        if (!is_array($config)) {
47
            $config = [];
48
        }
49
50 8
        if (array_key_exists('rootResource', $config) && true === $config['rootResource']) {
51 8
            $classMetadata->setRestResource(true);
52
        }
53
54 8
        if (array_key_exists('service', $config)) {
55 2
            $classMetadata->setService($config['service']);
56
        }
57
58 8
        $fieldConfigs = [];
59 8
        if (array_key_exists('fields', $config)) {
60 8
            $fieldConfigs = $config['fields'];
61
        }
62
63 8
        foreach ($class->getProperties() as $reflectionProperty) {
64
65 8
            $propertyName = $reflectionProperty->getName();
66 8
            $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $propertyName);
0 ignored issues
show
Compatibility introduced by
$classMetadata of type object<Metadata\ClassMetadata> is not a sub-type of object<Dontdrinkandroot\...Metadata\ClassMetadata>. It seems like you assume a child class of the class Metadata\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67
68 8
            if (array_key_exists($propertyName, $fieldConfigs)) {
69 8
                $fieldConfig = $fieldConfigs[$propertyName];
70 8
                $this->parseFieldConfig($propertyName, $fieldConfig, $propertyMetadata);
71 8
                unset($fieldConfigs[$propertyName]);
72
            }
73
74 8
            $classMetadata->addPropertyMetadata($propertyMetadata);
75
        }
76
77
        /* Parse unbacked field definitions */
78 8
        foreach ($fieldConfigs as $name => $fieldConfig) {
79 2
            $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $name);
0 ignored issues
show
Compatibility introduced by
$classMetadata of type object<Metadata\ClassMetadata> is not a sub-type of object<Dontdrinkandroot\...Metadata\ClassMetadata>. It seems like you assume a child class of the class Metadata\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
80 2
            $this->parseFieldConfig($propertyName, $fieldConfig, $propertyMetadata);
0 ignored issues
show
Bug introduced by
The variable $propertyName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81 2
            $classMetadata->addPropertyMetadata($propertyMetadata);
82
        }
83
84 8
        return $classMetadata;
85
    }
86
87 8
    protected function parseFieldConfig(string $name, array $fieldConfig, PropertyMetadata $propertyMetadata): void
88
    {
89 8
        if (array_key_exists('puttable', $fieldConfig) && true === $fieldConfig['puttable']) {
90
            $propertyMetadata->setPuttable(true);
91
        }
92
93 8
        if (array_key_exists('excluded', $fieldConfig) && true === $fieldConfig['excluded']) {
94 2
            $propertyMetadata->setExcluded(true);
95
        }
96
97 8
        if (array_key_exists('postable', $fieldConfig) && true === $fieldConfig['postable']) {
98
            $propertyMetadata->setPostable(true);
99
        }
100
101 8
        if (array_key_exists('includable', $fieldConfig)) {
102 7
            $value = $fieldConfig['includable'];
103 7
            if (is_array($value)) {
104 7
                $propertyMetadata->setIncludable(true);
105 7
                $propertyMetadata->setIncludablePaths($value);
106
            } elseif (true === $value) {
107
                $propertyMetadata->setIncludable(true);
108
                $propertyMetadata->setIncludablePaths([$name]);
0 ignored issues
show
Documentation introduced by
array($name) is of type array<integer,string,{"0":"string"}>, but the function expects a null|array<integer,object<string>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
            }
110
        }
111 8
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 19
    protected function getExtension()
117
    {
118 19
        return 'rest.yml';
119
    }
120
121 8
    protected function getOrCreatePropertymetadata(ClassMetadata $classMetadata, $propertyName): PropertyMetadata
122
    {
123 8
        $propertyMetadata = $classMetadata->getPropertyMetadata($propertyName);
124 8
        if (null === $propertyMetadata) {
125 2
            $propertyMetadata = new PropertyMetadata($classMetadata->name, $propertyName);
126
127 2
            return $propertyMetadata;
128
        }
129
130 7
        return $propertyMetadata;
131
    }
132
}
133