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

getObjectToReflect()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 6
nc 3
nop 1
crap 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