Passed
Push — master ( 0c284f...0146fd )
by Dawid
03:05
created

ControllerAnnotationExtractorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 66
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getAnnotationReader() 0 1 ?
A getClassAnnotationFromController() 0 11 2
A getMethodAnnotationFromController() 0 14 4
B getObjectToReflect() 0 12 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Annotation\Controller;
6
7
use Doctrine\Common\Annotations\Reader;
8
9
trait ControllerAnnotationExtractorTrait
10
{
11
    /**
12
     * @return Reader
13
     */
14
    abstract protected function getAnnotationReader(): Reader;
15
16
    /**
17
     * @param callable $controller
18
     * @param string   $annotationClass
19
     *
20
     * @return null|object
21
     */
22 13
    protected function getClassAnnotationFromController(callable $controller, string $annotationClass)
23
    {
24 13
        if (\is_object($objectToReflect = $this->getObjectToReflect($controller))) {
25 13
            return $this->getAnnotationReader()->getClassAnnotation(
26 13
                new \ReflectionObject($objectToReflect),
27 13
                $annotationClass
28
            );
29
        }
30
31
        return null;
32
    }
33
34
    /**
35
     * @param callable $controller
36
     * @param string   $annotationClass
37
     *
38
     * @throws \ReflectionException When $controller method does not exist
39
     *
40
     * @return null|object
41
     */
42 15
    protected function getMethodAnnotationFromController(callable $controller, string $annotationClass)
43
    {
44 15
        if (\is_array($controller)
45 15
            && isset($controller[1])
46 15
            && \is_object($objectToReflect = $this->getObjectToReflect($controller))
47
        ) {
48 15
            return $this->getAnnotationReader()->getMethodAnnotation(
49 15
                new \ReflectionMethod($objectToReflect, $controller[1]),
50 15
                $annotationClass
51
            );
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param callable $controller
59
     *
60
     * @return null|object
61
     */
62 19
    protected function getObjectToReflect(callable $controller)
63
    {
64 19
        if (\is_array($controller) && isset($controller[0]) && \is_object($controller[0])) {
65 16
            return $controller[0];
66
        }
67
68 3
        if (\is_object($controller) && !$controller instanceof \Closure) {
69 1
            return $controller;
70
        }
71
72 2
        return null;
73
    }
74
}
75