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

AttributeReader::getClassAnnotations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
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
            if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) {
20
                $result[] = $attribute->newInstance();
21
            }
22
        }
23
24
        return $result;
25
    }
26
27
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
28
    {
29
        $attributes = $class->getAttributes($annotationName);
30
        if (0 === count($attributes) || null === $attributes[0]) {
31
            return null;
32
        }
33
34
        return $attributes[0]->newInstance();
35
    }
36
37
    public function getMethodAnnotations(ReflectionMethod $method)
38
    {
39
        $result = [];
40
        $attributes = $method->getAttributes();
41
        foreach ($attributes as $attribute) {
42
            if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) {
43
                $result[] = $attribute->newInstance();
44
            }
45
        }
46
47
        return $result;
48
    }
49
50
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
51
    {
52
        $attributes = $method->getAttributes($annotationName);
53
        if ([] === $attributes || !isset($attributes[0])) {
54
            return null;
55
        }
56
57
        return $attributes[0]->newInstance();
58
    }
59
60
    public function getPropertyAnnotations(ReflectionProperty $property)
61
    {
62
        $result = [];
63
        $attributes = $property->getAttributes();
64
        foreach ($attributes as $attribute) {
65
            if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) {
66
                $result[] = $attribute->newInstance();
67
            }
68
        }
69
70
        return $result;
71
    }
72
73
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
74
    {
75
        $attributes = $property->getAttributes($annotationName);
76
        if ([] === $attributes || !isset($attributes[0])) {
77
            return null;
78
        }
79
80
        return $attributes[0]->newInstance();
81
    }
82
}
83