Test Failed
Branch develop (db5506)
by Felipe
03:46
created

AdminTrait::adminActions()   D

Complexity

Conditions 30
Paths 72

Size

Total Lines 105
Code Lines 76

Duplication

Lines 52
Ratio 49.52 %

Importance

Changes 0
Metric Value
cc 30
eloc 76
nc 72
nop 2
dl 52
loc 105
rs 4.425
c 0
b 0
f 0

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
namespace PHPPgAdmin\Controller;
3
4
use \PHPPgAdmin\Decorators\Decorator;
5
6
trait AdminTrait
7
{
8
9
/**
10
 * Show confirmation of cluster and perform cluster
11
 */
12
    public function doCluster($type, $confirm = false)
0 ignored issues
show
Coding Style introduced by
doCluster uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
13
    {
14
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
16
        $script = $this->script;
17
        $misc   = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
18
        $lang   = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
19
        $data   = $misc->getDatabaseAccessor();
20
21 View Code Duplication
        if (($type == 'table') && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $this->doDefault($lang['strspecifytabletocluster']);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

22
            $this->/** @scrutinizer ignore-call */ doDefault($lang['strspecifytabletocluster']);
Loading history...
23
            return;
24
        }
25
26
        if ($confirm) {
27 View Code Duplication
            if (isset($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
                $this->printTrail('schema');
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

28
                $this->/** @scrutinizer ignore-call */ printTrail('schema');
Loading history...
29
                $this->printTitle($lang['strclusterindex'], 'pg.index.cluster');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

29
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strclusterindex'], 'pg.index.cluster');
Loading history...
30
31
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
32
                foreach ($_REQUEST['ma'] as $v) {
33
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
0 ignored issues
show
Security introduced by
htmlspecialchars_decode(...\Controller\ENT_QUOTES) can contain request data and is used in unserialized context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and $_REQUEST['ma'] is assigned to $v
    in src/controllers/AdminTrait.php on line 32
  2. Data is passed through htmlspecialchars_decode()
    in src/controllers/AdminTrait.php on line 33

Preventing Object Injection Attacks

If you pass raw user-data to unserialize() for example, this can be used to create an object of any class that is available in your local filesystem. For an attacker, classes that have magic methods like __destruct or __wakeup are particularly interesting in such a case, as they can be exploited very easily.

We recommend to not pass user data to such a function. In case of unserialize, better use JSON to transfer data.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
34
                    echo '<p>', sprintf($lang['strconfclustertable'], $misc->printVal($a['table'])), "</p>\n";
35
                    echo '<input type="hidden" name="table[]" value="', htmlspecialchars($a['table']), "\" />\n";
36
                }
37
            } // END if multi cluster
38
            else {
39
                $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

39
                $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
40
                $this->printTitle($lang['strclusterindex'], 'pg.index.cluster');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

40
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strclusterindex'], 'pg.index.cluster');
Loading history...
41
42
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
43
44
                if ($type == 'table') {
45
                    echo '<p>', sprintf($lang['strconfclustertable'], $misc->printVal($_REQUEST['object'])), "</p>\n";
46
                    echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
47
                } else {
48
                    echo '<p>', sprintf($lang['strconfclusterdatabase'], $misc->printVal($_REQUEST['object'])), "</p>\n";
49
                    echo "<input type=\"hidden\" name=\"table\" value=\"\" />\n";
50
                }
51
            }
52
            echo "<input type=\"hidden\" name=\"action\" value=\"cluster\" />\n";
53
54
            echo $misc->form;
55
56
            echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strcluster']}\" />\n"; //TODO
57
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
58
            echo "</form>\n";
59
        } // END single cluster
60
        else {
61
            //If multi table cluster
62
            if ($type == 'table') {
63
                // cluster one or more table
64
                if (is_array($_REQUEST['table'])) {
65
                    $msg = '';
66
                    foreach ($_REQUEST['table'] as $o) {
67
                        $status = $data->clusterIndex($o);
68
                        if ($status == 0) {
69
                            $msg .= sprintf('%s: %s<br />', htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strclusteredgood']);
70
                        } else {
71
                            $this->doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strclusteredbad']));
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

71
                            $this->/** @scrutinizer ignore-call */ doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strclusteredbad']));
Loading history...
72
                            return;
73
                        }
74
                    }
75
                    // Everything went fine, back to the Default page....
76
                    $this->doDefault($msg);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

76
                    $this->/** @scrutinizer ignore-call */ doDefault($msg);
Loading history...
77 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
                    $status = $data->clusterIndex($_REQUEST['object']);
79
                    if ($status == 0) {
80
                        $this->doAdmin($type, $lang['strclusteredgood']);
81
                    } else {
82
                        $this->doAdmin($type, $lang['strclusteredbad']);
83
                    }
84
                }
85 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                // Cluster all tables in database
87
                $status = $data->clusterIndex();
88
                if ($status == 0) {
89
                    $this->doAdmin($type, $lang['strclusteredgood']);
90
                } else {
91
                    $this->doAdmin($type, $lang['strclusteredbad']);
92
                }
93
            }
94
        }
95
    }
96
97
    /**
98
     * Show confirmation of reindex and perform reindex
99
     */
100
    public function doReindex($type, $confirm = false)
0 ignored issues
show
Coding Style introduced by
doReindex uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
101
    {
102
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
103
        $script       = $this->script;
104
        $misc         = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
105
        $lang         = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
106
        $data         = $misc->getDatabaseAccessor();
107
108 View Code Duplication
        if (($type == 'table') && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $this->doDefault($lang['strspecifytabletoreindex']);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

109
            $this->/** @scrutinizer ignore-call */ doDefault($lang['strspecifytabletoreindex']);
Loading history...
110
            return;
111
        }
112
113
        if ($confirm) {
114 View Code Duplication
            if (isset($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
                $this->printTrail('schema');
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

115
                $this->/** @scrutinizer ignore-call */ printTrail('schema');
Loading history...
116
                $this->printTitle($lang['strreindex'], 'pg.reindex');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

116
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strreindex'], 'pg.reindex');
Loading history...
117
118
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
119
                foreach ($_REQUEST['ma'] as $v) {
120
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
0 ignored issues
show
Security introduced by
htmlspecialchars_decode(...\Controller\ENT_QUOTES) can contain request data and is used in unserialized context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and $_REQUEST['ma'] is assigned to $v
    in src/controllers/AdminTrait.php on line 119
  2. Data is passed through htmlspecialchars_decode()
    in src/controllers/AdminTrait.php on line 120

Preventing Object Injection Attacks

If you pass raw user-data to unserialize() for example, this can be used to create an object of any class that is available in your local filesystem. For an attacker, classes that have magic methods like __destruct or __wakeup are particularly interesting in such a case, as they can be exploited very easily.

We recommend to not pass user data to such a function. In case of unserialize, better use JSON to transfer data.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
121
                    echo '<p>', sprintf($lang['strconfreindextable'], $misc->printVal($a['table'])), "</p>\n";
122
                    echo '<input type="hidden" name="table[]" value="', htmlspecialchars($a['table']), "\" />\n";
123
                }
124
            } // END if multi reindex
125
            else {
126
                $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

126
                $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
127
                $this->printTitle($lang['strreindex'], 'pg.reindex');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

127
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strreindex'], 'pg.reindex');
Loading history...
128
129
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
130
131
                if ($type == 'table') {
132
                    echo '<p>', sprintf($lang['strconfreindextable'], $misc->printVal($_REQUEST['object'])), "</p>\n";
133
                    echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
134
                } else {
135
                    echo '<p>', sprintf($lang['strconfreindexdatabase'], $misc->printVal($_REQUEST['object'])), "</p>\n";
136
                    echo "<input type=\"hidden\" name=\"table\" value=\"\" />\n";
137
                }
138
            }
139
            echo "<input type=\"hidden\" name=\"action\" value=\"reindex\" />\n";
140
141
            if ($data->hasForceReindex()) {
142
                echo "<p><input type=\"checkbox\" id=\"reindex_force\" name=\"reindex_force\" /><label for=\"reindex_force\">{$lang['strforce']}</label></p>\n";
143
            }
144
145
            echo $misc->form;
146
147
            echo "<input type=\"submit\" name=\"reindex\" value=\"{$lang['strreindex']}\" />\n"; //TODO
148
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
149
            echo "</form>\n";
150
        } // END single reindex
151
        else {
152
            //If multi table reindex
153
            if (($type == 'table') && is_array($_REQUEST['table'])) {
154
                $msg = '';
155
                foreach ($_REQUEST['table'] as $o) {
156
                    $status = $data->reindex(strtoupper($type), $o, isset($_REQUEST['reindex_force']));
157
                    if ($status == 0) {
158
                        $msg .= sprintf('%s: %s<br />', htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strreindexgood']);
159
                    } else {
160
                        $this->doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strreindexbad']));
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

160
                        $this->/** @scrutinizer ignore-call */ doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['strreindexbad']));
Loading history...
161
                        return;
162
                    }
163
                }
164
                // Everything went fine, back to the Default page....
165
                $misc->setReloadBrowser(true);
166
                $this->doDefault($msg);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

166
                $this->/** @scrutinizer ignore-call */ doDefault($msg);
Loading history...
167
            } else {
168
                $status = $data->reindex(strtoupper($type), $_REQUEST['object'], isset($_REQUEST['reindex_force']));
169 View Code Duplication
                if ($status == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
                    $misc->setReloadBrowser(true);
171
                    $this->doAdmin($type, $lang['strreindexgood']);
172
                } else {
173
                    $this->doAdmin($type, $lang['strreindexbad']);
174
                }
175
            }
176
        }
177
    }
178
179
    /**
180
     * Show confirmation of analyze and perform analyze
181
     */
182
    public function doAnalyze($type, $confirm = false)
0 ignored issues
show
Coding Style introduced by
doAnalyze uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
183
    {
184
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
185
186
        $script = $this->script;
187
        $misc   = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
188
        $lang   = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
189
        $data   = $misc->getDatabaseAccessor();
190
191 View Code Duplication
        if (($type == 'table') && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
            $this->doDefault($lang['strspecifytabletoanalyze']);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

192
            $this->/** @scrutinizer ignore-call */ doDefault($lang['strspecifytabletoanalyze']);
Loading history...
193
            return;
194
        }
195
196
        if ($confirm) {
197 View Code Duplication
            if (isset($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
                $this->printTrail('schema');
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

198
                $this->/** @scrutinizer ignore-call */ printTrail('schema');
Loading history...
199
                $this->printTitle($lang['stranalyze'], 'pg.analyze');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

199
                $this->/** @scrutinizer ignore-call */ printTitle($lang['stranalyze'], 'pg.analyze');
Loading history...
200
201
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
202
                foreach ($_REQUEST['ma'] as $v) {
203
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
0 ignored issues
show
Security introduced by
htmlspecialchars_decode(...\Controller\ENT_QUOTES) can contain request data and is used in unserialized context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and $_REQUEST['ma'] is assigned to $v
    in src/controllers/AdminTrait.php on line 202
  2. Data is passed through htmlspecialchars_decode()
    in src/controllers/AdminTrait.php on line 203

Preventing Object Injection Attacks

If you pass raw user-data to unserialize() for example, this can be used to create an object of any class that is available in your local filesystem. For an attacker, classes that have magic methods like __destruct or __wakeup are particularly interesting in such a case, as they can be exploited very easily.

We recommend to not pass user data to such a function. In case of unserialize, better use JSON to transfer data.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
204
                    \Kint::dump($a);
205
                    echo '<p>', sprintf($lang['strconfanalyzetable'], $misc->printVal($a['table'])), "</p>\n";
206
                    echo '<input type="hidden" name="table[]" value="', htmlspecialchars($a['table']), "\" />\n";
207
                }
208
            } // END if multi analyze
209
            else {
210
                $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

210
                $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
211
                $this->printTitle($lang['stranalyze'], 'pg.analyze');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

211
                $this->/** @scrutinizer ignore-call */ printTitle($lang['stranalyze'], 'pg.analyze');
Loading history...
212
213
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
214
215
                if ($type == 'table') {
216
                    echo '<p>', sprintf($lang['strconfanalyzetable'], $misc->printVal($_REQUEST['object'])), "</p>\n";
217
                    echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
218
                } else {
219
                    echo '<p>', sprintf($lang['strconfanalyzedatabase'], $misc->printVal($_REQUEST['object'])), "</p>\n";
220
                    echo "<input type=\"hidden\" name=\"table\" value=\"\" />\n";
221
                }
222
            }
223
            echo "<input type=\"hidden\" name=\"action\" value=\"analyze\" />\n";
224
            echo $misc->form;
225
226
            echo "<input type=\"submit\" name=\"analyze\" value=\"{$lang['stranalyze']}\" />\n"; //TODO
227
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
228
            echo "</form>\n";
229
        } // END single analyze
230
        else {
231
            //If multi table analyze
232
            if (($type == 'table') && is_array($_REQUEST['table'])) {
233
                $msg = '';
234
                foreach ($_REQUEST['table'] as $o) {
235
                    $status = $data->analyzeDB($o);
236
                    if ($status == 0) {
237
                        $msg .= sprintf('%s: %s<br />', htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['stranalyzegood']);
238
                    } else {
239
                        $this->doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['stranalyzebad']));
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

239
                        $this->/** @scrutinizer ignore-call */ doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($o, ENT_QUOTES, 'UTF-8'), $lang['stranalyzebad']));
Loading history...
240
                        return;
241
                    }
242
                }
243
                // Everything went fine, back to the Default page....
244
                $misc->setReloadBrowser(true);
245
                $this->doDefault($msg);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

245
                $this->/** @scrutinizer ignore-call */ doDefault($msg);
Loading history...
246
            } else {
247
                //we must pass table here. When empty, analyze the whole db
248
                $status = $data->analyzeDB($_REQUEST['table']);
249 View Code Duplication
                if ($status == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
                    $misc->setReloadBrowser(true);
251
                    $this->doAdmin($type, $lang['stranalyzegood']);
252
                } else {
253
                    $this->doAdmin($type, $lang['stranalyzebad']);
254
                }
255
            }
256
        }
257
    }
258
259
    /**
260
     * Show confirmation of vacuum and perform actual vacuum
261
     */
262
    public function doVacuum($type, $confirm = false)
0 ignored issues
show
Coding Style introduced by
doVacuum uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
263
    {
264
        $script = ($type == 'database') ? 'database.php' : 'tables.php';
265
266
        $misc = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
267
        $lang = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
268
        $data = $misc->getDatabaseAccessor();
269
270 View Code Duplication
        if (($type == 'table') && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
            $this->doDefault($lang['strspecifytabletovacuum']);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

271
            $this->/** @scrutinizer ignore-call */ doDefault($lang['strspecifytabletovacuum']);
Loading history...
272
            return;
273
        }
274
275
        if ($confirm) {
276 View Code Duplication
            if (isset($_REQUEST['ma'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
277
                $this->printTrail('schema');
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

277
                $this->/** @scrutinizer ignore-call */ printTrail('schema');
Loading history...
278
                $this->printTitle($lang['strvacuum'], 'pg.vacuum');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

278
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strvacuum'], 'pg.vacuum');
Loading history...
279
280
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
281
                foreach ($_REQUEST['ma'] as $v) {
282
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
0 ignored issues
show
Security introduced by
htmlspecialchars_decode(...\Controller\ENT_QUOTES) can contain request data and is used in unserialized context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and $_REQUEST['ma'] is assigned to $v
    in src/controllers/AdminTrait.php on line 281
  2. Data is passed through htmlspecialchars_decode()
    in src/controllers/AdminTrait.php on line 282

Preventing Object Injection Attacks

If you pass raw user-data to unserialize() for example, this can be used to create an object of any class that is available in your local filesystem. For an attacker, classes that have magic methods like __destruct or __wakeup are particularly interesting in such a case, as they can be exploited very easily.

We recommend to not pass user data to such a function. In case of unserialize, better use JSON to transfer data.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
283
                    echo '<p>', sprintf($lang['strconfvacuumtable'], $misc->printVal($a['table'])), "</p>\n";
284
                    echo '<input type="hidden" name="table[]" value="', htmlspecialchars($a['table']), "\" />\n";
285
                }
286
            } else {
287
                // END if multi vacuum
288
                $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

288
                $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
289
                $this->printTitle($lang['strvacuum'], 'pg.vacuum');
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

289
                $this->/** @scrutinizer ignore-call */ printTitle($lang['strvacuum'], 'pg.vacuum');
Loading history...
290
291
                echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
292
293
                if ($type == 'table') {
294
                    echo '<p>', sprintf($lang['strconfvacuumtable'], $misc->printVal($_REQUEST['object'])), "</p>\n";
295
                    echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
296
                } else {
297
                    echo '<p>', sprintf($lang['strconfvacuumdatabase'], $misc->printVal($_REQUEST['object'])), "</p>\n";
298
                    echo "<input type=\"hidden\" name=\"table\" value=\"\" />\n";
299
                }
300
            }
301
            echo "<input type=\"hidden\" name=\"action\" value=\"vacuum\" />\n";
302
            echo $misc->form;
303
            echo "<p><input type=\"checkbox\" id=\"vacuum_full\" name=\"vacuum_full\" /> <label for=\"vacuum_full\">{$lang['strfull']}</label></p>\n";
304
            echo "<p><input type=\"checkbox\" id=\"vacuum_analyze\" name=\"vacuum_analyze\" /> <label for=\"vacuum_analyze\">{$lang['stranalyze']}</label></p>\n";
305
            echo "<p><input type=\"checkbox\" id=\"vacuum_freeze\" name=\"vacuum_freeze\" /> <label for=\"vacuum_freeze\">{$lang['strfreeze']}</label></p>\n";
306
            echo "<input type=\"submit\" name=\"vacuum\" value=\"{$lang['strvacuum']}\" />\n";
307
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
308
            echo "</form>\n";
309
        } // END single vacuum
310
        else {
311
            //If multi drop
312
            if (is_array($_REQUEST['table'])) {
313
                $msg = '';
314
                foreach ($_REQUEST['table'] as $t) {
315
                    $status = $data->vacuumDB($t, isset($_REQUEST['vacuum_analyze']), isset($_REQUEST['vacuum_full']), isset($_REQUEST['vacuum_freeze']));
316
                    if ($status == 0) {
317
                        $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strvacuumgood']);
318
                    } else {
319
                        $this->doDefault($type, sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strvacuumbad']));
320
                        return;
321
                    }
322
                }
323
                // Everything went fine, back to the Default page....
324
                $misc->setReloadBrowser(true);
325
                $this->doDefault($msg);
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

325
                $this->/** @scrutinizer ignore-call */ doDefault($msg);
Loading history...
326
            } else {
327
                //we must pass table here. When empty, vacuum the whole db
328
                $status = $data->vacuumDB($_REQUEST['table'], isset($_REQUEST['vacuum_analyze']), isset($_REQUEST['vacuum_full']), isset($_REQUEST['vacuum_freeze']));
329 View Code Duplication
                if ($status == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
330
                    $misc->setReloadBrowser(true);
331
                    $this->doAdmin($type, $lang['strvacuumgood']);
332
                } else {
333
                    $this->doAdmin($type, $lang['strvacuumbad']);
334
                }
335
            }
336
        }
337
    }
338
339
    /**
340
     * Add or Edit autovacuum params and save them
341
     */
342
    public function doEditAutovacuum($type, $confirm, $msg = '')
0 ignored issues
show
Coding Style introduced by
doEditAutovacuum uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doEditAutovacuum uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
343
    {
344
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
345
        $script       = $this->script;
0 ignored issues
show
Unused Code introduced by
The assignment to $script is dead and can be removed.
Loading history...
346
347
        $misc = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
348
        $lang = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
349
        $data = $misc->getDatabaseAccessor();
350
351
        if (empty($_REQUEST['table'])) {
352
            $this->doAdmin($type, '', $lang['strspecifyeditvacuumtable']);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\Controller\AdminTrait::doAdmin() has too many arguments starting with $lang['strspecifyeditvacuumtable']. ( Ignorable by Annotation )

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

352
            /** @scrutinizer ignore-call */ $this->doAdmin($type, '', $lang['strspecifyeditvacuumtable']);

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...
353
            return;
354
        }
355
356
        $script = ($type == 'database') ? 'database.php' : 'tables.php';
357
358
        if ($confirm) {
359
            $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

359
            $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
360
            $this->printTitle(sprintf($lang['streditvacuumtable'], $misc->printVal($_REQUEST['table'])));
0 ignored issues
show
Bug introduced by
It seems like printTitle() 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

360
            $this->/** @scrutinizer ignore-call */ printTitle(sprintf($lang['streditvacuumtable'], $misc->printVal($_REQUEST['table'])));
Loading history...
361
            $this->printMsg(sprintf($msg, $misc->printVal($_REQUEST['table'])));
0 ignored issues
show
Bug introduced by
It seems like printMsg() 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

361
            $this->/** @scrutinizer ignore-call */ printMsg(sprintf($msg, $misc->printVal($_REQUEST['table'])));
Loading history...
362
363
            if (empty($_REQUEST['table'])) {
364
                $this->doAdmin($type, '', $lang['strspecifyeditvacuumtable']);
365
                return;
366
            }
367
368
            $old_val  = $data->getTableAutovacuum($_REQUEST['table']);
369
            $defaults = $data->getAutovacuum();
370
            $old_val  = $old_val->fields;
371
372
            if (isset($old_val['autovacuum_enabled']) and ($old_val['autovacuum_enabled'] == 'off')) {
373
                $enabled  = '';
374
                $disabled = 'checked="checked"';
375
            } else {
376
                $enabled  = 'checked="checked"';
377
                $disabled = '';
378
            }
379
380
            if (!isset($old_val['autovacuum_vacuum_threshold'])) {
381
                $old_val['autovacuum_vacuum_threshold'] = '';
382
            }
383
384
            if (!isset($old_val['autovacuum_vacuum_scale_factor'])) {
385
                $old_val['autovacuum_vacuum_scale_factor'] = '';
386
            }
387
388
            if (!isset($old_val['autovacuum_analyze_threshold'])) {
389
                $old_val['autovacuum_analyze_threshold'] = '';
390
            }
391
392
            if (!isset($old_val['autovacuum_analyze_scale_factor'])) {
393
                $old_val['autovacuum_analyze_scale_factor'] = '';
394
            }
395
396
            if (!isset($old_val['autovacuum_vacuum_cost_delay'])) {
397
                $old_val['autovacuum_vacuum_cost_delay'] = '';
398
            }
399
400
            if (!isset($old_val['autovacuum_vacuum_cost_limit'])) {
401
                $old_val['autovacuum_vacuum_cost_limit'] = '';
402
            }
403
404
            echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
405
            echo $misc->form;
406
            echo "<input type=\"hidden\" name=\"action\" value=\"editautovac\" />\n";
407
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
408
409
            echo "<br />\n<br />\n<table>\n";
410
            echo "\t<tr><td>&nbsp;</td>\n";
411
            echo "<th class=\"data\">{$lang['strnewvalues']}</th><th class=\"data\">{$lang['strdefaultvalues']}</th></tr>\n";
412
            echo "\t<tr><th class=\"data left\">{$lang['strenable']}</th>\n";
413
            echo "<td class=\"data1\">\n";
414
            echo "<label for=\"on\">on</label><input type=\"radio\" name=\"autovacuum_enabled\" id=\"on\" value=\"on\" {$enabled} />\n";
415
            echo "<label for=\"off\">off</label><input type=\"radio\" name=\"autovacuum_enabled\" id=\"off\" value=\"off\" {$disabled} /></td>\n";
416
            echo "<th class=\"data left\">{$defaults['autovacuum']}</th></tr>\n";
417
            echo "\t<tr><th class=\"data left\">{$lang['strvacuumbasethreshold']}</th>\n";
418
            echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_threshold\" value=\"{$old_val['autovacuum_vacuum_threshold']}\" /></td>\n";
419
            echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_threshold']}</th></tr>\n";
420
            echo "\t<tr><th class=\"data left\">{$lang['strvacuumscalefactor']}</th>\n";
421
            echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_scale_factor\" value=\"{$old_val['autovacuum_vacuum_scale_factor']}\" /></td>\n";
422
            echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_scale_factor']}</th></tr>\n";
423
            echo "\t<tr><th class=\"data left\">{$lang['stranalybasethreshold']}</th>\n";
424
            echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_analyze_threshold\" value=\"{$old_val['autovacuum_analyze_threshold']}\" /></td>\n";
425
            echo "<th class=\"data left\">{$defaults['autovacuum_analyze_threshold']}</th></tr>\n";
426
            echo "\t<tr><th class=\"data left\">{$lang['stranalyzescalefactor']}</th>\n";
427
            echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_analyze_scale_factor\" value=\"{$old_val['autovacuum_analyze_scale_factor']}\" /></td>\n";
428
            echo "<th class=\"data left\">{$defaults['autovacuum_analyze_scale_factor']}</th></tr>\n";
429
            echo "\t<tr><th class=\"data left\">{$lang['strvacuumcostdelay']}</th>\n";
430
            echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_cost_delay\" value=\"{$old_val['autovacuum_vacuum_cost_delay']}\" /></td>\n";
431
            echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_cost_delay']}</th></tr>\n";
432
            echo "\t<tr><th class=\"data left\">{$lang['strvacuumcostlimit']}</th>\n";
433
            echo "<td class=\"datat1\"><input type=\"text\" name=\"autovacuum_vacuum_cost_limit\" value=\"{$old_val['autovacuum_vacuum_cost_limit']}\" /></td>\n";
434
            echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_cost_limit']}</th></tr>\n";
435
            echo "</table>\n";
436
            echo '<br />';
437
            echo '<br />';
438
            echo "<input type=\"submit\" name=\"save\" value=\"{$lang['strsave']}\" />\n";
439
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
440
441
            echo "</form>\n";
442
        } else {
443
            $status = $data->saveAutovacuum($_REQUEST['table'], $_POST['autovacuum_enabled'], $_POST['autovacuum_vacuum_threshold'],
444
                $_POST['autovacuum_vacuum_scale_factor'], $_POST['autovacuum_analyze_threshold'], $_POST['autovacuum_analyze_scale_factor'],
445
                $_POST['autovacuum_vacuum_cost_delay'], $_POST['autovacuum_vacuum_cost_limit']);
446
447
            if ($status == 0) {
448
                $this->doAdmin($type, '', sprintf($lang['strsetvacuumtablesaved'], $_REQUEST['table']));
449
            } else {
450
                $this->doEditAutovacuum($type, true, $lang['strsetvacuumtablefail']);
451
            }
452
        }
453
    }
454
455
    /**
456
     * confirm drop autovacuum params for a table and drop it
457
     */
458
    public function doDropAutovacuum($type, $confirm)
0 ignored issues
show
Coding Style introduced by
doDropAutovacuum uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doDropAutovacuum uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doDropAutovacuum uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
459
    {
460
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
461
        $script       = $this->script;
0 ignored issues
show
Unused Code introduced by
The assignment to $script is dead and can be removed.
Loading history...
462
463
        $misc = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
464
        $lang = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
465
        $data = $misc->getDatabaseAccessor();
466
467
        if (empty($_REQUEST['table'])) {
468
            $this->doAdmin($type, '', $lang['strspecifydelvacuumtable']);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\Controller\AdminTrait::doAdmin() has too many arguments starting with $lang['strspecifydelvacuumtable']. ( Ignorable by Annotation )

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

468
            /** @scrutinizer ignore-call */ $this->doAdmin($type, '', $lang['strspecifydelvacuumtable']);

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...
469
            return;
470
        }
471
472
        if ($confirm) {
473
            $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

473
            $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
474
            $this->printTabs($type, 'admin');
0 ignored issues
show
Bug introduced by
It seems like printTabs() 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

474
            $this->/** @scrutinizer ignore-call */ printTabs($type, 'admin');
Loading history...
475
476
            $script = ($type == 'database') ? 'database.php' : 'tables.php';
477
478
            printf("<p>{$lang['strdelvacuumtable']}</p>\n",
479
                $misc->printVal("\"{$_GET['schema']}\".\"{$_GET['table']}\""));
480
481
            echo "<form style=\"float: left\" action=\"{$script}\" method=\"post\">\n";
482
            echo "<input type=\"hidden\" name=\"action\" value=\"delautovac\" />\n";
483
            echo $misc->form;
484
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
485
            echo '<input type="hidden" name="rel" value="', htmlspecialchars(serialize([$_REQUEST['schema'], $_REQUEST['table']])), "\" />\n";
486
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
487
            echo "</form>\n";
488
489
            echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
490
            echo "<input type=\"hidden\" name=\"action\" value=\"admin\" />\n";
491
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
492
            echo $misc->form;
493
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
494
            echo "</form>\n";
495
        } else {
496
            $status = $data->dropAutovacuum($_POST['table']);
497
498
            if ($status == 0) {
499
                $this->doAdmin($type, '', sprintf($lang['strvacuumtablereset'], $misc->printVal($_POST['table'])));
500
            } else {
501
                $this->doAdmin($type, '', sprintf($lang['strdelvacuumtablefail'], $misc->printVal($_POST['table'])));
502
            }
503
        }
504
    }
505
506
    /**
507
     * database/table administration and tuning tasks
508
     *
509
     * $Id: admin.php
510
     */
511
512
    public function doAdmin($type, $msg = '')
0 ignored issues
show
Coding Style introduced by
doAdmin uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
513
    {
514
        $this->script = ($type == 'database') ? 'database.php' : 'tables.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
515
516
        $script = $this->script;
517
518
        $misc = $this->misc;
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
519
        $lang = $this->lang;
1 ignored issue
show
Bug Best Practice introduced by
The property lang does not exist on PHPPgAdmin\Controller\AdminTrait. Did you maybe forget to declare it?
Loading history...
520
521
        $data = $misc->getDatabaseAccessor();
522
523
        $this->printTrail($type);
0 ignored issues
show
Bug introduced by
It seems like printTrail() 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

523
        $this->/** @scrutinizer ignore-call */ printTrail($type);
Loading history...
524
        $this->printTabs($type, 'admin');
0 ignored issues
show
Bug introduced by
It seems like printTabs() 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

524
        $this->/** @scrutinizer ignore-call */ printTabs($type, 'admin');
Loading history...
525
        $this->printMsg($msg);
0 ignored issues
show
Bug introduced by
It seems like printMsg() 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

525
        $this->/** @scrutinizer ignore-call */ printMsg($msg);
Loading history...
526
527
        if ($type == 'database') {
528
            printf("<p>{$lang['stradminondatabase']}</p>\n", $misc->printVal($_REQUEST['object']));
529
        } else {
530
            printf("<p>{$lang['stradminontable']}</p>\n", $misc->printVal($_REQUEST['object']));
531
        }
532
533
        echo "<table style=\"width: 50%\">\n";
534
        echo "<tr>\n";
535
        echo '<th class="data">';
536
        $this->misc->printHelp($lang['strvacuum'], 'pg.admin.vacuum') . "</th>\n";
537
        echo '</th>';
538
        echo '<th class="data">';
539
        $this->misc->printHelp($lang['stranalyze'], 'pg.admin.analyze');
540
        echo '</th>';
541
        if ($data->hasRecluster()) {
542
            echo '<th class="data">';
543
            $this->misc->printHelp($lang['strclusterindex'], 'pg.index.cluster');
544
            echo '</th>';
545
        }
546
        echo '<th class="data">';
547
        $this->misc->printHelp($lang['strreindex'], 'pg.index.reindex');
548
        echo '</th>';
549
        echo '</tr>';
550
551
        // Vacuum
552
        echo "<tr class=\"row1\">\n";
553
        echo "<td style=\"text-align: center; vertical-align: bottom\">\n";
554
        echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
555
556
        echo "<p><input type=\"hidden\" name=\"action\" value=\"confirm_vacuum\" />\n";
557
        echo $misc->form;
558
        if ($type == 'table') {
559
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
560
            echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
561
        }
562
        echo "<input type=\"submit\" value=\"{$lang['strvacuum']}\" /></p>\n";
563
        echo "</form>\n";
564
        echo "</td>\n";
565
566
        // Analyze
567
        echo "<td style=\"text-align: center; vertical-align: bottom\">\n";
568
        echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
569
        echo "<p><input type=\"hidden\" name=\"action\" value=\"confirm_analyze\" />\n";
570
        echo $misc->form;
571
        if ($type == 'table') {
572
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
573
            echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
574
        }
575
        echo "<input type=\"submit\" value=\"{$lang['stranalyze']}\" /></p>\n";
576
        echo "</form>\n";
577
        echo "</td>\n";
578
579
        // Cluster
580
        if ($data->hasRecluster()) {
581
            $disabled = '';
582
            echo "<td style=\"text-align: center; vertical-align: bottom\">\n";
583
            echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
584
            echo $misc->form;
585
            if ($type == 'table') {
586
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
587
                echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
588
                if (!$data->alreadyClustered($_REQUEST['object'])) {
589
                    $disabled = 'disabled="disabled" ';
590
                    echo "{$lang['strnoclusteravailable']}<br />";
591
                }
592
            }
593
            echo "<p><input type=\"hidden\" name=\"action\" value=\"confirm_cluster\" />\n";
594
            echo "<input type=\"submit\" value=\"{$lang['strclusterindex']}\" $disabled/></p>\n";
595
            echo "</form>\n";
596
            echo "</td>\n";
597
        }
598
599
        // Reindex
600
        echo "<td style=\"text-align: center; vertical-align: bottom\">\n";
601
        echo '<form action="' . SUBFOLDER . "/src/views/{$script}\" method=\"post\">\n";
602
        echo "<p><input type=\"hidden\" name=\"action\" value=\"confirm_reindex\" />\n";
603
        echo $misc->form;
604
        if ($type == 'table') {
605
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['object']), "\" />\n";
606
            echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
607
        }
608
        echo "<input type=\"submit\" value=\"{$lang['strreindex']}\" /></p>\n";
609
        echo "</form>\n";
610
        echo "</td>\n";
611
        echo "</tr>\n";
612
        echo "</table>\n";
613
614
        // Autovacuum
615
        if ($data->hasAutovacuum()) {
616
            // get defaults values for autovacuum
617
            $defaults = $data->getAutovacuum();
618
            // Fetch the autovacuum properties from the database or table if != ''
619
            if ($type == 'table') {
620
                $autovac = $data->getTableAutovacuum($_REQUEST['table']);
621
            } else {
622
                $autovac = $data->getTableAutovacuum();
623
            }
624
625
            echo "<br /><br /><h2>{$lang['strvacuumpertable']}</h2>";
626
            echo '<p>' . (($defaults['autovacuum'] == 'on') ? $lang['strturnedon'] : $lang['strturnedoff']) . '</p>';
627
            echo "<p class=\"message\">{$lang['strnotdefaultinred']}</p>";
628
629
            $enlight = function ($f, $p) {
630
                if (isset($f[$p[0]]) and ($f[$p[0]] != $p[1])) {
631
                    return '<span style="color:#F33;font-weight:bold">' . htmlspecialchars($f[$p[0]]) . '</span>';
632
                }
633
634
                return htmlspecialchars($p[1]);
635
            };
636
637
            $columns = [
638
                'namespace'                       => [
639
                    'title' => $lang['strschema'],
640
                    'field' => Decorator::field('nspname'),
641
                    'url'   => SUBFOLDER . "/redirect/schema?{$misc->href}&amp;",
642
                    'vars'  => ['schema' => 'nspname'],
643
                ],
644
                'relname'                         => [
645
                    'title' => $lang['strtable'],
646
                    'field' => Decorator::field('relname'),
647
                    'url'   => SUBFOLDER . "/redirect/table?{$misc->href}&amp;",
648
                    'vars'  => ['table' => 'relname', 'schema' => 'nspname'],
649
                ],
650
                'autovacuum_enabled'              => [
651
                    'title' => $lang['strenabled'],
652
                    'field' => Decorator::callback($enlight, ['autovacuum_enabled', $defaults['autovacuum']]),
653
                    'type'  => 'verbatim',
654
                ],
655
                'autovacuum_vacuum_threshold'     => [
656
                    'title' => $lang['strvacuumbasethreshold'],
657
                    'field' => Decorator::callback($enlight, ['autovacuum_vacuum_threshold', $defaults['autovacuum_vacuum_threshold']]),
658
                    'type'  => 'verbatim',
659
                ],
660
                'autovacuum_vacuum_scale_factor'  => [
661
                    'title' => $lang['strvacuumscalefactor'],
662
                    'field' => Decorator::callback($enlight, ['autovacuum_vacuum_scale_factor', $defaults['autovacuum_vacuum_scale_factor']]),
663
                    'type'  => 'verbatim',
664
                ],
665
                'autovacuum_analyze_threshold'    => [
666
                    'title' => $lang['stranalybasethreshold'],
667
                    'field' => Decorator::callback($enlight, ['autovacuum_analyze_threshold', $defaults['autovacuum_analyze_threshold']]),
668
                    'type'  => 'verbatim',
669
                ],
670
                'autovacuum_analyze_scale_factor' => [
671
                    'title' => $lang['stranalyzescalefactor'],
672
                    'field' => Decorator::callback($enlight, ['autovacuum_analyze_scale_factor', $defaults['autovacuum_analyze_scale_factor']]),
673
                    'type'  => 'verbatim',
674
                ],
675
                'autovacuum_vacuum_cost_delay'    => [
676
                    'title' => $lang['strvacuumcostdelay'],
677
                    'field' => Decorator::concat(Decorator::callback($enlight, ['autovacuum_vacuum_cost_delay', $defaults['autovacuum_vacuum_cost_delay']]), 'ms'),
678
                    'type'  => 'verbatim',
679
                ],
680
                'autovacuum_vacuum_cost_limit'    => [
681
                    'title' => $lang['strvacuumcostlimit'],
682
                    'field' => Decorator::callback($enlight, ['autovacuum_vacuum_cost_limit', $defaults['autovacuum_vacuum_cost_limit']]),
683
                    'type'  => 'verbatim',
684
                ],
685
            ];
686
687
            // Maybe we need to check permissions here?
688
            $columns['actions'] = ['title' => $lang['stractions']];
689
690
            $actions = [
691
                'edit'   => [
692
                    'content' => $lang['stredit'],
693
                    'attr'    => [
694
                        'href' => [
695
                            'url'     => $script,
696
                            'urlvars' => [
697
                                'subject' => $type,
698
                                'action'  => 'confeditautovac',
699
                                'schema'  => Decorator::field('nspname'),
700
                                'table'   => Decorator::field('relname'),
701
                            ],
702
                        ],
703
                    ],
704
                ],
705
                'delete' => [
706
                    'content' => $lang['strdelete'],
707
                    'attr'    => [
708
                        'href' => [
709
                            'url'     => $script,
710
                            'urlvars' => [
711
                                'subject' => $type,
712
                                'action'  => 'confdelautovac',
713
                                'schema'  => Decorator::field('nspname'),
714
                                'table'   => Decorator::field('relname'),
715
                            ],
716
                        ],
717
                    ],
718
                ],
719
            ];
720
721
            if ($type == 'table') {
722
                unset($actions['edit']['vars']['schema'],
723
                    $actions['delete']['vars']['schema'],
724
                    $columns['namespace'],
725
                    $columns['relname']
726
                );
727
            }
728
729
            echo $this->printTable($autovac, $columns, $actions, 'admin-admin', $lang['strnovacuumconf']);
0 ignored issues
show
Bug introduced by
It seems like printTable() 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

729
            echo $this->/** @scrutinizer ignore-call */ printTable($autovac, $columns, $actions, 'admin-admin', $lang['strnovacuumconf']);
Loading history...
730
731
            if (($type == 'table') and ($autovac->recordCount() == 0)) {
732
                echo '<br />';
733
                echo "<a href=\"tables.php?action=confeditautovac&amp;{$misc->href}&amp;table=", htmlspecialchars($_REQUEST['table'])
734
                , "\">{$lang['straddvacuumtable']}</a>";
735
            }
736
        }
737
    }
738
739
    public function adminActions($action, $type)
0 ignored issues
show
Coding Style introduced by
adminActions uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
adminActions uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
740
    {
741
        if ($type == 'database') {
742
            $_REQUEST['object'] = $_REQUEST['database'];
743
            $this->script       = 'database.php';
1 ignored issue
show
Bug Best Practice introduced by
The property script does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
744
        } else {
745
            // $_REQUEST['table'] is no set if we are in the schema page
746
            $_REQUEST['object'] = (isset($_REQUEST['table']) ? $_REQUEST['table'] : '');
747
            $this->script       = 'tables.php';
748
        }
749
750
        $script = $this->script;
0 ignored issues
show
Unused Code introduced by
The assignment to $script is dead and can be removed.
Loading history...
751
752
        switch ($action) {
753
            case 'confirm_cluster':
754
                $this->doCluster($type, true);
755
                break;
756
            case 'confirm_reindex':
757
                $this->doReindex($type, true);
758
                break;
759
            case 'confirm_analyze':
760
                $this->doAnalyze($type, true);
761
                break;
762
            case 'confirm_vacuum':
763
                $this->doVacuum($type, true);
764
                break;
765 View Code Duplication
            case 'cluster':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
766
                if (isset($_POST['cluster'])) {
767
                    $this->doCluster($type);
768
                }
769
770
                // if multi-action from table canceled: back to the schema default page
771
                elseif (($type == 'table') && is_array($_REQUEST['object'])) {
772
                    $this->doDefault();
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

772
                    $this->/** @scrutinizer ignore-call */ doDefault();
Loading history...
773
                } else {
774
                    $this->doAdmin($type);
775
                }
776
777
                break;
778 View Code Duplication
            case 'reindex':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
779
                if (isset($_POST['reindex'])) {
780
                    $this->doReindex($type);
781
                }
782
783
                // if multi-action from table canceled: back to the schema default page
784
                elseif (($type == 'table') && is_array($_REQUEST['object'])) {
785
                    $this->doDefault();
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

785
                    $this->/** @scrutinizer ignore-call */ doDefault();
Loading history...
786
                } else {
787
                    $this->doAdmin($type);
788
                }
789
790
                break;
791 View Code Duplication
            case 'analyze':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
792
                if (isset($_POST['analyze'])) {
793
                    $this->doAnalyze($type);
794
                }
795
796
                // if multi-action from table canceled: back to the schema default page
797
                elseif (($type == 'table') && is_array($_REQUEST['object'])) {
798
                    $this->doDefault();
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

798
                    $this->/** @scrutinizer ignore-call */ doDefault();
Loading history...
799
                } else {
800
                    $this->doAdmin($type);
801
                }
802
803
                break;
804 View Code Duplication
            case 'vacuum':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
805
                if (isset($_POST['vacuum'])) {
806
                    $this->doVacuum($type);
807
                }
808
809
                // if multi-action from table canceled: back to the schema default page
810
                elseif (($type == 'table') && is_array($_REQUEST['object'])) {
811
                    $this->doDefault();
0 ignored issues
show
Bug introduced by
It seems like doDefault() 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

811
                    $this->/** @scrutinizer ignore-call */ doDefault();
Loading history...
812
                } else {
813
                    $this->doAdmin($type);
814
                }
815
816
                break;
817
            case 'admin':
818
                $this->doAdmin($type);
819
                break;
820
            case 'confeditautovac':
821
                $this->doEditAutovacuum($type, true);
822
                break;
823
            case 'confdelautovac':
824
                $this->doDropAutovacuum($type, true);
825
                break;
826
            case 'confaddautovac':
827
                $this->doAddAutovacuum(true);
0 ignored issues
show
Bug introduced by
It seems like doAddAutovacuum() 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

827
                $this->/** @scrutinizer ignore-call */ doAddAutovacuum(true);
Loading history...
828
                break;
829
            case 'editautovac':
830
                if (isset($_POST['save'])) {
831
                    $this->doEditAutovacuum($type, false);
832
                } else {
833
                    $this->doAdmin($type);
834
                }
835
836
                break;
837
            case 'delautovac':
838
                $this->doDropAutovacuum($type, false);
839
                break;
840
            default:
841
                return false;
842
        }
843
        return true;
844
    }
845
}
846