Passed
Pull Request — main (#24)
by Mojtaba
12:28
created

AttributeReader::getClassAnnotations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bugloos\QueryFilterBundle\Service;
6
7
use Doctrine\Common\Annotations\Reader;
8
9
final class AttributeReader implements Reader
10
{
11
    public function getMethodAnnotations(\ReflectionMethod $method): array
12
    {
13
        $attributesRefs = $method->getAttributes();
14
        $attributes = [];
15
        foreach ($attributesRefs as $ref) {
16
            $attributes[] = $ref->newInstance();
17
        }
18
19
        return $attributes;
20
    }
21
22
    public function getClassAnnotations(\ReflectionClass $class): array
23
    {
24
        $attributesRefs = $class->getAttributes();
25
        $attributes = [];
26
        foreach ($attributesRefs as $ref) {
27
            $attribute = $ref->newInstance();
28
            $attributes[] = $attribute;
29
        }
30
31
        return $attributes;
32
    }
33
34
    public function getClassAnnotation(\ReflectionClass $class, $annotationName): ?object
35
    {
36
        $attributes = $class->getAttributes($annotationName, \ReflectionAttribute::IS_INSTANCEOF);
37
        if (isset($attributes[0])) {
38
            return $attributes[0]->newInstance();
39
        }
40
41
        return null;
42
    }
43
44
    public function getMethodAnnotation(\ReflectionMethod $method, $annotationName): ?object
45
    {
46
        $attributes = $method->getAttributes($annotationName, \ReflectionAttribute::IS_INSTANCEOF);
47
        if (isset($attributes[0])) {
48
            return $attributes[0]->newInstance();
49
        }
50
51
        return null;
52
    }
53
54
    public function getPropertyAnnotations(\ReflectionProperty $property): array
55
    {
56
        $attributesRefs = $property->getAttributes();
57
        $attributes = [];
58
        foreach ($attributesRefs as $ref) {
59
            $attributes[] = $ref->newInstance();
60
        }
61
62
        return $attributes;
63
    }
64
65
    public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName): ?object
66
    {
67
        $attributes = $property->getAttributes($annotationName, \ReflectionAttribute::IS_INSTANCEOF);
68
        if (isset($attributes[0])) {
69
            return $attributes[0]->newInstance();
70
        }
71
72
        return null;
73
    }
74
}
75