Passed
Pull Request — master (#1332)
by Asmir
02:32
created

AttributeReader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 63
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyAnnotations() 0 9 2
A getClassAnnotations() 0 9 2
A getClassAnnotation() 0 8 3
A getMethodAnnotations() 0 9 2
A getPropertyAnnotation() 0 8 3
A getMethodAnnotation() 0 8 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
        $result = [];
17
        $attributes = $class->getAttributes();
18
        foreach ($attributes as $attribute) {
19
            $result[] = $attribute->newInstance();
20
        }
21
22
        return $result;
23
    }
24
25
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
26
    {
27
        $attributes = $class->getAttributes($annotationName);
28
        if (0 === count($attributes) || null === $attributes[0]) {
29
            return null;
30
        }
31
32
        return $attributes[0]->newInstance();
33
    }
34
35
    public function getMethodAnnotations(ReflectionMethod $method)
36
    {
37
        $result = [];
38
        $attributes = $method->getAttributes();
39
        foreach ($attributes as $attribute) {
40
            $result[] = $attribute->newInstance();
41
        }
42
43
        return $result;
44
    }
45
46
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
47
    {
48
        $attributes = $method->getAttributes($annotationName);
49
        if (0 === count($attributes) || null === $attributes[0]) {
50
            return null;
51
        }
52
53
        return $attributes[0]->newInstance();
54
    }
55
56
    public function getPropertyAnnotations(ReflectionProperty $property)
57
    {
58
        $result = [];
59
        $attributes = $property->getAttributes();
60
        foreach ($attributes as $attribute) {
61
            $result[] = $attribute->newInstance();
62
        }
63
64
        return $result;
65
    }
66
67
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
68
    {
69
        $attributes = $property->getAttributes($annotationName);
70
        if (0 === count($attributes) || null === $attributes[0]) {
71
            return null;
72
        }
73
74
        return $attributes[0]->newInstance();
75
    }
76
}
77