Completed
Push — master ( 6751ec...bd2805 )
by JHONATAN
03:37
created

YmlDriver::loadMetadataForClass()   F

Complexity

Conditions 12
Paths 552

Size

Total Lines 77
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 12.0014

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 45
cts 46
cp 0.9783
rs 2.9089
c 0
b 0
f 0
cc 12
eloc 45
nc 552
nop 1
crap 12.0014

How to fix   Long Method    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
namespace Vox\Metadata\Driver;
4
5
use Metadata\Driver\DriverInterface;
6
use Metadata\MethodMetadata;
7
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface;
8
use ReflectionClass;
9
use ReflectionProperty;
10
use RuntimeException;
11
use Symfony\Component\Yaml\Parser;
12
use Vox\Data\Mapping\Bindings;
13
use Vox\Data\Mapping\Exclude;
14
use Vox\Metadata\ClassMetadata;
15
use Vox\Metadata\PropertyMetadata;
16
use Vox\Webservice\Mapping\BelongsTo;
17
use Vox\Webservice\Mapping\HasMany;
18
use Vox\Webservice\Mapping\HasOne;
19
use Vox\Webservice\Mapping\Id;
20
use Vox\Webservice\Mapping\Resource;
21
22
/**
23
 * Yml driver to create a class metadata information
24
 * 
25
 * @author Jhonatan Teixeira <[email protected]>
26
 */
27
class YmlDriver implements DriverInterface
28
{
29
    private $ymlParser;
30
    
31
    private $path;
32
    
33
    private $classMetadataClassName;
34
    
35
    private $propertyMetadataClassName;
36
    
37 6
    public function __construct(
38
        string $path,
39
        string $classMetadataClassName = ClassMetadata::class,
40
        string $propertyMetadataClassName = PropertyMetadata::class
41
    ) {
42 6
        $this->ymlParser                 = new Parser();
43 6
        $this->path                      = realpath($path);
44 6
        $this->classMetadataClassName    = $classMetadataClassName;
45 6
        $this->propertyMetadataClassName = $propertyMetadataClassName;
46 6
    }
47
    
48 6
    public function loadMetadataForClass(ReflectionClass $class): ClassMetadata
49
    {
50 6
        if ($class->implementsInterface(AccessInterceptorValueHolderInterface::class)) {
51
            $class = $class->getParentClass();
52
        }
53
        
54 6
        $yml = $this->loadYml($class);
55
        
56
        /* @var $classMetadata ClassMetadata */
57 6
        $classMetadata = (new ReflectionClass($this->classMetadataClassName))->newInstance($class->name);
58
        
59 6
        if (isset($yml['resource'])) {
60 4
            $resource         = new Resource();
61 4
            $resource->client = $yml['resource']['client'] ?? null;
62 4
            $resource->route  = $yml['resource']['route'] ?? null;
63 4
            $classMetadata->setAnnotations([Resource::class => $resource]);
64
        }
65
        
66 6
        foreach ($class->getMethods() as $method) {
67 4
            $classMetadata->addMethodMetadata(new MethodMetadata($class->name, $method->name));
68
        }
69
        
70
        /* @var $reflectionProperty ReflectionProperty */
71 6
        foreach ($class->getProperties() as $reflectionProperty) {
72 6
            $annotations = [];
73 6
            $annotations[Bindings::class] = $bindings = new Bindings();
74 6
            $name                         = $reflectionProperty->name;
75
76 6
            $idMapping = $yml['id'] ?? [];
77
78 6
            $ids = is_scalar($idMapping) ? [$idMapping] : $idMapping;
79
80 6
            if (in_array($name, $ids)) {
81 5
                $annotations[Id::class] = new Id();
82
            }
83
            
84 6
            if (isset($yml['parameters'][$reflectionProperty->name])) {
85 4
                $config           = $yml['parameters'][$name];
86 4
                $bindings->source = $config['bindings']['source'] ?? null;
87 4
                $bindings->target = $config['bindings']['target'] ?? null;
88
89 4
                if (isset($config['belongsTo'])) {
90 2
                    $belongsTo = new BelongsTo();
91 2
                    $belongsTo->foreignField = $config['belongsTo']['foreignField'];
92 2
                    $annotations[BelongsTo::class] = $belongsTo;
93
                }
94
                
95 4
                if (isset($config['hasOne'])) {
96 1
                    $hasOne = new HasOne();
97 1
                    $hasOne->foreignField = $config['hasOne']['foreignField'];
98 1
                    $annotations[HasOne::class] = $hasOne;
99
                }
100
                
101 4
                if (isset($config['hasMany'])) {
102 1
                    $hasMany = new HasMany();
103 1
                    $hasMany->foreignField = $config['hasMany']['foreignField'];
104 1
                    $annotations[HasMany::class] = $hasMany;
105
                }
106
                
107 4
                if (isset($config['exclude'])) {
108 1
                    $exclude = new Exclude();
109 1
                    $exclude->input  = $config['exlude']['input'] ?? true;
110 1
                    $exclude->output = $config['exlude']['output'] ?? true;
111 1
                    $annotations[Exclude::class] = $exclude;
112
                }
113
            }
114
            
115
            /* @var $propertyMetadata PropertyMetadata */
116 6
            $propertyMetadata = (new ReflectionClass($this->propertyMetadataClassName))
117 6
                ->newInstance($class->name, $reflectionProperty->name);
118
            
119 6
            $propertyMetadata->setAnnotations($annotations);
120
            
121 6
            $classMetadata->addPropertyMetadata($propertyMetadata);
122
        }
123
        
124 6
        return $classMetadata;
125
    }
126
    
127 6
    private function loadYml(ReflectionClass $class)
128
    {
129 6
        $className = $class->getName();
130
        
131 6
        $path = sprintf(
132 6
            '%s/%s.yml', 
133 6
            preg_replace('/\/$/', '', $this->path), 
134 6
            str_replace('\\', DIRECTORY_SEPARATOR, $className)
135
        );
136
        
137 6
        if (is_file($path)) {
138
            return $this->ymlParser->parse(file_get_contents($path));
139
        }
140
        
141 6
        $path = sprintf(
142 6
            '%s/%s.yml', 
143 6
            preg_replace('/\/$/', '', $this->path), 
144 6
            str_replace('\\', '.', $className)
145
        );
146
        
147 6
        if (is_file($path)) {
148 5
            return $this->ymlParser->parse(file_get_contents($path));
149
        }
150
151 1
        return [];
152
        throw new RuntimeException("metadata file not found for class $className");
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
153
    }
154
}
155