Passed
Push — main ( 55838a...93e1a1 )
by Thierry
09:27
created

Driver::afterConnectConfig()   A

Complexity

Conditions 6
Paths 18

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 18
nop 0
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\PgSql;
4
5
use Lagdo\DbAdmin\Driver\Exception\AuthException;
6
use Lagdo\DbAdmin\Driver\Utils\Utils;
0 ignored issues
show
Bug introduced by
The type Lagdo\DbAdmin\Driver\Utils\Utils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Lagdo\DbAdmin\Driver\Driver as AbstractDriver;
8
9
class Driver extends AbstractDriver
10
{
11
    /**
12
     * The constructor
13
     *
14
     * @param Utils $utils
15
     * @param array $options
16
     */
17
    public function __construct(Utils $utils, array $options)
18
    {
19
        parent::__construct($utils, $options);
0 ignored issues
show
Bug introduced by
$options of type array is incompatible with the type Lagdo\DbAdmin\Driver\TranslatorInterface expected by parameter $trans of Lagdo\DbAdmin\Driver\Driver::__construct(). ( Ignorable by Annotation )

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

19
        parent::__construct($utils, /** @scrutinizer ignore-type */ $options);
Loading history...
Bug introduced by
The call to Lagdo\DbAdmin\Driver\Driver::__construct() has too few arguments starting with options. ( Ignorable by Annotation )

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

19
        parent::/** @scrutinizer ignore-call */ 
20
                __construct($utils, $options);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
20
21
        $this->server = new Db\Server($this, $this->utils);
0 ignored issues
show
Bug introduced by
The property utils does not exist on Lagdo\DbAdmin\Driver\PgSql\Driver. Did you mean util?
Loading history...
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgS...b\Server::__construct() has too few arguments starting with trans. ( Ignorable by Annotation )

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

21
        $this->server = /** @scrutinizer ignore-call */ new Db\Server($this, $this->utils);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
22
        $this->database = new Db\Database($this, $this->utils);
0 ignored issues
show
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgS...Database::__construct() has too few arguments starting with trans. ( 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->database = /** @scrutinizer ignore-call */ new Db\Database($this, $this->utils);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
23
        $this->table = new Db\Table($this, $this->utils);
0 ignored issues
show
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgSql\Db\Table::__construct() has too few arguments starting with trans. ( Ignorable by Annotation )

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

23
        $this->table = /** @scrutinizer ignore-call */ new Db\Table($this, $this->utils);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
24
        $this->query = new Db\Query($this, $this->utils);
0 ignored issues
show
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgSql\Db\Query::__construct() has too few arguments starting with trans. ( Ignorable by Annotation )

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

24
        $this->query = /** @scrutinizer ignore-call */ new Db\Query($this, $this->utils);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
25
        $this->grammar = new Db\Grammar($this, $this->utils);
0 ignored issues
show
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgS...\Grammar::__construct() has too few arguments starting with trans. ( Ignorable by Annotation )

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

25
        $this->grammar = /** @scrutinizer ignore-call */ new Db\Grammar($this, $this->utils);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function name()
32
    {
33
        return "PostgreSQL";
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    protected function beforeConnectConfig()
40
    {
41
        // Init config
42
        $this->config->jush = 'pgsql';
43
        $this->config->drivers = ["PgSQL", "PDO_PgSQL"];
44
        $this->config->setTypes([ //! arrays
45
            'Numbers' => ["smallint" => 5, "integer" => 10, "bigint" => 19, "boolean" => 1,
46
                "numeric" => 0, "real" => 7, "double precision" => 16, "money" => 20],
47
            'Date and time' => ["date" => 13, "time" => 17, "timestamp" => 20, "timestamptz" => 21, "interval" => 0],
48
            'Strings' => ["character" => 0, "character varying" => 0, "text" => 0,
49
                "tsquery" => 0, "tsvector" => 0, "uuid" => 0, "xml" => 0],
50
            'Binary' => ["bit" => 0, "bit varying" => 0, "bytea" => 0],
51
            'Network' => ["cidr" => 43, "inet" => 43, "macaddr" => 17, "txid_snapshot" => 0],
52
            'Geometry' => ["box" => 0, "circle" => 0, "line" => 0, "lseg" => 0,
53
                "path" => 0, "point" => 0, "polygon" => 0],
54
        ]);
55
        // $this->config->unsigned = [];
56
        $this->config->operators = ["=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE",
57
            "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"]; // no "SQL" to avoid CSRF
58
        $this->config->functions = ["char_length", "lower", "round", "to_hex", "to_timestamp", "upper"];
59
        $this->config->grouping = ["avg", "count", "count distinct", "max", "min", "sum"];
60
        $this->config->editFunctions = [[
61
            "char" => "md5",
62
            "date|time" => "now",
63
        ],[
64
            $this->numberRegex() => "+/-",
65
            "date|time" => "+ interval/- interval", //! escape
66
            "char|text" => "||",
67
        ]];
68
        $this->config->features = ['database', 'table', 'columns', 'sql', 'indexes', 'descidx',
69
            'comment', 'view', 'scheme', 'routine', 'processlist', 'sequence', 'trigger',
70
            'type', 'variables', 'drop_col', 'kill', 'dump', 'fkeys_sql'];
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    protected function afterConnectConfig()
77
    {
78
        if ($this->minVersion(9.3)) {
79
            $this->config->features[] = 'materializedview';
80
        }
81
        if ($this->minVersion(9.2)) {
82
            $this->config->structuredTypes[$this->utils->trans->lang('Strings')][] = "json";
0 ignored issues
show
Bug introduced by
The property utils does not exist on Lagdo\DbAdmin\Driver\PgSql\Driver. Did you mean util?
Loading history...
83
            $this->config->types["json"] = 4294967295;
84
            if ($this->minVersion(9.4)) {
85
                $this->config->structuredTypes[$this->utils->trans->lang('Strings')][] = "jsonb";
86
                $this->config->types["jsonb"] = 4294967295;
87
            }
88
        }
89
        foreach ($this->userTypes() as $type) { //! get types from current_schemas('t')
90
            if (!isset($this->config->types[$type])) {
91
                $this->config->types[$type] = 0;
92
                $this->config->structuredTypes[$this->utils->trans->lang('User types')][] = $type;
93
            }
94
        }
95
    }
96
97
    /**
98
     * @inheritDoc
99
     * @throws AuthException
100
     */
101
    protected function createConnection()
102
    {
103
        if (!$this->options('prefer_pdo', false) && extension_loaded("pgsql")) {
104
            $connection = new Db\PgSql\Connection($this, $this->utils, 'PgSQL');
0 ignored issues
show
Bug introduced by
'PgSQL' of type string is incompatible with the type Lagdo\DbAdmin\Driver\TranslatorInterface expected by parameter $trans of Lagdo\DbAdmin\Driver\PgS...nnection::__construct(). ( Ignorable by Annotation )

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

104
            $connection = new Db\PgSql\Connection($this, $this->utils, /** @scrutinizer ignore-type */ 'PgSQL');
Loading history...
Bug introduced by
The property utils does not exist on Lagdo\DbAdmin\Driver\PgSql\Driver. Did you mean util?
Loading history...
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgS...nnection::__construct() has too few arguments starting with extension. ( Ignorable by Annotation )

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

104
            $connection = /** @scrutinizer ignore-call */ new Db\PgSql\Connection($this, $this->utils, 'PgSQL');

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
105
            return $this->connection = $connection;
106
        }
107
        if (extension_loaded("pdo_pgsql")) {
108
            $connection = new Db\Pdo\Connection($this, $this->utils, 'PDO_PgSQL');
0 ignored issues
show
Bug introduced by
The call to Lagdo\DbAdmin\Driver\PgS...nnection::__construct() has too few arguments starting with extension. ( Ignorable by Annotation )

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

108
            $connection = /** @scrutinizer ignore-call */ new Db\Pdo\Connection($this, $this->utils, 'PDO_PgSQL');

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
Bug introduced by
'PDO_PgSQL' of type string is incompatible with the type Lagdo\DbAdmin\Driver\TranslatorInterface expected by parameter $trans of Lagdo\DbAdmin\Driver\PgS...nnection::__construct(). ( Ignorable by Annotation )

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

108
            $connection = new Db\Pdo\Connection($this, $this->utils, /** @scrutinizer ignore-type */ 'PDO_PgSQL');
Loading history...
109
            return $this->connection = $connection;
110
        }
111
        throw new AuthException($this->utils->trans->lang('No package installed to connect to a PostgreSQL server.'));
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function error()
118
    {
119
        $message = parent::error();
120
        if (preg_match('~^(.*\n)?([^\n]*)\n( *)\^(\n.*)?$~s', $message, $match)) {
121
            $match = array_pad($match, 5, '');
122
            $message = $match[1] . preg_replace('~((?:[^&]|&[^;]*;){' .
123
                strlen($match[3]) . '})(.*)~', '\1<b>\2</b>', $match[2]) . $match[4];
124
        }
125
        return $message;
126
    }
127
}
128