getObjectToReflect()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 6
rs 9.2222
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 20
    protected function getClassAnnotationFromController(callable $controller, string $annotationClass)
23
    {
24 20
        if (\is_object($objectToReflect = $this->getObjectToReflect($controller))) {
25 20
            return $this->getAnnotationReader()->getClassAnnotation(
26 20
                new \ReflectionObject($objectToReflect),
27 20
                $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 22
    protected function getMethodAnnotationFromController(callable $controller, string $annotationClass)
43
    {
44 22
        if (\is_array($controller)
45 22
            && isset($controller[1])
46 22
            && \is_object($objectToReflect = $this->getObjectToReflect($controller))
47
        ) {
48 22
            return $this->getAnnotationReader()->getMethodAnnotation(
49 22
                new \ReflectionMethod($objectToReflect, $controller[1]),
50 22
                $annotationClass
51
            );
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param callable $controller
59
     *
60
     * @return null|object
61
     */
62 26
    protected function getObjectToReflect(callable $controller)
63
    {
64 26
        if (\is_array($controller) && isset($controller[0]) && \is_object($controller[0])) {
65 23
            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