Completed
Pull Request — master (#1320)
by
unknown
03:30 queued 13s
created

AttributeReader::getPropertyAnnotations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
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