Completed
Push — master ( 808187...59f278 )
by Eric
04:10
created

ClassMetadataFactory::create()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 12
nc 7
nop 1
crap 6
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
use phpDocumentor\Reflection\ClassReflector;
22
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
23
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
24
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
class ClassMetadataFactory extends AbstractClassMetadataFactory
30
{
31
    /**
32
     * @var ClassMetadataLoaderInterface
33
     */
34
    private $loader;
35
36
    /**
37
     * @param ClassMetadataLoaderInterface $loader
38
     */
39 1332
    public function __construct(ClassMetadataLoaderInterface $loader)
40
    {
41 1332
        $this->loader = $loader;
42 1332
    }
43
44
    /**
45
     * @param ClassMetadataLoaderInterface[] $loaders
46
     *
47
     * @return ClassMetadataFactoryInterface
48
     */
49 1312
    public static function create(array $loaders = [])
50
    {
51 1312
        if (empty($loaders)) {
52 1312
            $extractor = null;
53
54 1312
            if (class_exists(PropertyInfoExtractor::class)) {
55 1312
                $extractors = $typeExtractors = [new ReflectionExtractor()];
56
57 1312
                if (class_exists(ClassReflector::class)) {
58 1312
                    array_unshift($typeExtractors, new PhpDocExtractor());
59 656
                }
60
61 1312
                $extractor = new PropertyInfoExtractor($extractors, $typeExtractors, [], $extractors);
62 656
            }
63
64 1312
            $loaders = [new ReflectionClassMetadataLoader($extractor)];
65
66 1312
            if (class_exists(AnnotationReader::class)) {
67 1312
                $loaders[] = new AnnotationClassMetadataLoader(new AnnotationReader());
68 656
            }
69 656
        }
70
71 1312
        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...
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1040
    protected function fetchClassMetadata($class)
78
    {
79 1040
        $classMetadata = new ClassMetadata($class);
80 1036
        $found = false;
81
82 1036
        if (($parentMetadata = $this->getParentClassMetadata($class)) !== null) {
83 4
            $classMetadata->merge($parentMetadata);
84 4
            $found = true;
85 2
        }
86
87 1036
        $found = $this->loader->loadClassMetadata($classMetadata) || $found;
88
89 1036
        return $found ? $classMetadata : null;
90
    }
91
92
    /**
93
     * @param string $class
94
     *
95
     * @return ClassMetadataInterface|null
96
     */
97 1036
    private function getParentClassMetadata($class)
98
    {
99 1036
        if (($parent = get_parent_class($class)) !== false) {
100 4
            return $this->getClassMetadata($parent);
101
        }
102 1036
    }
103
}
104