Passed
Pull Request — master (#1340)
by Asmir
04:27 queued 01:57
created

AttributeReader::getMethodAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
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
    /**
15
     * @var Reader
16
     */
17
    private $reader;
18
19
    public function __construct(Reader $reader)
20
    {
21
        $this->reader = $reader;
22
    }
23
24
    public function getClassAnnotations(ReflectionClass $class)
25
    {
26
        $attributes = $class->getAttributes();
27
28
        return array_merge($this->reader->getClassAnnotations($class), $this->buildAnnotations($attributes));
29
    }
30
31
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
32
    {
33
        $attributes = $class->getAttributes($annotationName);
34
35
        return $this->reader->getClassAnnotation($class, $annotationName) ?? $this->buildAnnotation($attributes);
36
    }
37
38
    public function getMethodAnnotations(ReflectionMethod $method)
39
    {
40
        $attributes = $method->getAttributes();
41
42
        return array_merge($this->reader->getMethodAnnotations($method), $this->buildAnnotations($attributes));
43
    }
44
45
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
46
    {
47
        $attributes = $method->getAttributes($annotationName);
48
49
        return $this->reader->getClassAnnotation($method, $annotationName) ?? $this->buildAnnotation($attributes);
0 ignored issues
show
Bug introduced by
$method of type ReflectionMethod is incompatible with the type ReflectionClass expected by parameter $class of Doctrine\Common\Annotati...r::getClassAnnotation(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        return $this->reader->getClassAnnotation(/** @scrutinizer ignore-type */ $method, $annotationName) ?? $this->buildAnnotation($attributes);
Loading history...
50
    }
51
52
    public function getPropertyAnnotations(ReflectionProperty $property)
53
    {
54
        $attributes = $property->getAttributes();
55
56
        return array_merge($this->reader->getPropertyAnnotations($property), $this->buildAnnotations($attributes));
57
    }
58
59
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
60
    {
61
        $attributes = $property->getAttributes($annotationName);
62
63
        return $this->reader->getClassAnnotation($property, $annotationName) ?? $this->buildAnnotation($attributes);
0 ignored issues
show
Bug introduced by
$property of type ReflectionProperty is incompatible with the type ReflectionClass expected by parameter $class of Doctrine\Common\Annotati...r::getClassAnnotation(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        return $this->reader->getClassAnnotation(/** @scrutinizer ignore-type */ $property, $annotationName) ?? $this->buildAnnotation($attributes);
Loading history...
64
    }
65
66
    private function buildAnnotation(array $attributes): ?object
67
    {
68
        if (!isset($attributes[0])) {
69
            return null;
70
        }
71
72
        return $attributes[0]->newInstance();
73
    }
74
75
    private function buildAnnotations(array $attributes): array
76
    {
77
        $result = [];
78
        foreach ($attributes as $attribute) {
79
            if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) {
80
                $result[] = $attribute->newInstance();
81
            }
82
        }
83
84
        return $result;
85
    }
86
}
87