Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

GroupsController::doProperties()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 48
nc 8
nop 1
dl 0
loc 73
rs 8.5021
c 0
b 0
f 0

How to fix   Long Method   

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
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class GroupsController extends BaseController
15
{
16
    public $controller_name = 'GroupsController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $this->printHeader($lang['strgroups']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $lang seems to be never defined.
Loading history...
24
        $this->printBody();
25
26
        switch ($action) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $action seems to be never defined.
Loading history...
27
            case 'add_member':
28
                $this->doAddMember();
29
30
                break;
31
            case 'drop_member':
32
                if (isset($_REQUEST['drop'])) {
33
                    $this->doDropMember(false);
34
                } else {
35
                    $this->doProperties();
36
                }
37
38
                break;
39
            case 'confirm_drop_member':
40
                $this->doDropMember(true);
41
42
                break;
43
            case 'save_create':
44
                if (isset($_REQUEST['cancel'])) {
45
                    $this->doDefault();
46
                } else {
47
                    $this->doSaveCreate();
48
                }
49
50
                break;
51
            case 'create':
52
                $this->doCreate();
53
54
                break;
55
            case 'drop':
56
                if (isset($_REQUEST['drop'])) {
57
                    $this->doDrop(false);
58
                } else {
59
                    $this->doDefault();
60
                }
61
62
                break;
63
            case 'confirm_drop':
64
                $this->doDrop(true);
65
66
                break;
67
            case 'save_edit':
68
                $this->doSaveEdit();
0 ignored issues
show
Bug introduced by
The method doSaveEdit() does not exist on PHPPgAdmin\Controller\GroupsController. ( Ignorable by Annotation )

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

68
                $this->/** @scrutinizer ignore-call */ 
69
                       doSaveEdit();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
70
                break;
71
            case 'edit':
72
                $this->doEdit();
0 ignored issues
show
Bug introduced by
The method doEdit() does not exist on PHPPgAdmin\Controller\GroupsController. ( Ignorable by Annotation )

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

72
                $this->/** @scrutinizer ignore-call */ 
73
                       doEdit();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
                break;
75
            case 'properties':
76
                $this->doProperties();
77
78
                break;
79
            default:
80
                $this->doDefault();
81
82
                break;
83
        }
84
85
        $this->printFooter();
86
    }
87
88
    /**
89
     * Show default list of groups in the database.
90
     *
91
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
92
     */
93
    public function doDefault($msg = '')
94
    {
95
        $lang = $this->lang;
96
        $data = $this->misc->getDatabaseAccessor();
97
98
        $this->printTrail('server');
99
        $this->printTabs('server', 'groups');
100
        $this->printMsg($msg);
101
102
        $groups = $data->getGroups();
103
104
        $columns = [
105
            'group'   => [
106
                'title' => $lang['strgroup'],
107
                'field' => Decorator::field('groname'),
108
                'url'   => "groups.php?action=properties&amp;{$this->misc->href}&amp;",
109
                'vars'  => ['group' => 'groname'],
110
            ],
111
            'actions' => [
112
                'title' => $lang['stractions'],
113
            ],
114
        ];
115
116
        $actions = [
117
            'drop' => [
118
                'content' => $lang['strdrop'],
119
                'attr'    => [
120
                    'href' => [
121
                        'url'     => 'groups.php',
122
                        'urlvars' => [
123
                            'action' => 'confirm_drop',
124
                            'group'  => Decorator::field('groname'),
125
                        ],
126
                    ],
127
                ],
128
            ],
129
        ];
130
131
        echo $this->printTable($groups, $columns, $actions, 'groups-properties', $lang['strnogroups']);
132
133
        $this->printNavLinks(['create' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
134
            'attr'    => [
135
                'href' => [
136
                    'url'     => 'groups.php',
137
                    'urlvars' => [
138
                        'action' => 'create',
139
                        'server' => $_REQUEST['server'],
140
                    ],
141
                ],
142
            ],
143
            'content' => $lang['strcreategroup'],
144
        ]], 'groups-groups', get_defined_vars());
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...
145
    }
146
147
    /**
148
     * Add user to a group.
149
     */
150
    public function doAddMember()
151
    {
152
        $lang = $this->lang;
153
        $data = $this->misc->getDatabaseAccessor();
154
155
        $status = $data->addGroupMember($_REQUEST['group'], $_REQUEST['user']);
156
        if (0 == $status) {
157
            $this->doProperties($lang['strmemberadded']);
158
        } else {
159
            $this->doProperties($lang['strmemberaddedbad']);
160
        }
161
    }
162
163
    /**
164
     * Show confirmation of drop user from group and perform actual drop.
165
     *
166
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
167
     */
168
    public function doDropMember($confirm)
169
    {
170
        $lang = $this->lang;
171
        $data = $this->misc->getDatabaseAccessor();
172
173
        if ($confirm) {
174
            $this->printTrail('group');
175
            $this->printTitle($lang['strdropmember'], 'pg.group.alter');
176
177
            echo '<p>', sprintf($lang['strconfdropmember'], $this->misc->printVal($_REQUEST['user']), $this->misc->printVal($_REQUEST['group'])), "</p>\n";
178
179
            echo '<form action="' . \SUBFOLDER . "/src/views/groups.php\" method=\"post\">\n";
180
            echo $this->misc->form;
181
            echo "<input type=\"hidden\" name=\"action\" value=\"drop_member\" />\n";
182
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
183
            echo '<input type="hidden" name="user" value="', htmlspecialchars($_REQUEST['user']), "\" />\n";
184
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
185
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
186
            echo "</form>\n";
187
        } else {
188
            $status = $data->dropGroupMember($_REQUEST['group'], $_REQUEST['user']);
189
            if (0 == $status) {
190
                $this->doProperties($lang['strmemberdropped']);
191
            } else {
192
                $this->doDropMember(true, $lang['strmemberdroppedbad']);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\Controller\Gr...troller::doDropMember() has too many arguments starting with $lang['strmemberdroppedbad']. ( Ignorable by Annotation )

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

192
                $this->/** @scrutinizer ignore-call */ 
193
                       doDropMember(true, $lang['strmemberdroppedbad']);

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...
193
            }
194
        }
195
    }
196
197
    /**
198
     * Show read only properties for a group.
199
     *
200
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
201
     */
202
    public function doProperties($msg = '')
203
    {
204
        $lang = $this->lang;
205
        $data = $this->misc->getDatabaseAccessor();
206
207
        if (!isset($_POST['user'])) {
208
            $_POST['user'] = '';
209
        }
210
211
        $this->printTrail('group');
212
        $this->printTitle($lang['strproperties'], 'pg.group');
213
        $this->printMsg($msg);
214
215
        $groupdata = $data->getGroup($_REQUEST['group']);
216
        $users     = $data->getUsers();
217
218
        if ($groupdata->recordCount() > 0) {
219
            $columns = [
220
                'members' => [
221
                    'title' => $lang['strmembers'],
222
                    'field' => Decorator::field('usename'),
223
                ],
224
                'actions' => [
225
                    'title' => $lang['stractions'],
226
                ],
227
            ];
228
229
            $actions = [
230
                'drop' => [
231
                    'content' => $lang['strdrop'],
232
                    'attr'    => [
233
                        'href' => [
234
                            'url'     => 'groups.php',
235
                            'urlvars' => [
236
                                'action' => 'confirm_drop_member',
237
                                'group'  => $_REQUEST['group'],
238
                                'user'   => Decorator::field('usename'),
239
                            ],
240
                        ],
241
                    ],
242
                ],
243
            ];
244
245
            echo $this->printTable($groupdata, $columns, $actions, 'groups-members', $lang['strnousers']);
246
        }
247
248
        // Display form for adding a user to the group
249
        echo '<form action="' . \SUBFOLDER . "/src/views/groups.php\" method=\"post\">\n";
250
        echo '<select name="user">';
251
        while (!$users->EOF) {
252
            $uname = $this->misc->printVal($users->fields['usename']);
253
            echo "<option value=\"{$uname}\"",
254
            ($uname == $_POST['user']) ? ' selected="selected"' : '', ">{$uname}</option>\n";
255
            $users->moveNext();
256
        }
257
        echo "</select>\n";
258
        echo "<input type=\"submit\" value=\"{$lang['straddmember']}\" />\n";
259
        echo $this->misc->form;
260
        echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
261
        echo "<input type=\"hidden\" name=\"action\" value=\"add_member\" />\n";
262
        echo "</form>\n";
263
264
        $this->printNavLinks(['showall' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
265
            'attr'    => [
266
                'href' => [
267
                    'url'     => 'groups.php',
268
                    'urlvars' => [
269
                        'server' => $_REQUEST['server'],
270
                    ],
271
                ],
272
            ],
273
            'content' => $lang['strshowallgroups'],
274
        ]], 'groups-properties', get_defined_vars());
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...
275
    }
276
277
    /**
278
     * Show confirmation of drop and perform actual drop.
279
     *
280
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
281
     */
282
    public function doDrop($confirm)
283
    {
284
        $lang = $this->lang;
285
        $data = $this->misc->getDatabaseAccessor();
286
287
        if ($confirm) {
288
            $this->printTrail('group');
289
            $this->printTitle($lang['strdrop'], 'pg.group.drop');
290
291
            echo '<p>', sprintf($lang['strconfdropgroup'], $this->misc->printVal($_REQUEST['group'])), "</p>\n";
292
293
            echo '<form action="' . \SUBFOLDER . "/src/views/groups.php\" method=\"post\">\n";
294
            echo $this->misc->form;
295
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
296
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
297
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
298
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
299
            echo "</form>\n";
300
        } else {
301
            $status = $data->dropGroup($_REQUEST['group']);
302
            if (0 == $status) {
303
                $this->doDefault($lang['strgroupdropped']);
304
            } else {
305
                $this->doDefault($lang['strgroupdroppedbad']);
306
            }
307
        }
308
    }
309
310
    /**
311
     * Displays a screen where they can enter a new group.
312
     *
313
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
314
     */
315
    public function doCreate($msg = '')
316
    {
317
        $lang = $this->lang;
318
        $data = $this->misc->getDatabaseAccessor();
319
        if (!isset($_POST['name'])) {
320
            $_POST['name'] = '';
321
        }
322
323
        if (!isset($_POST['members'])) {
324
            $_POST['members'] = [];
325
        }
326
327
        // Fetch a list of all users in the cluster
328
        $users = $data->getUsers();
329
330
        $this->printTrail('server');
331
        $this->printTitle($lang['strcreategroup'], 'pg.group.create');
332
        $this->printMsg($msg);
333
334
        echo "<form action=\"\" method=\"post\">\n";
335
        echo $this->misc->form;
336
        echo "<table>\n";
337
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
338
        echo "\t\t<td class=\"data\"><input size=\"32\" maxlength=\"{$data->_maxNameLen}\" name=\"name\" value=\"", htmlspecialchars($_POST['name']), "\" /></td>\n\t</tr>\n";
339
        if ($users->recordCount() > 0) {
340
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strmembers']}</th>\n";
341
342
            echo "\t\t<td class=\"data\">\n";
343
            echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(40, $users->recordCount()), "\">\n";
344
            while (!$users->EOF) {
345
                $username = $users->fields['usename'];
346
                echo "\t\t\t\t<option value=\"{$username}\"",
347
                (in_array($username, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($username), "</option>\n";
348
                $users->moveNext();
349
            }
350
            echo "\t\t\t</select>\n";
351
            echo "\t\t</td>\n\t</tr>\n";
352
        }
353
        echo "</table>\n";
354
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
355
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
356
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
357
        echo "</form>\n";
358
    }
359
360
    /**
361
     * Actually creates the new group in the database.
362
     */
363
    public function doSaveCreate()
364
    {
365
        $lang = $this->lang;
366
        $data = $this->misc->getDatabaseAccessor();
367
368
        if (!isset($_POST['members'])) {
369
            $_POST['members'] = [];
370
        }
371
372
        // Check form vars
373
        if ('' == trim($_POST['name'])) {
374
            $this->doCreate($lang['strgroupneedsname']);
375
        } else {
376
            $status = $data->createGroup($_POST['name'], $_POST['members']);
377
            if (0 == $status) {
378
                $this->doDefault($lang['strgroupcreated']);
379
            } else {
380
                $this->doCreate($lang['strgroupcreatedbad']);
381
            }
382
        }
383
    }
384
}
385