Passed
Pull Request — master (#380)
by Alexander
03:34 queued 01:02
created

anonymous//tests/Support/Stubs/Connection.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 10
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support\Stubs;
6
7
use Yiisoft\Db\Driver\PDO\CommandPDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
11
use Yiisoft\Db\Driver\PDO\PDODriver;
12
use Yiisoft\Db\Driver\PDO\PDODriverInterface;
13
use Yiisoft\Db\Driver\PDO\TransactionPDO;
14
use Yiisoft\Db\QueryBuilder\QueryBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Tests\Support\Stubs\QueryBuilder. Consider defining an alias.

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...
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
use Yiisoft\Db\Schema\Quoter;
17
use Yiisoft\Db\Schema\QuoterInterface;
18
use Yiisoft\Db\Schema\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Tests\Support\Stubs\Schema. Consider defining an alias.

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...
19
use Yiisoft\Db\Schema\SchemaInterface;
20
use Yiisoft\Db\Tests\Support\Mock;
21
use Yiisoft\Db\Transaction\TransactionInterface;
22
23
final class Connection extends ConnectionPDO implements ConnectionPDOInterface
24
{
25
    protected QueryBuilderInterface|null $queryBuilder = null;
26
    protected SchemaInterface|null $schema = null;
27
28
    public function __construct()
29
    {
30
        $this->mock = new Mock();
31
32
        parent::__construct($this->pdoDriver(), $this->mock->getQueryCache(), $this->mock->getSchemaCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
33
    }
34
35
    protected function initConnection(): void
36
    {
37
    }
38
39
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
40
    {
41
        $command = new CommandPDO($this, $this->mock->getQueryCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
42
43
        if ($sql !== null) {
44
            $command->setSql($sql);
45
        }
46
47
        if ($this->logger !== null) {
48
            $command->setLogger($this->logger);
49
        }
50
51
        if ($this->profiler !== null) {
52
            $command->setProfiler($this->profiler);
53
        }
54
55
        return $command->bindValues($params);
56
    }
57
58
    public function createTransaction(): TransactionInterface
59
    {
60
        return new TransactionPDO($this);
61
    }
62
63
    public function getQueryBuilder(): QueryBuilderInterface
64
    {
65
        if ($this->queryBuilder === null) {
66
            $this->queryBuilder = new QueryBuilder(
0 ignored issues
show
Bug introduced by
The call to Yiisoft\Db\QueryBuilder\...yBuilder::__construct() has too few arguments starting with ddlBuilder. ( Ignorable by Annotation )

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

66
            $this->queryBuilder = /** @scrutinizer ignore-call */ new QueryBuilder(

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...
67
                $this->getQuoter(),
68
                $this->getSchema(),
69
            );
70
        }
71
72
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    public function getQuoter(): QuoterInterface
76
    {
77
        if ($this->quoter === null) {
78
            $this->quoter = new Quoter('`', '`', $this->getTablePrefix());
79
        }
80
81
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
82
    }
83
84
    public function getSchema(): SchemaInterface
85
    {
86
        if ($this->schema === null) {
87
            $this->schema = new Schema($this, $this->mock->getSchemaCache());
0 ignored issues
show
Bug Best Practice introduced by
The property mock does not exist on Yiisoft\Db\Tests\Support\Stubs\Connection. Did you maybe forget to declare it?
Loading history...
88
        }
89
90
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
91
    }
92
93
    private function pdoDriver(): PDODriverInterface
94
    {
95
        return new class ('sqlite::memory:') extends PDODriver implements PDODriverInterface {
96
            public function __construct(string $dsn)
97
            {
98
                parent::__construct($dsn);
99
            }
100
101
            public function getDriverName(): string
102
            {
103
                return 'sqlite';
104
            }
105
        };
106
    }
107
}
108