Passed
Push — master ( 965dbc...c6b243 )
by Felipe
11:26 queued 06:11
created

ServersController::getServersGroups()   C

Complexity

Conditions 10
Paths 6

Size

Total Lines 66
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 6.2025
c 0
b 0
f 0
cc 10
eloc 39
nc 6
nop 2

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.33
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class ServersController extends BaseController
17
{
18
19
    use ServersTrait;
1 ignored issue
show
introduced by
The trait PHPPgAdmin\Controller\ServersTrait requires some properties which are not provided by PHPPgAdmin\Controller\ServersController: $EOF, $fields
Loading history...
20
21
    public $controller_name = 'ServersController';
22
    public $table_place     = 'servers-servers';
23
    public $section         = 'servers';
24
    public $query           = '';
25
    public $subject         = '';
26
    public $start_time;
27
    public $duration;
28
29
    /**
30
     * Default method to render the controller according to the action parameter.
31
     */
32
    public function render()
33
    {
34
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
35
36
        $action = $this->action;
37
38
        if ('tree' == $action) {
39
            return $this->doTree();
40
        }
41
42
        $msg = $this->msg;
43
44
        $server_html = $this->printHeader($this->lang['strservers'], null, false);
45
        $server_html .= $this->printBody(false);
46
        $server_html .= $this->printTrail('root', false);
47
48
        ob_start();
49
        switch ($action) {
50
            case 'logout':
51
                $this->doLogout();
52
53
                break;
54
            default:
55
                $this->doDefault($msg);
56
57
                break;
58
        }
59
60
        $server_html .= ob_get_clean();
61
62
        $server_html .= $this->printFooter(false);
63
64
        if (null === $this->container->requestobj->getAttribute('route')) {
65
            echo $server_html;
66
        } else {
67
            $body = $this->container->responseobj->getBody();
68
            $body->write($server_html);
69
70
            return $this->container->responseobj;
71
        }
72
    }
73
74
    public function doDefault($msg = '')
75
    {
76
        $lang = $this->lang;
77
78
        $this->printTabs('root', 'servers');
79
        $this->printMsg($msg);
80
        $group = isset($_GET['group']) ? $_GET['group'] : false;
81
82
        $groups  = $this->getServersGroups(true, $group);
83
        $columns = [
84
            'group' => [
85
                'title' => $lang['strgroup'],
86
                'field' => Decorator::field('desc'),
87
                'url'   => 'servers?',
88
                'vars'  => ['group' => 'id'],
89
            ],
90
        ];
91
        $actions = [];
92
        if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
93
            (false !== $group) &&
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
94
            (isset($this->conf['srv_groups'][$group])) &&
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
95
            ($groups->recordCount() > 0)
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
96
        ) {
97
            $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')));
98
            echo $this->printTable($groups, $columns, $actions, $this->table_place);
99
        }
100
101
        $servers = $this->getServers(true, $group);
102
103
        $columns = [
104
            'server'   => [
105
                'title' => $lang['strserver'],
106
                'field' => Decorator::field('desc'),
107
                'url'   => \SUBFOLDER . '/redirect/server?',
108
                'vars'  => ['server' => 'sha'],
109
            ],
110
            'host'     => [
111
                'title' => $lang['strhost'],
112
                'field' => Decorator::field('host'),
113
            ],
114
            'port'     => [
115
                'title' => $lang['strport'],
116
                'field' => Decorator::field('port'),
117
            ],
118
            'username' => [
119
                'title' => $lang['strusername'],
120
                'field' => Decorator::field('username'),
121
            ],
122
            'actions'  => [
123
                'title' => $lang['stractions'],
124
            ],
125
        ];
126
127
        $actions = [
128
            'logout' => [
129
                'content' => $lang['strlogout'],
130
                'attr'    => [
131
                    'href' => [
132
                        'url'     => 'servers',
133
                        'urlvars' => [
134
                            'action'       => 'logout',
135
                            'logoutServer' => Decorator::field('id'),
136
                        ],
137
                    ],
138
                ],
139
            ],
140
        ];
141
142
        $svPre = function (&$rowdata) use ($actions) {
143
            $actions['logout']['disable'] = empty($rowdata->fields['username']);
144
145
            return $actions;
146
        };
147
148
        if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
149
            (false !== $group) &&
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
150
            isset($this->conf['srv_groups'][$group])
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
151
        ) {
152
            $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')), null);
153
            $actions['logout']['attr']['href']['urlvars']['group'] = $group;
154
        }
155
        echo $this->printTable($servers, $columns, $actions, $this->table_place, $lang['strnoobjects'], $svPre);
156
    }
157
158
    public function doTree()
159
    {
160
        $nodes    = [];
161
        $group_id = isset($_GET['group']) ? $_GET['group'] : false;
162
163
        // root with srv_groups
164
        if (isset($this->conf['srv_groups']) and count($this->conf['srv_groups']) > 0
165
            and false === $group_id) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
166
            $nodes = $this->getServersGroups(true);
167
        } elseif (isset($this->conf['srv_groups']) and false !== $group_id) {
168
            // group subtree
169
            if ('all' !== $group_id) {
170
                $nodes = $this->getServersGroups(false, $group_id);
171
            }
172
173
            $nodes = array_merge($nodes, $this->getServers(false, $group_id));
2 ignored issues
show
Bug introduced by
It seems like $nodes can also be of type PHPPgAdmin\ArrayRecordSet; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

173
            $nodes = array_merge(/** @scrutinizer ignore-type */ $nodes, $this->getServers(false, $group_id));
Loading history...
Bug introduced by
It seems like $this->getServers(false, $group_id) can also be of type PHPPgAdmin\ArrayRecordSet; however, parameter $array2 of array_merge() does only seem to accept null|array, maybe add an additional type check? ( Ignorable by Annotation )

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

173
            $nodes = array_merge($nodes, /** @scrutinizer ignore-type */ $this->getServers(false, $group_id));
Loading history...
174
            $nodes = new \PHPPgAdmin\ArrayRecordSet($nodes);
175
        } else {
176
            // no srv_group
177
            $nodes = $this->getServers(true, false);
178
        }
179
180
        $reqvars = $this->misc->getRequestVars('server');
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
181
182
        //$this->prtrace($reqvars);
183
184
        $attrs = [
185
            'text'    => Decorator::field('desc'),
186
            // Show different icons for logged in/out
187
            'icon'    => Decorator::field('icon'),
188
            'toolTip' => Decorator::field('id'),
189
            'action'  => Decorator::field('action'),
190
            // Only create a branch url if the user has
191
            // logged into the server.
192
            'branch'  => Decorator::field('branch'),
193
        ];
194
        /*$this->prtrace([
195
        'nodes'   => $nodes,
196
        'attrs'   => $attrs,
197
        'section' => $this->section,
198
        ]);*/
199
        return $this->printTree($nodes, $attrs, $this->section);
200
    }
201
202
    public function doLogout()
203
    {
204
        $plugin_manager = $this->plugin_manager;
205
        $lang           = $this->lang;
206
        $this->misc     = $this->misc;
207
        $conf           = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
208
        $data           = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
209
210
        $plugin_manager->do_hook('logout', $_REQUEST['logoutServer']);
211
212
        $server_info = $this->misc->getServerInfo($_REQUEST['logoutServer']);
213
        $this->misc->setServerInfo(null, null, $_REQUEST['logoutServer']);
214
215
        unset($_SESSION['sharedUsername'], $_SESSION['sharedPassword']);
216
217
        $this->misc->setReloadBrowser(true);
218
219
        echo sprintf($lang['strlogoutmsg'], $server_info['desc']);
220
    }
221
222
    /**
223
     * Get list of server groups.
224
     *
225
     * @param bool  $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array
226
     * @param mixed $group_id  a group name to filter the returned servers using $this->conf[srv_groups]
227
     *
228
     * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable
229
     */
230
    private function getServersGroups($recordset = false, $group_id = false)
1 ignored issue
show
Coding Style introduced by
Private method name "ServersController::getServersGroups" must be prefixed with an underscore
Loading history...
231
    {
232
        $lang = $this->lang;
233
        $grps = [];
234
235
        if (isset($this->conf['srv_groups'])) {
236
            foreach ($this->conf['srv_groups'] as $i => $group) {
237
                if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
238
                    (($group_id === false) and (!isset($group['parents']))) /* root */
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
239
                    or (
240
                        ($group_id !== false)
2 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
241
                        and isset($group['parents'])
1 ignored issue
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
242
                        and in_array($group_id, explode(
2 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
243
                            ',',
244
                            preg_replace('/\s/', '', $group['parents'])
245
                        ), true)
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
246
                    ) /* nested group */
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
247
                ) {
248
                    $grps[$i] = [
249
                        'id'     => $i,
250
                        'desc'   => $group['desc'],
251
                        'icon'   => 'Servers',
252
                        'action' => Decorator::url(
253
                            'servers',
254
                            [
255
                                'group' => Decorator::field('id'),
256
                            ]
257
                        ),
258
                        'branch' => Decorator::url(
259
                            'servers',
260
                            [
261
                                'action' => 'tree',
262
                                'group'  => $i,
263
                            ]
264
                        ),
265
                    ];
266
                }
267
            }
268
269
            if ($group_id === false) {
270
                $grps['all'] = [
271
                    'id'     => 'all',
272
                    'desc'   => $lang['strallservers'],
273
                    'icon'   => 'Servers',
274
                    'action' => Decorator::url(
275
                        'servers',
276
                        [
277
                            'group' => Decorator::field('id'),
278
                        ]
279
                    ),
280
                    'branch' => Decorator::url(
281
                        'servers',
282
                        [
283
                            'action' => 'tree',
284
                            'group'  => 'all',
285
                        ]
286
                    ),
287
                ];
288
            }
289
        }
290
291
        if ($recordset) {
292
            return new \PHPPgAdmin\ArrayRecordSet($grps);
293
        }
294
295
        return $grps;
296
    }
297
}
298