Completed
Push — master ( 7e12ab...eee1f0 )
by Eric
03:22
created

AnnotationClassMetadataLoader::loadAnnotations()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 26
cts 26
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 24
nc 12
nop 1
crap 12

How to fix   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
/*
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\Loader;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\Annotations\Reader;
16
use Ivory\Serializer\Mapping\Annotation\Accessor;
17
use Ivory\Serializer\Mapping\Annotation\Alias;
18
use Ivory\Serializer\Mapping\Annotation\Exclude;
19
use Ivory\Serializer\Mapping\Annotation\ExclusionPolicy;
20
use Ivory\Serializer\Mapping\Annotation\Expose;
21
use Ivory\Serializer\Mapping\Annotation\Groups;
22
use Ivory\Serializer\Mapping\Annotation\MaxDepth;
23
use Ivory\Serializer\Mapping\Annotation\Mutator;
24
use Ivory\Serializer\Mapping\Annotation\Order;
25
use Ivory\Serializer\Mapping\Annotation\Since;
26
use Ivory\Serializer\Mapping\Annotation\Type;
27
use Ivory\Serializer\Mapping\Annotation\Until;
28
use Ivory\Serializer\Type\Parser\TypeParserInterface;
29
30
/**
31
 * @author GeLo <[email protected]>
32
 */
33
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader
34
{
35
    /**
36
     * @var Reader
37
     */
38
    private $reader;
39
40
    /**
41
     * @param Reader|null              $reader
42
     * @param TypeParserInterface|null $typeParser
43
     */
44 630
    public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null)
45
    {
46 630
        parent::__construct($typeParser);
47
48 630
        $this->reader = $reader ?: new AnnotationReader();
49 630
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 465
    protected function loadClass(\ReflectionClass $class)
55
    {
56 465
        $definition = parent::loadClass($class);
57
58 465
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
59 105
            if ($annotation instanceof ExclusionPolicy) {
60 42
                $definition['exclusion_policy'] = $annotation->getPolicy();
61 70
            } elseif ($annotation instanceof Order) {
62 77
                $definition['order'] = $annotation->getOrder();
63 42
            }
64 310
        }
65
66 465
        return $definition;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 462
    protected function loadProperty(\ReflectionProperty $property)
73
    {
74 462
        return $this->loadAnnotations($this->reader->getPropertyAnnotations($property));
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 261
    protected function loadMethod(\ReflectionMethod $method)
81
    {
82 261
        $result = $this->loadAnnotations($this->reader->getMethodAnnotations($method));
83
84 261
        if (!empty($result)) {
85 39
            return $result;
86
        }
87 261
    }
88
89
    /**
90
     * @param object[] $annotations
91
     *
92
     * @return mixed[]
93
     */
94 462
    private function loadAnnotations(array $annotations)
95
    {
96 462
        $definition = [];
97
98 462
        foreach ($annotations as $annotation) {
99 399
            if ($annotation instanceof Alias) {
100 75
                $definition['alias'] = $annotation->getAlias();
101 266
            } elseif ($annotation instanceof Type) {
102 192
                $definition['type'] = $annotation->getType();
103 267
            } elseif ($annotation instanceof Expose) {
104 21
                $definition['expose'] = true;
105 164
            } elseif ($annotation instanceof Exclude) {
106 21
                $definition['exclude'] = true;
107 150
            } elseif ($annotation instanceof Accessor) {
108 21
                $definition['accessor'] = $annotation->getAccessor();
109 136
            } elseif ($annotation instanceof Mutator) {
110 21
                $definition['mutator'] = $annotation->getMutator();
111 124
            } elseif ($annotation instanceof Since) {
112 57
                $definition['since'] = $annotation->getVersion();
113 110
            } elseif ($annotation instanceof Until) {
114 57
                $definition['until'] = $annotation->getVersion();
115 108
            } elseif ($annotation instanceof MaxDepth) {
116 39
                $definition['max_depth'] = $annotation->getMaxDepth();
117 70
            } elseif ($annotation instanceof Groups) {
118 177
                $definition['groups'] = $annotation->getGroups();
119 44
            }
120 308
        }
121
122 462
        return $definition;
123
    }
124
}
125