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

ServersTrait::getServers()   C

Complexity

Conditions 14
Paths 192

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 5.4422
c 0
b 0
f 0
cc 14
eloc 41
nc 192
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
 * Common trait for listing servers
13
 */
14
trait ServersTrait
15
{
16
17
    /**
18
     * Get list of servers.
19
     *
20
     * @param bool  $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array
21
     * @param mixed $group     a group name to filter the returned servers using $this->conf[srv_groups]
22
     *
23
     * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable
24
     */
25
    public function getServers($recordset = false, $group = false)
26
    {
27
        $logins = isset($_SESSION['webdbLogin']) && is_array($_SESSION['webdbLogin']) ? $_SESSION['webdbLogin'] : [];
28
        $srvs   = [];
29
30
        if (($group !== false) && ($group !== 'all')) {
31
            if (isset($this->conf['srv_groups'][$group]['servers'])) {
32
                $group = array_fill_keys(explode(',', preg_replace(
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...
33
                    '/\s/',
34
                    '',
35
                    $this->conf['srv_groups'][$group]['servers']
36
                )), 1);
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...
37
            } else {
38
                $group = '';
39
            }
40
        }
41
42
        foreach ($this->conf['servers'] as $idx => $info) {
43
            $server_id = $info['host'] . ':' . $info['port'] . ':' . $info['sslmode'];
1 ignored issue
show
Unused Code introduced by
The assignment to $server_id is dead and can be removed.
Loading history...
44
            if ($group === false || isset($group[$idx]) || ($group === 'all')) {
45
                $server_id  = $info['host'] . ':' . $info['port'] . ':' . $info['sslmode'];
46
                $server_sha = sha1($server_id);
47
48
                if (isset($logins[$server_sha])) {
49
                    $srvs[$server_sha] = $logins[$server_sha];
50
                } else if (isset($logins[$server_id])) {
51
                    $srvs[$server_sha] = $logins[$server_id];
52
                } else {
53
                    $srvs[$server_sha] = $info;
54
                }
55
56
                $srvs[$server_sha]['id']     = $server_id;
57
                $srvs[$server_sha]['sha']    = $server_sha;
58
                $srvs[$server_sha]['action'] = Decorator::url(
59
                    '/redirect/server',
60
                    [
61
                        'server' => Decorator::field('sha'),
62
                    ]
63
                );
64
                if (isset($srvs[$server_sha]['username'])) {
65
                    $srvs[$server_sha]['icon']   = 'Server';
66
                    $srvs[$server_sha]['branch'] = Decorator::url(
67
                        '/src/views/alldb',
68
                        [
69
                            'action'  => 'tree',
70
                            'subject' => 'server',
71
                            'server'  => Decorator::field('sha'),
72
                        ]
73
                    );
74
                } else {
75
                    $srvs[$server_sha]['icon']   = 'DisconnectedServer';
76
                    $srvs[$server_sha]['branch'] = false;
77
                }
78
            }
79
        }
80
81
        uasort($srvs, function ($a, $b) {
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...
82
            return strcmp($a['desc'], $b['desc']);
83
        });
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...
84
85
        if ($recordset) {
86
            return new \PHPPgAdmin\ArrayRecordSet($srvs);
87
        }
88
89
        return $srvs;
90
    }
91
92
    /*
93
     * Output dropdown list to select server and
94
     * databases form the popups windows.
95
     * @param string $the_action an action identifying the purpose of this snipper sql|find|history
96
     */
97
    public function printConnection($the_action, $do_print = true)
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
98
    {
99
        $lang = $this->lang;
100
101
        $connection_html = "<table class=\"printconnection\" style=\"width: 100%\"><tr><td class=\"popup_select1\">\n";
102
103
        $conf_servers = $this->getServers();
104
        $server_id    = $this->misc->getServerId();
105
        $servers      = [];
106
        foreach ($conf_servers as $key => $info) {
107
            if (empty($info['username'])) {
108
                continue;
109
            }
110
            $info['selected'] = '';
111
            if ($this->getRequestParam('server') === $info['id'] || $this->getRequestParam('server') === $key) {
1 ignored issue
show
Bug introduced by
It seems like getRequestParam() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
            if ($this->/** @scrutinizer ignore-call */ getRequestParam('server') === $info['id'] || $this->getRequestParam('server') === $key) {
Loading history...
112
                $info['selected'] = ' selected="selected" ';
113
            }
114
            $servers[$key] = $info;
115
        }
116
        $connection_html .= '<input type="hidden" readonly="readonly" value="' . $the_action . '" id="the_action">';
117
118
        if (count($servers) === 1) {
119
            $connection_html .= '<input type="hidden" readonly="readonly" value="' . $server_id . '" name="server">';
120
        } else {
121
            $connection_html .= '<label>';
122
            $connection_html .= $this->misc->printHelp($lang['strserver'], 'pg.server', false);
123
            $connection_html .= ': </label>';
124
            $connection_html .= " <select name=\"server\" id='selectserver' >\n";
125
            foreach ($servers as $id => $server) {
126
127
                $connection_html .= '<option value="' . $id . '" ' . $server['selected'] . '>';
128
                $connection_html .= htmlspecialchars("{$server['desc']} ({$server['id']})");
129
                $connection_html .= "</option>\n";
130
            }
131
            $connection_html .= "</select>\n";
132
        }
133
134
        $connection_html .= "</td><td class=\"popup_select2\" style=\"text-align: right\">\n";
135
136
        if (count($servers) === 1 &&
137
            isset($servers[$server_id]['useonlydefaultdb']) &&
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
138
            $servers[$server_id]['useonlydefaultdb'] === true
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
139
        ) {
140
            $connection_html .= '<input type="hidden" name="database" value="' . htmlspecialchars($servers[$server_id]['defaultdb']) . "\" />\n";
141
        } else {
142
            // Get the list of all databases
143
            $data      = $this->misc->getDatabaseAccessor();
144
            $databases = $data->getDatabases();
145
            if ($databases->recordCount() > 0) {
146
                $connection_html .= '<label>';
147
                $connection_html .= $this->misc->printHelp($lang['strdatabase'], 'pg.database', false);
148
                $connection_html .= ": <select  id='selectdb'  name=\"database\" >\n";
149
150
                //if no database was selected, user should select one
151
                if (!isset($_REQUEST['database'])) {
152
                    $connection_html .= "<option value=\"\">--</option>\n";
153
                }
154
155
                while (!$databases->EOF) {
156
                    $dbname     = $databases->fields['datname'];
157
                    $dbselected = isset($_REQUEST['database']) && $dbname == $_REQUEST['database'] ? ' selected="selected"' : '';
158
                    $connection_html .= '<option value="' . htmlspecialchars($dbname) . '" ' . $dbselected . '>' . htmlspecialchars($dbname) . "</option>\n";
159
160
                    $databases->moveNext();
161
                }
162
                $connection_html .= "</select></label>\n";
163
            } else {
164
                $server_info = $this->misc->getServerInfo();
165
                $connection_html .= '<input type="hidden" name="database" value="' . htmlspecialchars($server_info['defaultdb']) . "\" />\n";
166
            }
167
        }
168
169
        $connection_html .= "</td></tr></table>\n";
170
171
        if ($do_print) {
172
            echo $connection_html;
173
        } else {
174
            return $connection_html;
175
        }
176
    }
177
}
178