Completed
Push — 9.0-dev ( 007112...d6cf5f )
by Radu
01:33
created

PdoDatabase::bindParams()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 13
nc 4
nop 1
1
<?php
2
namespace WebServCo\Framework\Libraries;
3
4
final class PdoDatabase extends \WebServCo\Framework\AbstractDatabase implements
5
    \WebServCo\Framework\Interfaces\DatabaseInterface
6
{
7
    use \WebServCo\Framework\Traits\DatabaseTrait;
8
    use \WebServCo\Framework\Traits\MysqlDatabaseTrait;
9
    
10
    public function __construct($config)
11
    {
12
        parent::__construct($config);
13
        
14
        try {
15
            $this->db = new \PDO(
16
                $this->setting('driver', 'mysql') .
0 ignored issues
show
Documentation introduced by
'mysql' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17
                ':host=' . $this->setting('connection/host', '127.0.0.1') .
0 ignored issues
show
Documentation introduced by
'127.0.0.1' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
18
                ';dbname=' . $this->setting('connection/dbname', 'test') .
0 ignored issues
show
Documentation introduced by
'test' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
19
                ';port=' . $this->setting('connection/port', 3306) .
0 ignored issues
show
Documentation introduced by
3306 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
20
                ';charset=utf8mb4',
21
                $this->setting('connection/username', 'root'),
0 ignored issues
show
Documentation introduced by
'root' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
22
                $this->setting('connection/passwd', ''),
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
                [
24
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
25
                    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
26
                    \PDO::ATTR_EMULATE_PREPARES => false,
27
                    \PDO::ATTR_PERSISTENT => false
28
                ]
29
            );
30
        } catch (\Exception $e) {
31
            throw new \ErrorException($e->getMessage());
32
        }
33
    }
34
    
35
    public function escape($string)
36
    {
37
        return $this->db->quote($string);
0 ignored issues
show
Bug introduced by
The method quote does only exist in PDO, but not in mysqli.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
38
    }
39
    
40
    public function executeQuery($query, $params = [])
41
    {
42
        if (empty($query)) {
43
            throw new \ErrorException('No query specified');
44
        }
45
        
46
        if (!empty($params)) {
47
            $this->stmt = $this->db->prepare($query);
48
            $this->bindParams($params);
49
            $this->stmt->execute();
50
        } else {
51
            $this->stmt = $this->db->query($query);
52
        }
53
        $this->setLastInsertId();
54
        return true;
55
    }
56
    
57 View Code Duplication
    public function executeTransaction($data)
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...
58
    {
59
        try {
60
            $this->db->beginTransaction();
0 ignored issues
show
Bug introduced by
The method beginTransaction does only exist in PDO, but not in mysqli.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
            foreach ($data as $item) {
62
                if (!isset($item[0])) {
63
                    throw new \ErrorException('No query specified');
64
                }
65
                $params = isset($item[1]) ? $item[1] : [];
66
                $this->executeQuery($item[0], $params);
67
            }
68
            $this->db->commit();
69
            return true;
70
        } catch (\Exception $e) {
71
            $this->db->rollBack();
72
            throw new \ErrorException($e->getMessage());
73
        }
74
    }
75
    
76
    public function numRows()
77
    {
78
        if (!is_object($this->stmt)) {
79
            throw new \ErrorException('No Statement object available.');
80
        }
81
        if ('mysql' == $this->setting('driver')) {
82
            return $this->stmt->rowCount();
83
        }
84
        $rows = $this->rows ?: $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
85
        return count($rows);
86
    }
87
    
88
    public function affectedRows()
89
    {
90
        if (!is_object($this->stmt)) {
91
            throw new \ErrorException('No Statement object available.');
92
        }
93
        return $this->stmt->rowCount();
94
    }
95
    
96
    public function getRows($query, $params = [])
97
    {
98
        $this->executeQuery($query, $params);
99
        $this->rows = $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
100
        return $this->rows;
101
    }
102
    
103
    public function getRow($query, $params = [])
104
    {
105
        $this->executeQuery($query, $params);
106
        return $this->stmt->fetch(\PDO::FETCH_ASSOC);
107
    }
108
    
109
    public function getColumn($query, $params = [], $columnNumber = 0)
110
    {
111
        $this->executeQuery($query, $params);
112
        return $this->stmt->fetchColumn($columnNumber);
113
    }
114
        
115
    protected function bindParams($data)
116
    {
117
        if (empty($data)) {
118
            return false;
119
        }
120
        
121
        $i = 1;
122
        foreach ($data as $item) {
123
            if (is_array($item)) {
124
                foreach ($item as $v) {
125
                    $this->stmt->bindValue($i, $v, $this->getDataType($v));
126
                    $i++;
127
                }
128
            } else {
129
                $this->stmt->bindValue($i, $item, $this->getDataType($item));
130
                $i++;
131
            }
132
        }
133
        return true;
134
    }
135
    
136
    protected function getDataType($variable)
137
    {
138
        $type = gettype($variable);
139
        
140
        switch ($type) {
141
            case 'NULL':
142
                return PDO::PARAM_NULL;
143
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
144
            case 'boolean':
145
                return \PDO::PARAM_BOOL;
146
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
147
            case 'integer':
148
                return \PDO::PARAM_INT;
149
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
150
            case 'string':
151
            case 'double':
152
            case 'array':
153
            case 'object':
154
            case 'resource':
155
            case 'resource (closed)':
156
            case 'unknown type':
157
                return \PDO::PARAM_STR;
158
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
159
        }
160
    }
161
    
162
    protected function setLastInsertId()
163
    {
164
        $this->lastInsertId = $this->db->lastInsertId();
0 ignored issues
show
Bug introduced by
The method lastInsertId does only exist in PDO, but not in mysqli.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
165
    }
166
}
167