Completed
Pull Request — develop (#209)
by Felipe
26:08 queued 36s
created

Postgres96::hasUserSignals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Database;
8
9
/**
10
 * @file
11
 * PostgreSQL 9.6 support
12
 */
13
class Postgres96 extends Postgres
1 ignored issue
show
Coding Style introduced by
The property $major_version is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
14
{
15
    public $typIndexes    = ['BTREE', 'BRIN', 'RTREE', 'GIST', 'GIN', 'HASH', 'SP-GIST'];
16
    public $major_version = 9.6;
17
18
    // Administration functions
19
20
    /**
21
     * Returns all available process information.
22
     *
23
     * @param null|string $database (optional) Find only connections to specified database
24
     *
25
     * @return \PHPPgAdmin\ADORecordSet A recordset
26
     */
27 View Code Duplication
    public function getProcesses($database = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
29
        if ($database === null) {
30
            $sql = "SELECT datid, datname, pid, usename, application_name, client_addr, state, wait_event_type, wait_event, state_change as query_start,
31
					CASE
32
                        WHEN state='active' THEN query
33
                        ELSE state
34
                    END AS query
35
					FROM pg_catalog.pg_stat_activity
36
					ORDER BY datname, usename, pid";
37
        } else {
38
            $this->clean($database);
39
            $sql = "SELECT datid, datname, pid, usename, application_name, client_addr, state, wait_event_type, wait_event, state_change as query_start,
40
					CASE
41
                        WHEN state='active' THEN query
42
                        ELSE state
43
                    END AS query
44
					FROM pg_catalog.pg_stat_activity
45
					WHERE datname='{$database}'
46
					ORDER BY usename, pid";
47
        }
48
49
        return $this->selectSet($sql);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->selectSet($sql); of type integer|PHPPgAdmin\ADORecordSet adds the type integer to the return on line 49 which is incompatible with the return type documented by PHPPgAdmin\Database\Postgres96::getProcesses of type PHPPgAdmin\ADORecordSet.
Loading history...
50
    }
51
52
    public function hasUserSignals()
53
    {
54
        return true;
55
    }
56
57
    /**
58
     * Creates a new user. As of PG 9.6, CREATEUSER privilege has been deprecated.
59
     *
60
     * @see {@link https://www.postgresql.org/docs/9.6/static/sql-createrole.html}
61
     *
62
     * @param string $username   The username of the user to create
63
     * @param string $password   A password for the user
64
     * @param bool   $createdb   boolean Whether or not the user can create databases
65
     * @param bool   $createrole boolean Whether or not the user can create other users
66
     * @param string $expiry     string Format 'YYYY-MM-DD HH:MM:SS'.  '' means never expire
67
     * @param array  $groups     The groups to create the user in
68
     *
69
     * @return int 0 if operation was successful
0 ignored issues
show
Documentation introduced by
Should the return type not be \PHPPgAdmin\ADORecordSet?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     *
71
     * @internal param $group (array) The groups to create the user in
72
     */
73 View Code Duplication
    public function createUser($username, $password, $createdb, $createrole, $expiry, $groups)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    {
75
        $enc = $this->_encryptPassword($username, $password);
76
        $this->fieldClean($username);
77
        $this->clean($enc);
78
        $this->clean($expiry);
79
        $this->fieldArrayClean($groups);
80
81
        $sql = "CREATE USER \"{$username}\"";
82
        if ($password != '') {
83
            $sql .= " WITH ENCRYPTED PASSWORD '{$enc}'";
84
        }
85
86
        $sql .= $createdb ? ' CREATEDB' : ' NOCREATEDB';
87
        $sql .= $createrole ? ' CREATEROLE' : ' NOCREATEROLE';
88
        if (is_array($groups) && sizeof($groups) > 0) {
89
            $sql .= ' IN GROUP "'.join('", "', $groups).'"';
90
        }
91
92
        if ($expiry != '') {
93
            $sql .= " VALID UNTIL '{$expiry}'";
94
        } else {
95
            $sql .= " VALID UNTIL 'infinity'";
96
        }
97
98
        return $this->execute($sql);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->execute($sql); (PHPPgAdmin\ADORecordSet) is incompatible with the return type of the parent method PHPPgAdmin\Database\Postgres::createUser of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
    }
100
}
101