ParamReader::read()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Request;
13
14
use Doctrine\Common\Annotations\Reader;
15
use FOS\RestBundle\Controller\Annotations\ParamInterface;
16
17
/**
18
 * Class loading "@ParamInterface" annotations from methods.
19
 *
20
 * @author Alexander <[email protected]>
21
 * @author Lukas Kahwe Smith <[email protected]>
22
 * @author Boris Guéry  <[email protected]>
23
 */
24
final class ParamReader implements ParamReaderInterface
25
{
26
    private $annotationReader;
27
28 15
    public function __construct(Reader $annotationReader)
29
    {
30 15
        $this->annotationReader = $annotationReader;
31 15
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 14
    public function read(\ReflectionClass $reflection, string $method): array
37
    {
38 14
        if (!$reflection->hasMethod($method)) {
39 1
            throw new \InvalidArgumentException(sprintf('Class "%s" has no method "%s".', $reflection->getName(), $method));
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
40
        }
41
42 13
        $methodParams = $this->getParamsFromMethod($reflection->getMethod($method));
43 13
        $classParams = $this->getParamsFromClass($reflection);
44
45 13
        return array_merge($methodParams, $classParams);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 13
    public function getParamsFromMethod(\ReflectionMethod $method): array
52
    {
53 13
        $annotations = $this->annotationReader->getMethodAnnotations($method);
54
55 13
        return $this->getParamsFromAnnotationArray($annotations);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 13
    public function getParamsFromClass(\ReflectionClass $class): array
62
    {
63 13
        $annotations = $this->annotationReader->getClassAnnotations($class);
64
65 13
        return $this->getParamsFromAnnotationArray($annotations);
66
    }
67
68
    /**
69
     * @return ParamInterface[]
70
     */
71 13
    private function getParamsFromAnnotationArray(array $annotations): array
72
    {
73 13
        $params = array();
74 13
        foreach ($annotations as $annotation) {
75 13
            if ($annotation instanceof ParamInterface) {
76 13
                $params[$annotation->getName()] = $annotation;
77
            }
78
        }
79
80 13
        return $params;
81
    }
82
}
83