Test Failed
Pull Request — dev (#44)
by Wilmer
03:42
created

CommandPDOOracle::prepare()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 13
nop 1
dl 0
loc 29
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\PDO;
6
7
use PDO;
8
use PDOException;
9
use Yiisoft\Db\Cache\QueryCache;
10
use Yiisoft\Db\Command\Command;
11
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...
12
use Yiisoft\Db\Exception\Exception;
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
/**
18
 * Command represents an Oracle SQL statement to be executed against a database.
19
 */
20
final class CommandPDOOracle extends Command
21
{
22
    private int $i = 0;
0 ignored issues
show
introduced by
The private property $i is not used, and could be removed.
Loading history...
23
24
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
25
    {
26
        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

26
        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

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

112
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
113
                                         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...
113
                }
114
                break;
115
            } catch (PDOException $e) {
116
                $rawSql = $rawSql ?: $this->getRawSql();
117
                $e = $this->db->getSchema()->convertException($e, $rawSql);
118
119
                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...
120
                    throw $e;
121
                }
122
            }
123
        }
124
    }
125
}
126