Test Failed
Push — master ( 34dd7c...325a03 )
by Felipe
11:17 queued 06:14
created

ServersTrait   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 30
dl 0
loc 157
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C printConnection() 0 75 16
C getServers() 0 65 14
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.43
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Common trait for listing servers.
13
 */
14
trait ServersTrait
15
{
16
    /**
17
     * Get list of servers.
18
     *
19
     * @param bool  $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array
20
     * @param mixed $group     a group name to filter the returned servers using $this->conf[srv_groups]
21
     *
22
     * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable
23
     */
24
    public function getServers($recordset = false, $group = false)
25
    {
26
        $logins = isset($_SESSION['webdbLogin']) && is_array($_SESSION['webdbLogin']) ? $_SESSION['webdbLogin'] : [];
27
        $srvs   = [];
28
29
        if (($group !== false) && ($group !== 'all')) {
30
            if (isset($this->conf['srv_groups'][$group]['servers'])) {
31
                $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...
32
                    '/\s/',
33
                    '',
34
                    $this->conf['srv_groups'][$group]['servers']
35
                )), 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...
36
            } else {
37
                $group = '';
38
            }
39
        }
40
41
        foreach ($this->conf['servers'] as $idx => $info) {
42
            $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...
43
            if ($group === false || isset($group[$idx]) || ($group === 'all')) {
44
                $server_id  = $info['host'] . ':' . $info['port'] . ':' . $info['sslmode'];
45
                $server_sha = sha1($server_id);
46
47
                if (isset($logins[$server_sha])) {
48
                    $srvs[$server_sha] = $logins[$server_sha];
49
                } elseif (isset($logins[$server_id])) {
50
                    $srvs[$server_sha] = $logins[$server_id];
51
                } else {
52
                    $srvs[$server_sha] = $info;
53
                }
54
55
                $srvs[$server_sha]['id']     = $server_id;
56
                $srvs[$server_sha]['sha']    = $server_sha;
57
                $srvs[$server_sha]['action'] = Decorator::url(
58
                    '/redirect/server',
59
                    [
60
                        'server' => Decorator::field('sha'),
61
                    ]
62
                );
63
                if (isset($srvs[$server_sha]['username'])) {
64
                    $srvs[$server_sha]['icon']   = 'Server';
65
                    $srvs[$server_sha]['branch'] = Decorator::url(
66
                        '/src/views/alldb',
67
                        [
68
                            'action'  => 'tree',
69
                            'subject' => 'server',
70
                            'server'  => Decorator::field('sha'),
71
                        ]
72
                    );
73
                } else {
74
                    $srvs[$server_sha]['icon']   = 'DisconnectedServer';
75
                    $srvs[$server_sha]['branch'] = false;
76
                }
77
            }
78
        }
79
80
        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...
81
            return strcmp($a['desc'], $b['desc']);
82
        });
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...
83
84
        if ($recordset) {
85
            return new \PHPPgAdmin\ArrayRecordSet($srvs);
86
        }
87
88
        return $srvs;
89
    }
90
91
    /*
92
     * Output dropdown list to select server and
93
     * databases form the popups windows.
94
     * @param string $the_action an action identifying the purpose of this snipper sql|find|history
95
     */
96
    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...
97
    {
98
        $connection_html = "<table class=\"printconnection\" style=\"width: 100%\"><tr><td class=\"popup_select1\">\n";
99
100
        $conf_servers = $this->getServers();
101
        $server_id    = $this->misc->getServerId();
102
        $servers      = [];
103
        foreach ($conf_servers as $key => $info) {
104
            if (empty($info['username'])) {
105
                continue;
106
            }
107
            $info['selected'] = '';
108
            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

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