GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( c5ade0...e039e4 )
by Gabriel
05:51
created

Connection::setDatabase()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Database\Connections;
4
5
use Exception;
6
use Nip\Database\Adapters\AbstractAdapter;
7
use Nip\Database\Metadata\Manager as MetadataManager;
8
use Nip\Database\Query\AbstractQuery as AbstractQuery;
9
use Nip\Database\Query\Delete as DeleteQuery;
10
use Nip\Database\Query\Insert as InsertQuery;
11
use Nip\Database\Query\Select as SelectQuery;
12
use Nip\Database\Query\Update as UpdateQuery;
13
use Nip\Database\Result;
14
15
/**
16
 * Class Connection
17
 * @package Nip\Database
18
 */
19
class Connection
20
{
21
22
    /**
23
     * The active PDO connection.
24
     *
25
     * @var PDO
26
     */
27
    protected $pdo;
28
29
    /**
30
     * The name of the connected database.
31
     *
32
     * @var string
33
     */
34
    protected $database;
35
    /**
36
     * The table prefix for the connection.
37
     *
38
     * @var string
39
     */
40
    protected $tablePrefix = '';
41
42
    /**
43
     * The database connection configuration options.
44
     *
45
     * @var array
46
     */
47
    protected $config = [];
48
49
    protected $_adapter = null;
50
51
    protected $metadata;
52
53
    protected $_query;
54
55
    protected $_queries = [];
56
57
    /**
58
     * Create a new database connection instance.
59
     *
60
     * @param  \PDO|\Closure $pdo
61
     * @param  string $database
62
     * @param  string $tablePrefix
63
     * @param  array $config
64
     */
65
    public function __construct($pdo, $database = '', $tablePrefix = '', $config = [])
66
    {
67
        $this->pdo = $pdo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo of type object<PDO> or object<Closure> is incompatible with the declared type object<Nip\Database\Connections\PDO> of property $pdo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
69
        // First we will setup the default properties. We keep track of the DB
70
        // name we are connected to since it is needed when some reflective
71
        // type commands are run such as checking whether a table exists.
72
        $this->database = $database;
73
74
        $this->tablePrefix = $tablePrefix;
75
        $this->config = $config;
76
77
        // We need to initialize a query grammar and the query post processors
78
        // which are both very important parts of the database abstractions
79
        // so we initialize these to their default values while starting.
80
//        $this->useDefaultQueryGrammar();
81
//        $this->useDefaultPostProcessor();
82
    }
83
84
    /**
85
     * Connects to SQL server
86
     *
87
     * @param string $host
88
     * @param string $user
89
     * @param string $password
90
     * @param string $database
91
     * @param bool $newLink
92
     * @return static
93
     */
94
    public function connect($host, $user, $password, $database, $newLink = false)
95
    {
96
        if (!$this->pdo) {
97
            try {
98
                $this->pdo = $this->getAdapter()->connect($host, $user, $password, $database, $newLink);
0 ignored issues
show
Documentation introduced by
$host 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...
Documentation introduced by
$user 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...
Documentation introduced by
$password 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...
Documentation introduced by
$database 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...
99
                $this->setDatabase($database);
100
            } catch (Exception $e) {
101
                $e->log();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method log() does only exist in the following sub-classes of Exception: Nip\AutoLoader\Exception, Nip\Database\Exception, Nip\Logger\Exception, Nip_PHPException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
102
            }
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return AbstractAdapter
110
     */
111
    public function getAdapter()
112
    {
113
        if ($this->_adapter == null) {
114
            $this->initAdapter();
115
        }
116
117
        return $this->_adapter;
118
    }
119
120
    /**
121
     * @param $adapter
122
     */
123
    public function setAdapter($adapter)
124
    {
125
        $this->_adapter = $adapter;
126
    }
127
128
    public function initAdapter()
129
    {
130
        $this->setAdapterName('MySQLi');
131
    }
132
133
    /**
134
     * @param $name
135
     */
136
    public function setAdapterName($name)
137
    {
138
        $this->setAdapter($this->newAdapter($name));
139
    }
140
141
    /**
142
     * @param $name
143
     * @return AbstractAdapter
144
     */
145
    public function newAdapter($name)
146
    {
147
        $class = static::getAdapterClass($name);
148
149
        return new $class();
150
    }
151
152
    /**
153
     * @param $name
154
     * @return string
155
     */
156
    public static function getAdapterClass($name)
157
    {
158
        return '\Nip\Database\Adapters\\' . $name;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getDatabase()
165
    {
166
        return $this->database;
167
    }
168
169
    /**
170
     * @param mixed $database
171
     */
172
    public function setDatabase($database)
173
    {
174
        $this->database = $database;
175
    }
176
177
    /**
178
     * @return MetadataManager
179
     */
180
    public function getMetadata()
181
    {
182
        if (!$this->metadata) {
183
            $this->metadata = new MetadataManager();
184
            $this->metadata->setConnection($this);
185
        }
186
187
        return $this->metadata;
188
    }
189
190
    /**
191
     * Prefixes table names
192
     *
193
     * @param string $table
194
     * @return string
195
     */
196
    public function tableName($table)
197
    {
198
        return $table;
199
    }
200
201
    /**
202
     * @return SelectQuery
203
     */
204
    public function newSelect()
205
    {
206
        return $this->newQuery('select');
207
    }
208
209
    /**
210
     * @param string $type optional
211
     * @return AbstractQuery|SelectQuery|UpdateQuery|InsertQuery|DeleteQuery
212
     */
213
    public function newQuery($type = "select")
214
    {
215
        $className = '\Nip\Database\Query\\' . inflector()->camelize($type);
216
        $query = new $className();
217
        /** @var AbstractQuery $query */
218
        $query->setManager($this);
219
220
        return $query;
221
    }
222
223
    /**
224
     * @return InsertQuery
225
     */
226
    public function newInsert()
227
    {
228
        return $this->newQuery('insert');
229
    }
230
231
    /**
232
     * @return UpdateQuery
233
     */
234
    public function newUpdate()
235
    {
236
        return $this->newQuery('update');
237
    }
238
239
    /**
240
     * @return DeleteQuery
241
     */
242
    public function newDelete()
243
    {
244
        return $this->newQuery('delete');
245
    }
246
247
    /**
248
     * Executes SQL query
249
     *
250
     * @param mixed|AbstractQuery $query
251
     * @return Result
252
     */
253
    public function execute($query)
254
    {
255
        $this->_queries[] = $query;
256
257
        $sql = is_string($query) ? $query : $query->getString();
258
259
        $resultSQL = $this->getAdapter()->execute($sql);
260
        $result = new Result($resultSQL, $this->getAdapter());
261
        $result->setQuery($query);
262
263
        return $result;
264
    }
265
266
    /**
267
     * Gets the ID of the last inserted record
268
     * @return int
269
     */
270
    public function lastInsertID()
271
    {
272
        return $this->getAdapter()->lastInsertID();
273
    }
274
275
    /**
276
     * Gets the number of rows affected by the last operation
277
     * @return int
278
     */
279
    public function affectedRows()
280
    {
281
        return $this->getAdapter()->affectedRows();
282
    }
283
284
    /**
285
     * Disconnects from server
286
     */
287
    public function disconnect()
288
    {
289
        if ($this->pdo) {
290
            try {
291
                $this->getAdapter()->disconnect();
292
            } catch (Exception $e) {
293
                $e->log();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method log() does only exist in the following sub-classes of Exception: Nip\AutoLoader\Exception, Nip\Database\Exception, Nip\Logger\Exception, Nip_PHPException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
294
            }
295
        }
296
    }
297
298
    /**
299
     * @param $table
300
     * @return mixed
301
     */
302
    public function describeTable($table)
303
    {
304
        return $this->getAdapter()->describeTable($this->protect($table));
305
    }
306
307
    /**
308
     * Adds backticks to input
309
     *
310
     * @param string $input
311
     * @return string
312
     */
313
    public function protect($input)
314
    {
315
        return str_replace("`*`", "*", '`' . str_replace('.', '`.`', $input) . '`');
316
    }
317
318
    /**
319
     * @return array
320
     */
321
    public function getQueries()
322
    {
323
        return $this->_queries;
324
    }
325
}
326