Passed
Pull Request — develop (#184)
by Felipe
06:07 queued 31s
created

HTMLNavbarController::printTabs()   C

Complexity

Conditions 14
Paths 32

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 6.3132
c 0
b 0
f 0
cc 14
eloc 30
nc 32
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\XHtml;
8
9
/**
10
 * Class to render tables. Formerly part of Misc.php.
11
 */
12
class HTMLNavbarController extends HTMLController
13
{
14
    public $controller_name = 'HTMLNavbarController';
15
16
    /**
17
     * Display a bread crumb trail.
18
     *
19
     * @param array|string $trail an array of breadcrumb items, or a string to identify one of them
20
     * @param  $do_print true to echo, false to return html
21
     * @param null|string $from
22
     */
23
    public function printTrail($trail = [], $do_print = true, $from = null)
24
    {
25
        if (null === $from) {
26
            $from = __METHOD__;
27
        }
28
        $lang       = $this->lang;
29
        $this->misc = $this->misc;
30
        $data       = $this->misc->getDatabaseAccessor();
31
32
        $trail_html = $this->printTopbar(false, $from);
33
34
        if (is_string($trail)) {
35
            $trail = $this->getTrail($trail);
36
        }
37
38
        $crumbs = [];
39
        foreach ($trail as $crumb_id => $crumb) {
40
            if (isset($crumb['url'])) {
41
                $crumbs[$crumb_id]['url'] = str_replace('&amp;', '&', $crumb['url']);
42
            }
43
44
            if (isset($crumb['title'])) {
45
                $crumbs[$crumb_id]['title']   = $crumb['title'];
46
                $crumbs[$crumb_id]['iconalt'] = $crumb['title'];
47
            } else {
48
                $crumbs[$crumb_id]['iconalt'] = 'Database Root';
49
            }
50
51
            if (isset($crumb['icon']) && $icon = $this->misc->icon($crumb['icon'])) {
52
                $crumbs[$crumb_id]['icon'] = $icon;
53
            }
54
55
            $crumbs[$crumb_id]['text'] = $crumb['text'];
56
57
            if (isset($crumb['help'])) {
58
                $crumbs[$crumb_id]['helpurl'] = str_replace('&amp;', '&', $this->misc->getHelpLink($crumb['help']));
59
            }
60
        }
61
62
        $viewVars = [
63
            'crumbs'          => $crumbs,
64
65
            'controller_name' => $this->controller_name,
66
        ];
67
        if (isset($crumbs['database'])) {
68
            $search_path_crumbs = [];
69
            $dburl              = $crumbs['database']['url'];
70
            $search_paths       = $data->getSearchPath();
71
            foreach ($search_paths as $schema) {
72
                $search_path_crumbs[$schema] = [
73
                    'title'   => $lang['strschema'],
74
                    'text'    => $schema,
75
                    'icon'    => $this->misc->icon('Schema'),
76
                    'iconalt' => $lang['strschema'],
77
                    'url'     => str_replace(['&amp;', 'redirect/database'], ['&', 'redirect/schema'], $dburl.'&schema='.$schema),
78
                ];
79
            }
80
            $viewVars['search_paths'] = $search_path_crumbs;
81
        }
82
        //\Kint::dump($viewVars);
83
84
        $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars);
85
86
        if ($do_print) {
87
            echo $trail_html;
88
        } else {
89
            return $trail_html;
90
        }
91
    }
92
93
    /**
94
     * Display navigation tabs.
95
     *
96
     * @param string      $alltabs   The name of current section (Ex: intro, server, ...),
97
     *                               or an array with tabs (Ex: sqledit::doFind function)
98
     * @param string      $activetab the name of the tab to be highlighted
99
     * @param bool        $print     if false, return html
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $print does not match actual variable name $do_print
Loading history...
100
     * @param bool        $do_print  true to print html, false to return html
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $do_print does not match actual variable name $from
Loading history...
101
     * @param null|string $from      whichi method is calling this one
102
     */
103
    public function printTabs($alltabs, $activetab, $do_print = true, $from = null)
104
    {
105
        if (null === $from || false === $from) {
106
            $from = __METHOD__;
107
        }
108
109
        $lang       = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
110
        $this->misc = $this->misc;
111
112
        if (is_string($alltabs)) {
1 ignored issue
show
introduced by
The condition is_string($alltabs) is always true.
Loading history...
113
            $_SESSION['webdbLastTab'][$alltabs] = $activetab;
114
            $alltabs                            = $this->misc->getNavTabs($alltabs);
115
        }
116
        //$this->prtrace($tabs);
117
        $tabs_html = '';
118
119
        //Getting only visible tabs
120
        $tabs = [];
121
        if (count($alltabs) > 0) {
122
            foreach ($alltabs as $tab_id => $tab) {
123
                if (!isset($tab['hide']) || true !== $tab['hide']) {
124
                    $tabs[$tab_id]            = $tab;
125
                    $tabs[$tab_id]['active']  = $active  = ($tab_id == $activetab) ? ' active' : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $active is dead and can be removed.
Loading history...
126
                    $tabs[$tab_id]['tablink'] = str_replace(['&amp;', '.php'], ['&', ''], htmlentities($this->getActionUrl($tab, $_REQUEST, $from)));
127
                    //$this->prtrace('link for ' . $tab_id, $tabs[$tab_id]['tablink']);
128
                    if (isset($tab['icon']) && $icon = $this->misc->icon($tab['icon'])) {
129
                        $tabs[$tab_id]['iconurl'] = $icon;
130
                    }
131
                    if (isset($tab['help'])) {
132
                        $tabs[$tab_id]['helpurl'] = str_replace('&amp;', '&', $this->misc->getHelpLink($tab['help']));
133
                    }
134
                }
135
            }
136
        }
137
138
        //$this->prtrace($tabs);
139
140
        if (count($tabs) > 0) {
141
            $width = (int) (100 / count($tabs)).'%';
142
143
            $viewVars = [
144
                'width'           => $width,
145
                'tabs'            => $tabs,
146
                'controller_name' => $this->controller_name,
147
            ];
148
149
            $tabs_html = $this->getContainer()->view->fetch('components/tabs.twig', $viewVars);
150
        }
151
152
        if ($do_print) {
153
            echo $tabs_html;
154
        } else {
155
            return $tabs_html;
156
        }
157
    }
158
159
    /**
160
     * [printTopbar description].
161
     *
162
     * @param bool       $do_print true to print, false to return html
163
     * @param null|mixed $from     which method is calling this one
164
     *
165
     * @return string
166
     */
167
    private function printTopbar($do_print = true, $from = null)
1 ignored issue
show
Coding Style introduced by
Private method name "HTMLNavbarController::printTopbar" must be prefixed with an underscore
Loading history...
168
    {
169
        if (null === $from || false === $from) {
170
            $from = __METHOD__;
171
        }
172
173
        $lang           = $this->lang;
174
        $plugin_manager = $this->plugin_manager;
175
        $this->misc     = $this->misc;
176
        $appName        = $this->misc->appName;
177
        $appVersion     = $this->misc->appVersion;
178
179
        $server_info = $this->misc->getServerInfo();
180
        $server_id   = $this->misc->getServerId();
1 ignored issue
show
Unused Code introduced by
The assignment to $server_id is dead and can be removed.
Loading history...
181
        $reqvars     = $this->misc->getRequestVars('table');
182
183
        $topbar_html = '<div class="topbar" data-controller="'.$this->controller_name.'"><table style="width: 100%"><tr><td>';
184
185
        if ($server_info && isset($server_info['platform'], $server_info['username'])) {
186
            // top left informations when connected
187
            $topbar_html .= sprintf(
188
                $lang['strtopbar'],
189
                '<span class="platform">'.htmlspecialchars($server_info['platform']).'</span>',
190
                '<span class="host">'.htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']).'</span>',
191
                '<span class="port">'.htmlspecialchars($server_info['port']).'</span>',
192
                '<span class="username">'.htmlspecialchars($server_info['username']).'</span>'
193
            );
194
195
            $topbar_html .= '</td>';
196
197
            // top right informations when connected
198
199
            $toplinks = [
200
                'sql'     => [
201
                    'attr'    => [
202
                        'href'   => [
203
                            'url'     => SUBFOLDER.'/src/views/sqledit',
204
                            'urlvars' => array_merge($reqvars, [
205
                                'action' => 'sql',
206
                            ]),
207
                        ],
208
                        'target' => 'sqledit',
209
                        'id'     => 'toplink_sql',
210
                    ],
211
                    'content' => $lang['strsql'],
212
                ],
213
                'history' => [
214
                    'attr'    => [
215
                        'href' => [
216
                            'url'     => SUBFOLDER.'/src/views/history',
217
                            'urlvars' => array_merge($reqvars, [
218
                                'action' => 'pophistory',
219
                            ]),
220
                        ],
221
                        'id'   => 'toplink_history',
222
                    ],
223
                    'content' => $lang['strhistory'],
224
                ],
225
                'find'    => [
226
                    'attr'    => [
227
                        'href'   => [
228
                            'url'     => SUBFOLDER.'/src/views/sqledit',
229
                            'urlvars' => array_merge($reqvars, [
230
                                'action' => 'find',
231
                            ]),
232
                        ],
233
                        'target' => 'sqledit',
234
                        'id'     => 'toplink_find',
235
                    ],
236
                    'content' => $lang['strfind'],
237
                ],
238
                'logout'  => [
239
                    'attr'    => [
240
                        'href' => [
241
                            'url'     => SUBFOLDER.'/src/views/servers',
242
                            'urlvars' => [
243
                                'action'       => 'logout',
244
                                'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"),
245
                            ],
246
                        ],
247
                        'id'   => 'toplink_logout',
248
                    ],
249
                    'content' => $lang['strlogout'],
250
                ],
251
            ];
252
253
            // Toplink hook's place
254
            $plugin_functions_parameters = [
255
                'toplinks' => &$toplinks,
256
            ];
257
258
            $plugin_manager->doHook('toplinks', $plugin_functions_parameters);
259
260
            $topbar_html .= '<td style="text-align: right">';
261
262
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], false, $from);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string expected by parameter $from of PHPPgAdmin\XHtml\HTMLController::printLinksList(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

262
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], /** @scrutinizer ignore-type */ false, $from);
Loading history...
Unused Code introduced by
The call to PHPPgAdmin\XHtml\HTMLController::printLinksList() has too many arguments starting with $from. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

262
            $topbar_html .= $this->/** @scrutinizer ignore-call */ printLinksList($toplinks, 'toplink', [], false, $from);

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. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
array() of type array is incompatible with the type boolean expected by parameter $do_print of PHPPgAdmin\XHtml\HTMLController::printLinksList(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

262
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', /** @scrutinizer ignore-type */ [], false, $from);
Loading history...
263
264
            $topbar_html .= '</td>';
265
        } else {
266
            $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>";
267
        }
268
269
        $topbar_html .= "</tr></table></div>\n";
270
271
        if ($do_print) {
272
            echo $topbar_html;
273
        } else {
274
            return $topbar_html;
275
        }
276
    }
277
278
    private function getHREFSubject($subject)
1 ignored issue
show
Coding Style introduced by
Private method name "HTMLNavbarController::getHREFSubject" must be prefixed with an underscore
Loading history...
279
    {
280
        $vars = $this->misc->getSubjectParams($subject);
281
        ksort($vars['params']);
282
283
        return "{$vars['url']}?".http_build_query($vars['params'], '', '&amp;');
284
    }
285
286
    /**
287
     * Create a bread crumb trail of the object hierarchy.
288
     *
289
     * @param null|string $subject sunkect of the trail
290
     */
291
    private function getTrail($subject = null)
1 ignored issue
show
Coding Style introduced by
Private method name "HTMLNavbarController::getTrail" must be prefixed with an underscore
Loading history...
292
    {
293
        $lang           = $this->lang;
294
        $plugin_manager = $this->plugin_manager;
295
        $this->misc     = $this->misc;
296
        $appName        = $this->misc->appName;
297
298
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
299
300
        $trail = [];
301
        $vars  = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $vars is dead and can be removed.
Loading history...
302
        $done  = false;
303
304
        $trail['root'] = [
305
            'text' => $appName,
306
            'url'  => SUBFOLDER.'/src/views/servers',
307
            'icon' => 'Introduction',
308
        ];
309
310
        if ('root' == $subject) {
311
            $done = true;
312
        }
313
314
        if (!$done) {
315
            $server_info     = $this->misc->getServerInfo();
316
            $trail['server'] = [
317
                'title' => $lang['strserver'],
318
                'text'  => $server_info['desc'],
319
                'url'   => $this->getHREFSubject('server'),
320
                'help'  => 'pg.server',
321
                'icon'  => 'Server',
322
            ];
323
        }
324
        if ('server' == $subject) {
325
            $done = true;
326
        }
327
328
        if (isset($_REQUEST['database']) && !$done) {
329
            $trail['database'] = [
330
                'title' => $lang['strdatabase'],
331
                'text'  => $_REQUEST['database'],
332
                'url'   => $this->getHREFSubject('database'),
333
                'help'  => 'pg.database',
334
                'icon'  => 'Database',
335
            ];
336
        } elseif (isset($_REQUEST['rolename']) && !$done) {
337
            $trail['role'] = [
338
                'title' => $lang['strrole'],
339
                'text'  => $_REQUEST['rolename'],
340
                'url'   => $this->getHREFSubject('role'),
341
                'help'  => 'pg.role',
342
                'icon'  => 'Roles',
343
            ];
344
        }
345
        if ('database' == $subject || 'role' == $subject) {
346
            $done = true;
347
        }
348
349
        if (isset($_REQUEST['schema']) && !$done) {
350
            $trail['schema'] = [
351
                'title' => $lang['strschema'],
352
                'text'  => $_REQUEST['schema'],
353
                'url'   => $this->getHREFSubject('schema'),
354
                'help'  => 'pg.schema',
355
                'icon'  => 'Schema',
356
            ];
357
        }
358
        if ('schema' == $subject) {
359
            $done = true;
360
        }
361
362
        if (isset($_REQUEST['table']) && !$done) {
363
            $trail['table'] = [
364
                'title' => $lang['strtable'],
365
                'text'  => $_REQUEST['table'],
366
                'url'   => $this->getHREFSubject('table'),
367
                'help'  => 'pg.table',
368
                'icon'  => 'Table',
369
            ];
370
        } elseif (isset($_REQUEST['view']) && !$done) {
371
            $trail['view'] = [
372
                'title' => $lang['strview'],
373
                'text'  => $_REQUEST['view'],
374
                'url'   => $this->getHREFSubject('view'),
375
                'help'  => 'pg.view',
376
                'icon'  => 'View',
377
            ];
378
        } elseif (isset($_REQUEST['matview']) && !$done) {
379
            $trail['matview'] = [
380
                'title' => 'M'.$lang['strview'],
381
                'text'  => $_REQUEST['matview'],
382
                'url'   => $this->getHREFSubject('matview'),
383
                'help'  => 'pg.matview',
384
                'icon'  => 'MViews',
385
            ];
386
        } elseif (isset($_REQUEST['ftscfg']) && !$done) {
387
            $trail['ftscfg'] = [
388
                'title' => $lang['strftsconfig'],
389
                'text'  => $_REQUEST['ftscfg'],
390
                'url'   => $this->getHREFSubject('ftscfg'),
391
                'help'  => 'pg.ftscfg.example',
392
                'icon'  => 'Fts',
393
            ];
394
        }
395
        if ('table' == $subject || 'view' == $subject || 'matview' == $subject || 'ftscfg' == $subject) {
396
            $done = true;
397
        }
398
399
        if (!$done && !is_null($subject)) {
400
            switch ($subject) {
401
                case 'function':
402
                    $trail[$subject] = [
403
                        'title' => $lang['str'.$subject],
404
                        'text'  => $_REQUEST[$subject],
405
                        'url'   => $this->getHREFSubject('function'),
406
                        'help'  => 'pg.function',
407
                        'icon'  => 'Function',
408
                    ];
409
410
                    break;
411
                case 'aggregate':
412
                    $trail[$subject] = [
413
                        'title' => $lang['straggregate'],
414
                        'text'  => $_REQUEST['aggrname'],
415
                        'url'   => $this->getHREFSubject('aggregate'),
416
                        'help'  => 'pg.aggregate',
417
                        'icon'  => 'Aggregate',
418
                    ];
419
420
                    break;
421
                case 'column':
422
                    $trail['column'] = [
423
                        'title' => $lang['strcolumn'],
424
                        'text'  => $_REQUEST['column'],
425
                        'icon'  => 'Column',
426
                        'url'   => $this->getHREFSubject('column'),
427
                    ];
428
429
                    break;
430
                default:
431
                    if (isset($_REQUEST[$subject])) {
432
                        switch ($subject) {
433
                            case 'domain':
434
                                $icon = 'Domain';
435
436
                                break;
437
                            case 'sequence':
438
                                $icon = 'Sequence';
439
440
                                break;
441
                            case 'type':
442
                                $icon = 'Type';
443
444
                                break;
445
                            case 'operator':
446
                                $icon = 'Operator';
447
448
                                break;
449
                            case 'index':
450
                                $icon = 'Index';
451
452
                                break;
453
                            default:
454
                                $icon = null;
455
456
                                break;
457
                        }
458
                        $trail[$subject] = [
459
                            'title' => array_key_exists('str'.$subject, $lang) ? $lang['str'.$subject] : $subject,
460
                            'text'  => $_REQUEST[$subject],
461
                            'help'  => 'pg.'.$subject,
462
                            'icon'  => $icon,
463
                        ];
464
                    }
465
            }
466
        }
467
468
        // Trail hook's place
469
        $plugin_functions_parameters = [
470
            'trail'   => &$trail,
471
            'section' => $subject,
472
        ];
473
474
        $plugin_manager->doHook('trail', $plugin_functions_parameters);
475
476
        //$this->prtrace($trail);
477
478
        return $trail;
479
    }
480
}
481