Completed
Push — master ( bfefcf...b9abf7 )
by Mark
49s queued 48s
created

Xhgui_Controller_Run   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 351
Duplicated Lines 16.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 4
dl 58
loc 351
rs 8.6
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B index() 0 50 4
B view() 0 35 3
A delete() 0 15 2
B url() 0 50 5
B compare() 0 55 7
A symbol() 0 20 1
A symbolShort() 0 22 1
A callgraph() 11 11 1
A callgraphData() 12 12 3
A flamegraph() 11 11 1
A flamegraphData() 12 12 3
A callgraphDataDot() 12 12 3
A deleteAll() 0 14 2

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
use Slim\Slim;
4
5
class Xhgui_Controller_Run extends Xhgui_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    /**
8
     * @var Xhgui_Profiles
9
     */
10
    private $profiles;
11
12
    /**
13
     * @var Xhgui_WatchFunctions
14
     */
15
    private $watches;
16
17
    public function __construct(Slim $app, Xhgui_Profiles $profiles, Xhgui_WatchFunctions $watches)
18
    {
19
        $this->app = $app;
20
        $this->profiles = $profiles;
21
        $this->watches = $watches;
22
    }
23
24
    public function index()
25
    {
26
        $request = $this->app->request();
27
28
        $search = array();
29
        $keys = array('date_start', 'date_end', 'url');
30
        foreach ($keys as $key) {
31
            if ($request->get($key)) {
32
                $search[$key] = $request->get($key);
33
            }
34
        }
35
        $sort = $request->get('sort');
36
37
        $result = $this->profiles->getAll(array(
38
            'sort' => $sort,
39
            'page' => $request->get('page'),
40
            'direction' => $request->get('direction'),
41
            'perPage' => $this->app->config('page.limit'),
42
            'conditions' => $search,
43
            'projection' => true,
44
        ));
45
46
        $title = 'Recent runs';
47
        $titleMap = array(
48
            'wt' => 'Longest wall time',
49
            'cpu' => 'Most CPU time',
50
            'mu' => 'Highest memory use',
51
        );
52
        if (isset($titleMap[$sort])) {
53
            $title = $titleMap[$sort];
54
        }
55
56
        $paging = array(
57
            'total_pages' => $result['totalPages'],
58
            'page' => $result['page'],
59
            'sort' => $sort,
60
            'direction' => $result['direction']
61
        );
62
63
        $this->_template = 'runs/list.twig';
64
        $this->set(array(
65
            'paging' => $paging,
66
            'base_url' => 'home',
67
            'runs' => $result['results'],
68
            'date_format' => $this->app->config('date.format'),
69
            'search' => $search,
70
            'has_search' => strlen(implode('', $search)) > 0,
71
            'title' => $title
72
        ));
73
    }
74
75
    public function view()
76
    {
77
        $request = $this->app->request();
78
        $detailCount = $this->app->config('detail.count');
79
        $result = $this->profiles->get($request->get('id'));
80
81
        $result->calculateSelf();
82
83
        // Self wall time graph
84
        $timeChart = $result->extractDimension('ewt', $detailCount);
85
86
        // Memory Block
87
        $memoryChart = $result->extractDimension('emu', $detailCount);
88
89
        // Watched Functions Block
90
        $watchedFunctions = array();
91
        foreach ($this->watches->getAll() as $watch) {
92
            $matches = $result->getWatched($watch['name']);
93
            if ($matches) {
94
                $watchedFunctions = array_merge($watchedFunctions, $matches);
95
            }
96
        }
97
98
        $profile = $result->sort('ewt', $result->getProfile());
99
100
        $this->_template = 'runs/view.twig';
101
        $this->set(array(
102
            'profile' => $profile,
103
            'result' => $result,
104
            'wall_time' => $timeChart,
105
            'memory' => $memoryChart,
106
            'watches' => $watchedFunctions,
107
            'date_format' => $this->app->config('date.format'),
108
        ));
109
    }
110
111
    public function delete()
112
    {
113
        $request = $this->app->request();
114
        $id = $request->get('id');
115
116
        // Delete the profile run.
117
        $delete = $this->profiles->delete($id);
0 ignored issues
show
Unused Code introduced by
$delete is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
119
        $this->app->flash('success', 'Deleted profile ' . $id);
120
121
        $referrer = $request->getReferrer();
122
        // In case route is accessed directly the referrer is not set.
123
        $redirect = isset($referrer) ? $referrer : $this->app->urlFor('home');
124
        $this->app->redirect($redirect);
125
    }
126
127
    public function deleteAll()
128
    {
129
        $request = $this->app->request();
130
131
        // Delete all profile runs.
132
        $delete = $this->profiles->truncate();
0 ignored issues
show
Unused Code introduced by
$delete is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
134
        $this->app->flash('success', 'Deleted all profiles');
135
136
        $referrer = $request->getReferrer();
137
        // In case route is accessed directly the referrer is not set.
138
        $redirect = isset($referrer) ? $referrer : $this->app->urlFor('home');
139
        $this->app->redirect($redirect);
140
    }
141
142
    public function url()
143
    {
144
        $request = $this->app->request();
145
        $pagination = array(
146
            'sort' => $request->get('sort'),
147
            'direction' => $request->get('direction'),
148
            'page' => $request->get('page'),
149
            'perPage' => $this->app->config('page.limit'),
150
        );
151
152
        $search = array();
153
        $keys = array('date_start', 'date_end', 'limit', 'limit_custom');
154
        foreach ($keys as $key) {
155
            $search[$key] = $request->get($key);
156
        }
157
158
        $runs = $this->profiles->getForUrl(
159
            $request->get('url'),
160
            $pagination,
161
            $search
162
        );
163
164
        if (isset($search['limit_custom']) && strlen($search['limit_custom']) > 0 && $search['limit_custom'][0] == 'P') {
165
            $search['limit'] = $search['limit_custom'];
166
        }
167
168
        $chartData = $this->profiles->getPercentileForUrl(
169
            90,
170
            $request->get('url'),
171
            $search
172
        );
173
174
        $paging = array(
175
            'total_pages' => $runs['totalPages'],
176
            'sort' => $pagination['sort'],
177
            'page' => $runs['page'],
178
            'direction' => $runs['direction']
179
        );
180
181
        $this->_template = 'runs/url.twig';
182
        $this->set(array(
183
            'paging' => $paging,
184
            'base_url' => 'url.view',
185
            'runs' => $runs['results'],
186
            'url' => $request->get('url'),
187
            'chart_data' => $chartData,
188
            'date_format' => $this->app->config('date.format'),
189
            'search' => array_merge($search, array('url' => $request->get('url'))),
190
        ));
191
    }
192
193
    public function compare()
194
    {
195
        $request = $this->app->request();
196
197
        $baseRun = $headRun = $candidates = $comparison = null;
198
        $paging = array();
199
200
        if ($request->get('base')) {
201
            $baseRun = $this->profiles->get($request->get('base'));
202
        }
203
204
        if ($baseRun && !$request->get('head')) {
205
            $pagination = array(
206
                'direction' => $request->get('direction'),
207
                'sort' => $request->get('sort'),
208
                'page' => $request->get('page'),
209
                'perPage' => $this->app->config('page.limit'),
210
            );
211
            $candidates = $this->profiles->getForUrl(
212
                $baseRun->getMeta('simple_url'),
213
                $pagination
214
            );
215
216
            $paging = array(
217
                'total_pages' => $candidates['totalPages'],
218
                'sort' => $pagination['sort'],
219
                'page' => $candidates['page'],
220
                'direction' => $candidates['direction']
221
            );
222
        }
223
224
        if ($request->get('head')) {
225
            $headRun = $this->profiles->get($request->get('head'));
226
        }
227
228
        if ($baseRun && $headRun) {
229
            $comparison = $baseRun->compare($headRun);
230
        }
231
232
        $this->_template = 'runs/compare.twig';
233
        $this->set(array(
234
            'base_url' => 'run.compare',
235
            'base_run' => $baseRun,
236
            'head_run' => $headRun,
237
            'candidates' => $candidates,
238
            'url_params' => $request->get(),
239
            'date_format' => $this->app->config('date.format'),
240
            'comparison' => $comparison,
241
            'paging' => $paging,
242
            'search' => array(
243
                'base' => $request->get('base'),
244
                'head' => $request->get('head'),
245
            )
246
        ));
247
    }
248
249
    public function symbol()
250
    {
251
        $request = $this->app->request();
252
        $id = $request->get('id');
253
        $symbol = $request->get('symbol');
254
255
        $profile = $this->profiles->get($id);
256
        $profile->calculateSelf();
257
        list($parents, $current, $children) = $profile->getRelatives($symbol);
258
259
        $this->_template = 'runs/symbol.twig';
260
        $this->set(array(
261
            'symbol' => $symbol,
262
            'id' => $id,
263
            'main' => $profile->get('main()'),
264
            'parents' => $parents,
265
            'current' => $current,
266
            'children' => $children,
267
        ));
268
    }
269
270
    public function symbolShort()
271
    {
272
        $request = $this->app->request();
273
        $id = $request->get('id');
274
        $threshold = $request->get('threshold');
275
        $symbol = $request->get('symbol');
276
        $metric = $request->get('metric');
277
278
        $profile = $this->profiles->get($id);
279
        $profile->calculateSelf();
280
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
281
282
        $this->_template = 'runs/symbol-short.twig';
283
        $this->set(array(
284
            'symbol' => $symbol,
285
            'id' => $id,
286
            'main' => $profile->get('main()'),
287
            'parents' => $parents,
288
            'current' => $current,
289
            'children' => $children,
290
        ));
291
    }
292
293 View Code Duplication
    public function callgraph()
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...
294
    {
295
        $request = $this->app->request();
296
        $profile = $this->profiles->get($request->get('id'));
297
298
        $this->_template = 'runs/callgraph.twig';
299
        $this->set(array(
300
            'profile' => $profile,
301
            'date_format' => $this->app->config('date.format'),
302
        ));
303
    }
304
305 View Code Duplication
    public function callgraphData()
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...
306
    {
307
        $request = $this->app->request();
308
        $response = $this->app->response();
309
        $profile = $this->profiles->get($request->get('id'));
310
        $metric = $request->get('metric') ?: 'wt';
311
        $threshold = (float)$request->get('threshold') ?: 0.01;
312
        $callgraph = $profile->getCallgraph($metric, $threshold);
313
314
        $response['Content-Type'] = 'application/json';
315
        return $response->body(json_encode($callgraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
316
    }
317
318 View Code Duplication
    public function flamegraph()
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...
319
    {
320
        $request = $this->app->request();
321
        $profile = $this->profiles->get($request->get('id'));
322
323
        $this->_template = 'runs/flamegraph.twig';
324
        $this->set(array(
325
            'profile' => $profile,
326
            'date_format' => $this->app->config('date.format'),
327
        ));
328
    }
329
330 View Code Duplication
    public function flamegraphData()
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...
331
    {
332
        $request = $this->app->request();
333
        $response = $this->app->response();
334
        $profile = $this->profiles->get($request->get('id'));
335
        $metric = $request->get('metric') ?: 'wt';
336
        $threshold = (float)$request->get('threshold') ?: 0.01;
337
        $flamegraph = $profile->getFlamegraph($metric, $threshold);
338
339
        $response['Content-Type'] = 'application/json';
340
        return $response->body(json_encode($flamegraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
341
    }
342
343 View Code Duplication
    public function callgraphDataDot()
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...
344
    {
345
        $request = $this->app->request();
346
        $response = $this->app->response();
347
        $profile = $this->profiles->get($request->get('id'));
348
        $metric = $request->get('metric') ?: 'wt';
349
        $threshold = (float)$request->get('threshold') ?: 0.01;
350
        $callgraph = $profile->getCallgraphNodes($metric, $threshold);
0 ignored issues
show
Bug introduced by
The method getCallgraphNodes() does not exist on Xhgui_Profile. Did you maybe mean getCallgraph()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
351
352
        $response['Content-Type'] = 'application/json';
353
        return $response->body(json_encode($callgraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
354
    }
355
}
356