Completed
Pull Request — master (#268)
by
unknown
03:49 queued 02:35
created

Xhgui_Controller_Run::deleteAllForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Run extends Xhgui_Controller
6
{
7
    /**
8
     * @var Xhgui_Profiles
9
     */
10
    private $profiles;
11
12
    /**
13
     * @var \Xhgui_WatchedFunctionsStorageInterface
14
     */
15
    private $watches;
16
17
    /**
18
     * Xhgui_Controller_Run constructor.
19
     * @param Slim $app
20
     * @param Xhgui_Profiles $profiles
21
     * @param Xhgui_WatchedFunctionsStorageInterface $watches
22
     */
23
    public function __construct(Slim $app, Xhgui_Profiles $profiles, \Xhgui_WatchedFunctionsStorageInterface $watches)
24
    {
25
        $this->app      = $app;
26
        $this->profiles = $profiles;
27
        $this->watches  = $watches;
28
    }
29
30
    /**
31
     *
32
     */
33
    public function index()
34
    {
35
        $request = $this->app->request();
36
37
        $filter = Xhgui_Storage_Filter::fromRequest($request);
38
39
        $result = $this->profiles->getAll($filter);
40
        $title = 'Recent runs';
41
        $titleMap = array(
42
            'wt'    => 'Longest wall time',
43
            'cpu'   => 'Most CPU time',
44
            'mu'    => 'Highest memory use',
45
        );
46
        if (isset($titleMap[$filter->getSort()])) {
47
            $title = $titleMap[$filter->getSort()];
48
        }
49
        $paging = array(
50
            'total_pages'   => $result['totalPages'],
51
            'page'          => $result['page'],
52
            'sort'          => $filter->getSort(),
53
            'direction'     => $result['direction']
54
        );
55
56
        $this->_template = 'runs/list.twig';
57
58
        $this->set(array(
59
            'paging'        => $paging,
60
            'base_url'      => 'home',
61
            'runs'          => $result['results'],
62
            'date_format'   => $this->app->config('date.format'),
63
            'search'        => $filter->toArray(),
64
            'title'         => $title
65
        ));
66
    }
67
68
    /**
69
     *
70
     */
71
    public function view()
72
    {
73
        $request = $this->app->request();
74
        $detailCount = $this->app->config('detail.count');
75
        $result = $this->profiles->get($request->get('id'));
76
77
        $result->calculateSelf();
78
79
        // Self wall time graph
80
        $timeChart = $result->extractDimension('ewt', $detailCount);
81
82
        // Memory Block
83
        $memoryChart = $result->extractDimension('emu', $detailCount);
84
85
        // Watched Functions Block
86
        $watchedFunctions = array();
87
        foreach ($this->watches->getWatchedFunctions() as $watch) {
88
            $matches = $result->getWatched($watch['name']);
89
90
            if ($matches) {
91
                $watchedFunctions = array_merge($watchedFunctions, $matches);
92
            }
93
        }
94
95
        $profile = $result->sort('ewt', $result->getProfile());
96
97
        $this->_template = 'runs/view.twig';
98
        $this->set(array(
99
            'profile'       => $profile,
100
            'result'        => $result,
101
            'wall_time'     => $timeChart,
102
            'memory'        => $memoryChart,
103
            'watches'       => $watchedFunctions,
104
            'date_format'   => $this->app->config('date.format'),
105
        ));
106
    }
107
108
    /**
109
     * @throws Exception
110
     */
111
    public function deleteForm()
112
    {
113
        $request = $this->app->request();
114
        $id = $request->get('id');
115
        if (!is_string($id) || !strlen($id)) {
116
            throw new Exception('The "id" parameter is required.');
117
        }
118
119
        // Get details
120
        $result = $this->profiles->get($id);
121
122
        $this->_template = 'runs/delete-form.twig';
123
        $this->set(array(
124
            'run_id' => $id,
125
            'result' => $result,
126
        ));
127
    }
128
129
    /**
130
     * @throws Exception
131
     */
132
    public function deleteSubmit()
133
    {
134
        $request = $this->app->request();
135
        $id = $request->post('id');
136
137
        // Don't call profilers->delete() unless $id is set,
138
        // otherwise it will turn the null into a MongoId and return "Sucessful".
139
        if (!is_string($id) || !strlen($id)) {
140
            // Form checks this already,
141
            // only reachable by handcrafted or malformed requests.
142
            throw new Exception('The "id" parameter is required.');
143
        }
144
145
        // Delete the profile run.
146
        $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...
147
148
        $this->app->flash('success', 'Deleted profile ' . $id);
149
150
        $this->app->redirect($this->app->urlFor('home'));
151
    }
152
153
    /**
154
     *
155
     */
156
    public function deleteAllForm()
157
    {
158
        $this->_template = 'runs/delete-all-form.twig';
159
    }
160
161
    /**
162
     *
163
     */
164
    public function deleteAllSubmit()
165
    {
166
        $request = $this->app->request();
0 ignored issues
show
Unused Code introduced by
$request 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...
167
168
        // Delete all profile runs.
169
        $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...
170
171
        $this->app->flash('success', 'Deleted all profiles');
172
173
        $this->app->redirect($this->app->urlFor('home'));
174
    }
175
176
    /**
177
     *
178
     */
179
    public function url()
180
    {
181
        $request = $this->app->request();
182
183
        $filter = Xhgui_Storage_Filter::fromRequest($request);
184
        $filter->setUrl($request->get('url'));
185
        $result = $this->profiles->getAll($filter);
186
187
        $chartData = $this->profiles->getPercentileForUrl(
188
            90,
189
            $request->get('url'),
190
            $filter
191
        );
192
193
        $paging = array(
194
            'total_pages'   => $result['totalPages'],
195
            'sort'          => $filter->getSort(),
196
            'page'          => $result['page'],
197
            'direction'     => $result['direction']
198
        );
199
200
        $this->_template = 'runs/url.twig';
201
        $this->set(array(
202
            'paging'        => $paging,
203
            'base_url'      => 'url.view',
204
            'runs'          => $result['results'],
205
            'url'           => $filter->getUrl('url'),
0 ignored issues
show
Unused Code introduced by
The call to Xhgui_Storage_Filter::getUrl() has too many arguments starting with 'url'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
206
            'chart_data'    => $chartData,
207
            'date_format'   => $this->app->config('date.format'),
208
            'search'        => array_merge($filter->toArray(), array('url' => $request->get('url'))),
209
        ));
210
    }
211
212
    /**
213
     *
214
     */
215
    public function compare()
216
    {
217
        $request = $this->app->request();
218
219
        $baseRun = $headRun = $candidates = $comparison = null;
220
        $paging = array();
221
222
        if ($request->get('base')) {
223
            $baseRun = $this->profiles->get($request->get('base'));
224
        }
225
226
        // we have one selected but we need to list other runs.
227
        if ($baseRun && !$request->get('head')) {
228
            $filter = Xhgui_Storage_Filter::fromRequest($request);
229
            $filter->setUrl($baseRun->getMeta('simple_url'));
230
231
            $candidates = $this->profiles->getAll($filter);
232
233
            $paging = array(
234
                'total_pages'   => $candidates['totalPages'],
235
                'page'          => $candidates['page'],
236
                'sort'          => $filter->getSort(),
237
                'direction'     => $candidates['direction']
238
            );
239
        }
240
241
        if ($request->get('head')) {
242
            $headRun = $this->profiles->get($request->get('head'));
243
        }
244
245
        if ($baseRun && $headRun) {
246
            $comparison = $baseRun->compare($headRun);
247
        }
248
249
        $this->_template = 'runs/compare.twig';
250
        $this->set(array(
251
            'base_url'      => 'run.compare',
252
            'base_run'      => $baseRun,
253
            'head_run'      => $headRun,
254
            'candidates'    => $candidates,
255
            'url_params'    => $request->get(),
256
            'date_format'   => $this->app->config('date.format'),
257
            'comparison'    => $comparison,
258
            'paging'        => $paging,
259
            'search'        => array(
260
                'base' => $request->get('base'),
261
                'head' => $request->get('head'),
262
            )
263
        ));
264
    }
265
266
    /**
267
     *
268
     */
269
    public function symbol()
270
    {
271
        $request    = $this->app->request();
272
        $id         = $request->get('id');
273
        $symbol     = $request->get('symbol');
274
275
        $profile = $this->profiles->get($id);
276
        $profile->calculateSelf();
277
        list($parents, $current, $children) = $profile->getRelatives($symbol);
278
279
        $this->_template = 'runs/symbol.twig';
280
        $this->set(array(
281
            'symbol'    => $symbol,
282
            'id'        => $id,
283
            'main'      => $profile->get('main()'),
284
            'parents'   => $parents,
285
            'current'   => $current,
286
            'children'  => $children,
287
        ));
288
    }
289
290
    /**
291
     *
292
     */
293
    public function symbolShort()
294
    {
295
        $request    = $this->app->request();
296
        $id         = $request->get('id');
297
        $threshold  = $request->get('threshold');
298
        $symbol     = $request->get('symbol');
299
        $metric     = $request->get('metric');
300
301
        $profile = $this->profiles->get($id);
302
        $profile->calculateSelf();
303
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
304
305
        $this->_template = 'runs/symbol-short.twig';
306
        $this->set(array(
307
            'symbol'    => $symbol,
308
            'id'        => $id,
309
            'main'      => $profile->get('main()'),
310
            'parents'   => $parents,
311
            'current'   => $current,
312
            'children'  => $children,
313
        ));
314
    }
315
316
    /**
317
     *
318
     */
319
    public function callgraph()
320
    {
321
        $request = $this->app->request();
322
        $profile = $this->profiles->get($request->get('id'));
323
324
        $this->_template = 'runs/callgraph.twig';
325
        $this->set(array(
326
            'profile'       => $profile,
327
            'date_format'   => $this->app->config('date.format'),
328
        ));
329
    }
330
331
    /**
332
     * @return string
333
     * @throws Exception
334
     */
335 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...
336
    {
337
        $request    = $this->app->request();
338
        $response   = $this->app->response();
339
        $profile    = $this->profiles->get($request->get('id'));
340
        $metric     = $request->get('metric') ?: 'wt';
341
        $threshold  = (float)$request->get('threshold') ?: 0.01;
342
        $callgraph  = $profile->getCallgraph($metric, $threshold);
343
344
        $response['Content-Type'] = 'application/json';
345
        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...
346
    }
347
348
    /**
349
     * @return string
350
     */
351 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...
352
    {
353
        $request    = $this->app->request();
354
        $response   = $this->app->response();
355
        $profile    = $this->profiles->get($request->get('id'));
356
        $metric     = $request->get('metric') ?: 'wt';
357
        $threshold  = (float)$request->get('threshold') ?: 0.01;
358
        $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...
359
360
        $response['Content-Type'] = 'application/json';
361
        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...
362
    }
363
}
364