Completed
Push — master ( 1f2b71...3f1d1d )
by Matze
07:04
created

Controller   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 110
ccs 29
cts 33
cp 0.8788
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 34 3
A setDefaults() 0 23 3
B handleRouteAnnotation() 0 26 2
1
<?php
2
3
namespace BrainExe\Core\Annotations\Builder;
4
5
use BrainExe\Annotations\Annotations\Service;
6
use BrainExe\Annotations\Builder\ServiceDefinition;
7
use BrainExe\Core\Annotations\Guest;
8
use BrainExe\Core\Annotations\Role;
9
use BrainExe\Core\Annotations\Route;
10
use BrainExe\Core\DependencyInjection\CompilerPass\ControllerCompilerPass;
11
use BrainExe\Core\Annotations\Controller as ControllerAnnotation;
12
use ReflectionClass;
13
use ReflectionMethod;
14
use Symfony\Component\DependencyInjection\Definition;
15
16
class Controller extends ServiceDefinition
17
{
18
19
    /**
20
     * @param ReflectionClass $reflectionClass
21
     * @param ControllerAnnotation|Service $annotation
22
     * @param Definition $definition
23
     * @return array
24
     */
25 1
    public function build(ReflectionClass $reflectionClass, Service $annotation, Definition $definition)
26
    {
27
        /** @var Definition $definition */
28 1
        list ($serviceId, $definition) = parent::build(
0 ignored issues
show
Unused Code introduced by
The assignment to $serviceId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
29
            $reflectionClass,
30
            $annotation,
31
            $definition
32
        );
33 1
        $serviceId = $reflectionClass->getName();
34
35 1
        $definition->addTag(ControllerCompilerPass::CONTROLLER_TAG);
36 1
        $definition->setPublic(true);
37 1
        $definition->setShared(false);
38
39 1
        foreach ($reflectionClass->getMethods() as $method) {
40
            /** @var Route $routeAnnotation */
41 1
            $routeAnnotation = $this->reader->getMethodAnnotation(
42
                $method,
43 1
                Route::class
44
            );
45
46 1
            if ($routeAnnotation) {
47 1
                $this->handleRouteAnnotation(
48
                    $annotation,
0 ignored issues
show
Compatibility introduced by
$annotation of type object<BrainExe\Annotations\Annotations\Service> is not a sub-type of object<BrainExe\Core\Annotations\Controller>. It seems like you assume a child class of the class BrainExe\Annotations\Annotations\Service to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
                    $method,
50
                    $routeAnnotation,
51
                    $serviceId,
52
                    $definition
53
                );
54
            }
55
        }
56
57 1
        return [$serviceId, $definition];
58
    }
59
60
61
    /**
62
     * @param Route $routeAnnotation
63
     * @param ReflectionMethod $method
64
     * @param string $serviceId
65
     * @param Guest $guestAnnotation
66
     * @param Role $roleAnnotation
67
     */
68 1
    protected function setDefaults(
69
        Route $routeAnnotation,
70
        ReflectionMethod $method,
71
        string $serviceId,
72
        Guest $guestAnnotation = null,
73
        Role $roleAnnotation = null
74
    ) {
75 1
        $defaults = $routeAnnotation->getDefaults();
76 1
        $defaults['_controller'] = [
77 1
            $serviceId,
78 1
            $method->getName()
79
        ];
80
81 1
        if ($guestAnnotation) {
82 1
            $defaults['_guest'] = true;
83
        }
84
85 1
        if ($roleAnnotation) {
86
            $defaults['_role'] = $roleAnnotation->role;
87
        }
88
89 1
        $routeAnnotation->setDefaults($defaults);
90 1
    }
91
92
    /**
93
     * @param ControllerAnnotation $annotation
94
     * @param ReflectionMethod $method
95
     * @param Route $routeAnnotation
96
     * @param string $serviceId
97
     * @param Definition $definition
98
     */
99 1
    private function handleRouteAnnotation(
100
        ControllerAnnotation $annotation,
101
        ReflectionMethod$method,
102
        Route $routeAnnotation,
103
        string $serviceId,
104
        Definition $definition
105
    ) {
106
        /** @var Guest $guestAnnotation */
107 1
        $guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class);
108
109
        /** @var Role $roleAnnotation */
110 1
        $roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class);
111
112 1
        $this->setDefaults($routeAnnotation, $method, $serviceId, $guestAnnotation, $roleAnnotation);
113
114 1
        if ($annotation->requirements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->requirements of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            $routeAnnotation->setRequirements(
116
                array_merge(
117
                    $routeAnnotation->getRequirements(),
118
                    $annotation->requirements
119
                )
120
            );
121
        }
122
123 1
        $definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]);
124 1
    }
125
}
126