Test Failed
Pull Request — master (#98)
by Wilmer
21:47 queued 09:55
created

CommandPDOPgsql::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Cache\QueryCache;
9
use Yiisoft\Db\Command\Command;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Connection\ConnectionPDOInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Db\Exception\Exception;
12
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Db\Schema\QuoterInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Schema\QuoterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Db\Schema\SchemaInterface;
15
16
final class CommandPDOPgsql extends Command
17
{
18
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
19
    {
20
        parent::__construct($queryCache);
0 ignored issues
show
Bug introduced by
$queryCache of type Yiisoft\Db\Cache\QueryCache is incompatible with the type Yiisoft\Db\Connection\ConnectionInterface expected by parameter $db of Yiisoft\Db\Command\Command::__construct(). ( Ignorable by Annotation )

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

20
        parent::__construct(/** @scrutinizer ignore-type */ $queryCache);
Loading history...
Bug introduced by
The call to Yiisoft\Db\Command\Command::__construct() has too few arguments starting with queryCache. ( Ignorable by Annotation )

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

20
        parent::/** @scrutinizer ignore-call */ 
21
                __construct($queryCache);

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...
21
    }
22
23
    public function queryBuilder(): QueryBuilderInterface
24
    {
25
        return $this->db->getQueryBuilder();
26
    }
27
28
    public function prepare(?bool $forRead = null): void
29
    {
30
        if (isset($this->pdoStatement)) {
0 ignored issues
show
Bug introduced by
The property pdoStatement is declared private in Yiisoft\Db\Command\Command and cannot be accessed from this context.
Loading history...
31
            $this->bindPendingParams();
32
33
            return;
34
        }
35
36
        $sql = $this->getSql();
37
38
        if ($this->db->getTransaction()) {
39
            /** master is in a transaction. use the same connection. */
40
            $forRead = false;
41
        }
42
43
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
44
            $pdo = $this->db->getSlavePdo();
45
        } else {
46
            $pdo = $this->db->getMasterPdo();
47
        }
48
49
        try {
50
            $this->pdoStatement = $pdo->prepare($sql);
51
            $this->bindPendingParams();
52
        } catch (PDOException $e) {
53
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
54
            $errorInfo = $e->errorInfo ?? null;
55
56
            throw new Exception($message, $errorInfo, $e);
57
        }
58
    }
59
60
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
61
    {
62
        return [
63
            __CLASS__,
64
            $method,
65
            $fetchMode,
66
            $this->db->getDriver()->getDsn(),
67
            $this->db->getDriver()->getUsername(),
68
            $rawSql,
69
        ];
70
    }
71
72
    protected function internalExecute(?string $rawSql): void
73
    {
74
        $attempt = 0;
75
76
        while (true) {
77
            try {
78
                if (
79
                    ++$attempt === 1
80
                    && $this->isolationLevel !== null
0 ignored issues
show
Bug introduced by
The property isolationLevel is declared private in Yiisoft\Db\Command\Command and cannot be accessed from this context.
Loading history...
81
                    && $this->db->getTransaction() === null
82
                ) {
83
                    $this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
84
                } else {
85
                    $this->pdoStatement->execute();
0 ignored issues
show
Bug introduced by
The method execute() does not exist on null. ( Ignorable by Annotation )

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

85
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
86
                                         execute();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property pdoStatement is declared private in Yiisoft\Db\Command\Command and cannot be accessed from this context.
Loading history...
86
                }
87
                break;
88
            } catch (\Exception $e) {
89
                $rawSql = $rawSql ?: $this->getRawSql();
90
                $e = $this->db->getSchema()->convertException($e, $rawSql);
91
92
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
0 ignored issues
show
Bug introduced by
The property retryHandler is declared private in Yiisoft\Db\Command\Command and cannot be accessed from this context.
Loading history...
93
                    throw $e;
94
                }
95
            }
96
        }
97
    }
98
}
99