Db_Pgsql   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 90
rs 10
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 25 6
A fetch_assoc() 0 2 1
A query() 0 12 4
A last_query_error() 0 2 1
A num_rows() 0 2 1
A init() 0 8 1
A affected_rows() 0 2 1
A escape_string() 0 6 2
A last_error() 0 2 1
A close() 0 2 1
A fetch_result() 0 2 1
1
<?php
2
class Db_Pgsql implements IDb {
3
    private $link;
4
    private $last_error;
5
6
    public function connect($host, $user, $pass, $db, $port) {
7
        $string = "dbname=$db user=$user";
8
9
        if ($pass) {
10
            $string .= " password=$pass";
11
        }
12
13
        if ($host) {
14
            $string .= " host=$host";
15
        }
16
17
        if (is_numeric($port) && $port > 0) {
18
            $string = "$string port=".$port;
19
        }
20
21
        $this->link = pg_connect($string);
22
23
        if (!$this->link) {
24
            print("Unable to connect to database (as $user to $host, database $db):".pg_last_error());
25
            exit(102);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
26
        }
27
28
        $this->init();
29
30
        return $this->link;
31
    }
32
33
    public function escape_string($s, $strip_tags = true) {
34
        if ($strip_tags) {
35
            $s = strip_tags($s);
36
        }
37
38
        return pg_escape_string($s);
0 ignored issues
show
Bug introduced by
The call to pg_escape_string() has too few arguments starting with data. ( Ignorable by Annotation )

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

38
        return /** @scrutinizer ignore-call */ pg_escape_string($s);

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...
39
    }
40
41
    public function query($query, $die_on_error = true) {
42
        $result = @pg_query($this->link, $query);
43
44
        if (!$result) {
0 ignored issues
show
introduced by
$result is of type resource, thus it always evaluated to false.
Loading history...
45
            $this->last_error = @pg_last_error($this->link);
46
47
            @pg_query($this->link, "ROLLBACK");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for pg_query(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

47
            /** @scrutinizer ignore-unhandled */ @pg_query($this->link, "ROLLBACK");

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
            $query = htmlspecialchars($query); // just in case
49
            user_error("query $query failed: ".($this->link ? $this->last_error : "No connection"),
50
                $die_on_error ? E_USER_ERROR : E_USER_WARNING);
51
        }
52
        return $result;
53
    }
54
55
    public function fetch_assoc($result) {
56
        return pg_fetch_assoc($result);
57
    }
58
59
60
    public function num_rows($result) {
61
        return pg_num_rows($result);
62
    }
63
64
    public function fetch_result($result, $row, $param) {
65
        return pg_fetch_result($result, $row, $param);
66
    }
67
68
    public function close() {
69
        return pg_close($this->link);
70
    }
71
72
    public function affected_rows($result) {
73
        return pg_affected_rows($result);
74
    }
75
76
    public function last_error() {
77
        return pg_last_error($this->link);
78
    }
79
80
    public function last_query_error() {
81
        return $this->last_error;
82
    }
83
84
    public function init() {
85
        $this->query("set client_encoding = 'UTF-8'");
86
        pg_set_client_encoding("UNICODE");
0 ignored issues
show
Bug introduced by
The call to pg_set_client_encoding() has too few arguments starting with encoding. ( Ignorable by Annotation )

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

86
        /** @scrutinizer ignore-call */ 
87
        pg_set_client_encoding("UNICODE");

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
'UNICODE' of type string is incompatible with the type resource expected by parameter $connection of pg_set_client_encoding(). ( Ignorable by Annotation )

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

86
        pg_set_client_encoding(/** @scrutinizer ignore-type */ "UNICODE");
Loading history...
87
        $this->query("set datestyle = 'ISO, european'");
88
        $this->query("set TIME ZONE 0");
89
        $this->query("set cpu_tuple_cost = 0.5");
90
91
        return true;
92
    }
93
}
94