Completed
Push — master ( d8a83b...76c960 )
by Eric
02:56
created

ClassMetadataFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 10 4
B getClassMetadata() 0 18 5
A getParentClassMetadata() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Factory;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Ivory\Serializer\Mapping\ClassMetadata;
16
use Ivory\Serializer\Mapping\ClassMetadataInterface;
17
use Ivory\Serializer\Mapping\Loader\AnnotationClassMetadataLoader;
18
use Ivory\Serializer\Mapping\Loader\ChainClassMetadataLoader;
19
use Ivory\Serializer\Mapping\Loader\ClassMetadataLoaderInterface;
20
use Ivory\Serializer\Mapping\Loader\ReflectionClassMetadataLoader;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class ClassMetadataFactory implements ClassMetadataFactoryInterface
26
{
27
    /**
28
     * @var ClassMetadataLoaderInterface
29
     */
30
    private $loader;
31
32
    /**
33
     * @var ClassMetadataInterface[]
34
     */
35
    private $classMetadatas = [];
36
37
    /**
38
     * @param ClassMetadataLoaderInterface $loader
39
     */
40
    public function __construct(ClassMetadataLoaderInterface $loader)
41
    {
42
        $this->loader = $loader;
43
    }
44
45
    /**
46
     * @param ClassMetadataLoaderInterface[] $loaders
47
     *
48
     * @return ClassMetadataFactoryInterface
49
     */
50
    public static function create(array $loaders = [])
51
    {
52
        if (empty($loaders)) {
53
            $loaders[] = class_exists(AnnotationReader::class)
54
                ? new AnnotationClassMetadataLoader(new AnnotationReader())
55
                : new ReflectionClassMetadataLoader();
56
        }
57
58
        return new static(count($loaders) > 1 ? new ChainClassMetadataLoader($loaders) : array_shift($loaders));
0 ignored issues
show
Bug introduced by
It seems like count($loaders) > 1 ? ne...: array_shift($loaders) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getClassMetadata($class)
65
    {
66
        if (array_key_exists($class, $this->classMetadatas)) {
67
            return $this->classMetadatas[$class];
68
        }
69
70
        $classMetadata = new ClassMetadata($class);
71
        $found = false;
72
73
        if (($parentMetadata = $this->getParentClassMetadata($class)) !== null) {
74
            $classMetadata->merge($parentMetadata);
75
            $found = true;
76
        }
77
78
        $found = $this->loader->loadClassMetadata($classMetadata) || $found;
79
80
        return $this->classMetadatas[$class] = $found ? $classMetadata : null;
81
    }
82
83
    /**
84
     * @param string $class
85
     *
86
     * @return ClassMetadataInterface|null
87
     */
88
    private function getParentClassMetadata($class)
89
    {
90
        if (($parent = get_parent_class($class)) !== false) {
91
            return $this->getClassMetadata($parent);
92
        }
93
    }
94
}
95