Completed
Pull Request — master (#394)
by Elan
01:16
created

RouteProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 207
Duplicated Lines 22.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 8
dl 47
loc 207
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B registerRoutes() 47 199 1

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 XHGui\ServiceProvider;
4
5
use Exception;
6
use Pimple\Container;
7
use Pimple\ServiceProviderInterface;
8
use Slim\Slim as App;
9
use Slim\Views\Twig;
10
use XHGui\Controller;
11
use XHGui\Twig\TwigExtension;
12
13
class RouteProvider implements ServiceProviderInterface
14
{
15
    public function register(Container $di)
16
    {
17
        $this->registerRoutes($di, $di['app']);
18
    }
19
20
    private function registerRoutes(Container $di, App $app)
21
    {
22
        $app->error(static function (Exception $e) use ($di, $app) {
23
            /** @var Twig $view */
24
            $view = $di['view'];
25
            $view->parserOptions['cache'] = false;
26
            $view->parserExtensions = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array(new \XHGui\Twig\TwigExtension($app)) of type array<integer,object<XHG...Twig\\TwigExtension>"}> is incompatible with the declared type object<Slim\Views\TwigExtension> of property $parserExtensions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
                new TwigExtension($app),
28
            ];
29
30
            // Remove the controller so we don't render it.
31
            unset($app->controller);
32
33
            $app->view($view);
34
            $app->render('error/view.twig', [
35
                'message' => $e->getMessage(),
36
                'stack_trace' => $e->getTraceAsString(),
37
            ]);
38
        });
39
40
        // Profile Runs routes
41
        $app->get('/', static function () use ($di, $app) {
42
            /** @var Controller\RunController $controller */
43
            $controller = $app->controller = $di['runController'];
44
            $request = $app->request();
45
            $response = $app->response();
46
47
            $controller->index($request, $response);
48
        })->setName('home');
49
50 View Code Duplication
        $app->get('/run/view', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            /** @var Controller\RunController $controller */
52
            $controller = $app->controller = $di['runController'];
53
            $request = $app->request();
54
            $response = $app->response();
55
56
            $controller->view($request, $response);
57
        })->setName('run.view');
58
59
        $app->get('/run/delete', static function () use ($di, $app) {
60
            /** @var Controller\RunController $controller */
61
            $controller = $app->controller = $di['runController'];
62
            $request = $app->request();
63
64
            $controller->deleteForm($request);
65
        })->setName('run.delete.form');
66
67
        $app->post('/run/delete', static function () use ($di, $app) {
68
            /** @var Controller\RunController $controller */
69
            $controller = $di['runController'];
70
            $request = $app->request();
71
72
            $controller->deleteSubmit($request);
73
        })->setName('run.delete.submit');
74
75
        $app->get('/run/delete_all', static function () use ($di, $app) {
76
            /** @var Controller\RunController $controller */
77
            $controller = $app->controller = $di['runController'];
78
            $controller->deleteAllForm();
79
        })->setName('run.deleteAll.form');
80
81
        $app->post('/run/delete_all', static function () use ($di) {
82
            /** @var Controller\RunController $controller */
83
            $controller = $di['runController'];
84
            $controller->deleteAllSubmit();
85
        })->setName('run.deleteAll.submit');
86
87 View Code Duplication
        $app->get('/url/view', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
            /** @var Controller\RunController $controller */
89
            $controller = $app->controller = $di['runController'];
90
            $request = $app->request();
91
92
            $controller->url($request);
93
        })->setName('url.view');
94
95
        $app->get('/run/compare', static function () use ($di, $app) {
96
            /** @var Controller\RunController $controller */
97
            $controller = $app->controller = $di['runController'];
98
            $request = $app->request();
99
100
            $controller->compare($request);
101
        })->setName('run.compare');
102
103
        $app->get('/run/symbol', static function () use ($di, $app) {
104
            /** @var Controller\RunController $controller */
105
            $controller = $app->controller = $di['runController'];
106
            $request = $app->request();
107
108
            $controller->symbol($request);
109
        })->setName('run.symbol');
110
111
        $app->get('/run/symbol/short', static function () use ($di, $app) {
112
            /** @var Controller\RunController $controller */
113
            $controller = $app->controller = $di['runController'];
114
            $request = $app->request();
115
116
            $controller->symbolShort($request);
117
        })->setName('run.symbol-short');
118
119
        $app->get('/run/callgraph', static function () use ($di, $app) {
120
            /** @var Controller\RunController $controller */
121
            $controller = $app->controller = $di['runController'];
122
            $request = $app->request();
123
124
            $controller->callgraph($request);
125
        })->setName('run.callgraph');
126
127 View Code Duplication
        $app->get('/run/callgraph/data', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
            /** @var Controller\RunController $controller */
129
            $controller = $di['runController'];
130
            $request = $app->request();
131
            $response = $app->response();
132
133
            $controller->callgraphData($request, $response);
134
        })->setName('run.callgraph.data');
135
136 View Code Duplication
        $app->get('/run/callgraph/dot', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
137
            /** @var Controller\RunController $controller */
138
            $controller = $di['runController'];
139
            $request = $app->request();
140
            $response = $app->response();
141
142
            $controller->callgraphDataDot($request, $response);
143
        })->setName('run.callgraph.dot');
144
145
        // Import route
146
        $app->post('/run/import', static function () use ($di, $app) {
147
            /** @var Controller\ImportController $controller */
148
            $controller = $di['importController'];
149
            $request = $app->request();
150
            $response = $app->response();
151
152
            $controller->import($request, $response);
153
        })->setName('run.import');
154
155
        // Watch function routes.
156
        $app->get('/watch', static function () use ($di, $app) {
157
            /** @var Controller\WatchController $controller */
158
            $controller = $app->controller = $di['watchController'];
159
            $controller->get();
160
        })->setName('watch.list');
161
162
        $app->post('/watch', static function () use ($di, $app) {
163
            /** @var Controller\WatchController $controller */
164
            $controller = $di['watchController'];
165
            $request = $app->request();
166
167
            $controller->post($request);
168
        })->setName('watch.save');
169
170
        // Custom report routes.
171
        $app->get('/custom', static function () use ($di, $app) {
172
            /** @var Controller\CustomController $controller */
173
            $controller = $app->controller = $di['customController'];
174
            $controller->get();
175
        })->setName('custom.view');
176
177
        $app->get('/custom/help', static function () use ($di, $app) {
178
            /** @var Controller\CustomController $controller */
179
            $controller = $app->controller = $di['customController'];
180
            $request = $app->request();
181
182
            $controller->help($request);
183
        })->setName('custom.help');
184
185 View Code Duplication
        $app->post('/custom/query', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
186
            /** @var Controller\CustomController $controller */
187
            $controller = $di['customController'];
188
            $request = $app->request();
189
            $response = $app->response();
190
191
            $controller->query($request, $response);
192
        })->setName('custom.query');
193
194
        // Waterfall routes
195
        $app->get('/waterfall', static function () use ($di, $app) {
196
            /** @var Controller\WaterfallController $controller */
197
            $controller = $app->controller = $di['waterfallController'];
198
            $controller->index();
199
        })->setName('waterfall.list');
200
201 View Code Duplication
        $app->get('/waterfall/data', static function () use ($di, $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
202
            /** @var Controller\WaterfallController $controller */
203
            $controller = $di['waterfallController'];
204
            $request = $app->request();
205
            $response = $app->response();
206
207
            $controller->query($request, $response);
208
        })->setName('waterfall.data');
209
210
        // Metrics
211
        $app->get('/metrics', static function () use ($di, $app) {
212
            /** @var Controller\MetricsController $controller */
213
            $controller = $di['metricsController'];
214
            $response = $app->response();
215
216
            $controller->metrics($response);
217
        })->setName('metrics');
218
    }
219
}
220