Completed
Push — master ( 1856ca...43d45a )
by Eric
03:58
created

ClassMetadataFactory::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.2
cc 4
eloc 6
nc 3
nop 1
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 extends AbstractClassMetadataFactory
26
{
27
    /**
28
     * @var ClassMetadataLoaderInterface
29
     */
30
    private $loader;
31
32
    /**
33
     * @param ClassMetadataLoaderInterface $loader
34
     */
35
    public function __construct(ClassMetadataLoaderInterface $loader)
36
    {
37
        $this->loader = $loader;
38
    }
39
40
    /**
41
     * @param ClassMetadataLoaderInterface[] $loaders
42
     *
43
     * @return ClassMetadataFactoryInterface
44
     */
45
    public static function create(array $loaders = [])
46
    {
47
        if (empty($loaders)) {
48
            $loaders[] = class_exists(AnnotationReader::class)
49
                ? new AnnotationClassMetadataLoader(new AnnotationReader())
50
                : new ReflectionClassMetadataLoader();
51
        }
52
53
        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...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function fetchClassMetadata($class)
60
    {
61
        $classMetadata = new ClassMetadata($class);
62
        $found = false;
63
64
        if (($parentMetadata = $this->getParentClassMetadata($class)) !== null) {
65
            $classMetadata->merge($parentMetadata);
66
            $found = true;
67
        }
68
69
        $found = $this->loader->loadClassMetadata($classMetadata) || $found;
70
71
        return $found ? $classMetadata : null;
72
    }
73
74
    /**
75
     * @param string $class
76
     *
77
     * @return ClassMetadataInterface|null
78
     */
79
    private function getParentClassMetadata($class)
80
    {
81
        if (($parent = get_parent_class($class)) !== false) {
82
            return $this->getClassMetadata($parent);
83
        }
84
    }
85
}
86