Test Failed
Pull Request — master (#94)
by Wilmer
14:06 queued 02:52
created

CommandPDOMysql::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 22
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\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\Exception\InvalidConfigException;
13
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...
14
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...
15
use Yiisoft\Db\Schema\SchemaInterface;
16
17
final class CommandPDOMysql extends Command
18
{
19
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
20
    {
21
        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

21
        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

21
        parent::/** @scrutinizer ignore-call */ 
22
                __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...
22
    }
23
24
    public function queryBuilder(): QueryBuilderInterface
25
    {
26
        return $this->db->getQueryBuilder();
27
    }
28
29
    /**
30
     * @throws Exception|PDOException|InvalidConfigException
31
     */
32
    public function prepare(?bool $forRead = null): void
33
    {
34
        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...
35
            $this->bindPendingParams();
36
37
            return;
38
        }
39
40
        $sql = $this->getSql() ?? '';
41
42
        if ($this->db->getTransaction()) {
43
            /** master is in a transaction. use the same connection. */
44
            $forRead = false;
45
        }
46
47
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
48
            $pdo = $this->db->getSlavePdo();
49
        } else {
50
            $pdo = $this->db->getMasterPdo();
51
        }
52
53
        try {
54
            $this->pdoStatement = $pdo?->prepare($sql);
55
            $this->bindPendingParams();
56
        } catch (PDOException $e) {
57
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
58
            /** @var array|null */
59
            $errorInfo = $e->errorInfo ?? null;
60
61
            throw new Exception($message, $errorInfo, $e);
62
        }
63
    }
64
65
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
66
    {
67
        return [
68
            __CLASS__,
69
            $method,
70
            $fetchMode,
71
            $this->db->getDriver()->getDsn(),
72
            $this->db->getDriver()->getUsername(),
73
            $rawSql,
74
        ];
75
    }
76
77
    protected function internalExecute(?string $rawSql): void
78
    {
79
        $attempt = 0;
80
81
        while (true) {
82
            try {
83
                if (
84
                    ++$attempt === 1
85
                    && $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...
86
                    && $this->db->getTransaction() === null
87
                ) {
88
                    $this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
89
                } else {
90
                    $this->pdoStatement?->execute();
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...
91
                }
92
                break;
93
            } catch (PDOException $e) {
94
                $rawSql = $rawSql ?: $this->getRawSql();
95
                $e = $this->db->getSchema()->convertException($e, $rawSql);
96
97
                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...
98
                    throw $e;
99
                }
100
            }
101
        }
102
    }
103
}
104