Test Failed
Push — master ( b6dff7...455f41 )
by Gabriel
12:43 queued 11s
created

DatabaseManager::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 2
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Nip\Database;
4
5
use InvalidArgumentException;
6
use Nip\Application\ApplicationInterface;
7
use Nip\Database\Connections\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Nip\Database\Connection.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Nip\Database\Connections\ConnectionFactory;
9
use Nip\Database\Manager\HasApplication;
10
use Nip\Database\Manager\HasConnections;
11
12
/**
13
 * Class DatabaseManager
14
 * @package Nip\Database
15
 */
16
class DatabaseManager
17
{
18
    use HasApplication;
19
    use HasConnections;
20
21
    /**
22
     * The database connection factory instance.
23
     *
24
     * @var ConnectionFactory
25
     */
26
    protected $factory;
27
28
    /**
29
     * DatabaseManager constructor.
30
     * @param ApplicationInterface $application
31
     * @param ConnectionFactory $factory
32
     */
33
    public function __construct(ApplicationInterface $application = null, ConnectionFactory $factory = null)
34
    {
35
        $this->application = $application;
0 ignored issues
show
Documentation Bug introduced by
It seems like $application can also be of type object<Nip\Application\ApplicationInterface>. However, the property $application is declared as type object<Nip\Application>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
        $this->factory = $factory ? $factory : new ConnectionFactory();
37
    }
38
}
39