SaxulumControllerProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 35.8 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 15
lcom 0
cbo 4
dl 29
loc 81
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
B boot() 0 22 6
A constructController() 16 16 4
A methodInjections() 13 13 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Saxulum\SaxulumControllerProvider\Provider;
4
5
use Saxulum\SaxulumControllerProvider\Map\Controller;
6
use Saxulum\SaxulumControllerProvider\Map\Map;
7
use Silex\Application;
8
use Silex\ServiceProviderInterface;
9
10
class SaxulumControllerProvider implements ServiceProviderInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function register(Application $app)
16
    {
17
        $app['controller.map'] = $app->share(function () {
18
            return new Map();
19
        });
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function boot(Application $app)
26
    {
27
        foreach ($app['controller.map']->getControllers() as $controller) {
28
            /** @var Controller $controller */
29
            $controllerNamespace = $controller->getNamespace();
30
            $controllerReflection = new \ReflectionClass($controllerNamespace);
31
            if($controllerReflection->implementsInterface('Saxulum\SaxulumControllerProvider\Controller\ControllerRouteInterface') &&
32
               !$controllerReflection->isAbstract() &&
33
               !$controllerReflection->isInterface()) {
34
                $app[$controller->getServiceId()] = $app->share(function () use ($app, $controller, $controllerNamespace, $controllerReflection) {
35
                    if ($controller->isInjectContainer()) {
36
                        return new $controllerNamespace($app);
37
                    }
38
                    $controllerInstance = SaxulumControllerProvider::constructController($app, $controller, $controllerReflection);
39
                    SaxulumControllerProvider::methodInjections($app, $controller, $controllerReflection, $controllerInstance);
40
41
                    return $controllerInstance;
42
                });
43
                $controllerNamespace::addRoutes($app, $controller->getServiceId());
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param  Application      $app
50
     * @param  Controller       $controller
51
     * @param  \ReflectionClass $controllerReflection
52
     * @return object
53
     */
54 View Code Duplication
    public static function constructController(Application $app, Controller $controller, \ReflectionClass $controllerReflection)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if ($controller->hasInjectionKeys()) {
57
            $args = array();
58
            foreach ($controller->getInjectionKeys() as $injectionKey) {
59
                if (isset($app[$injectionKey])) {
60
                    $args[] = $app[$injectionKey];
61
                }
62
            }
63
            $controllerInstance = $controllerReflection->newInstanceArgs($args);
64
        } else {
65
            $controllerInstance = $controllerReflection->newInstance();
66
        }
67
68
        return $controllerInstance;
69
    }
70
71
    /**
72
     * @param Application      $app
73
     * @param Controller       $controller
74
     * @param \ReflectionClass $controllerReflection
75
     * @param $controllerInstance
76
     */
77 View Code Duplication
    public static function methodInjections(Application $app, Controller $controller, \ReflectionClass $controllerReflection, $controllerInstance)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        foreach ($controller->getMethods() as $method) {
80
            $methodReflection = $controllerReflection->getMethod($method->getName());
81
            $args = array();
82
            foreach ($method->getInjectionKeys() as $injectionKey) {
83
                if (isset($app[$injectionKey])) {
84
                    $args[] = $app[$injectionKey];
85
                }
86
            }
87
            $methodReflection->invokeArgs($controllerInstance, $args);
88
        }
89
    }
90
}
91