Passed
Push — master ( 4e4741...4fd99d )
by Asmir
03:38 queued 01:04
created

AttributeReader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyAnnotations() 0 5 1
A getClassAnnotations() 0 5 1
A buildAnnotation() 0 7 2
A getClassAnnotation() 0 5 1
A getMethodAnnotations() 0 5 1
A getPropertyAnnotation() 0 5 1
A getMethodAnnotation() 0 5 1
A buildAnnotations() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Metadata\Driver\AttributeDriver;
6
7
use Doctrine\Common\Annotations\Reader;
8
use ReflectionClass;
9
use ReflectionMethod;
10
use ReflectionProperty;
11
12
class AttributeReader implements Reader
13
{
14
    public function getClassAnnotations(ReflectionClass $class)
15
    {
16
        $attributes = $class->getAttributes();
17
18
        return $this->buildAnnotations($attributes);
19
    }
20
21
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
22
    {
23
        $attributes = $class->getAttributes($annotationName);
24
25
        return $this->buildAnnotation($attributes);
26
    }
27
28
    public function getMethodAnnotations(ReflectionMethod $method)
29
    {
30
        $attributes = $method->getAttributes();
31
32
        return $this->buildAnnotations($attributes);
33
    }
34
35
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
36
    {
37
        $attributes = $method->getAttributes($annotationName);
38
39
        return $this->buildAnnotation($attributes);
40
    }
41
42
    public function getPropertyAnnotations(ReflectionProperty $property)
43
    {
44
        $attributes = $property->getAttributes();
45
46
        return $this->buildAnnotations($attributes);
47
    }
48
49
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
50
    {
51
        $attributes = $property->getAttributes($annotationName);
52
53
        return $this->buildAnnotation($attributes);
54
    }
55
56
    private function buildAnnotation(array $attributes): ?object
57
    {
58
        if (!isset($attributes[0])) {
59
            return null;
60
        }
61
62
        return $attributes[0]->newInstance();
63
    }
64
65
    private function buildAnnotations(array $attributes): array
66
    {
67
        $result = [];
68
        foreach ($attributes as $attribute) {
69
            if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) {
70
                $result[] = $attribute->newInstance();
71
            }
72
        }
73
74
        return $result;
75
    }
76
}
77