Passed
Branch master (cea968)
by Wilmer
34:18 queued 27:49
created

CommandPDO::insertEx()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 55
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0018

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 18
nop 2
dl 0
loc 55
ccs 29
cts 30
cp 0.9667
crap 7.0018
rs 8.5226
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use PDO;
8
use PDOException;
9
use Throwable;
10
use Yiisoft\Db\Driver\PDO\AbstractCommandPDO;
11
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
12
use Yiisoft\Db\Exception\ConvertException;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
use Yiisoft\Db\Schema\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Oracle\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...
15
16
use function array_keys;
17
use function count;
18
use function implode;
19
use function strlen;
20
21
/**
22
 * Command represents an Oracle SQL statement to be executed against a database.
23
 */
24
final class CommandPDO extends AbstractCommandPDO
25
{
26 249
    public function queryBuilder(): QueryBuilderInterface
27
    {
28 249
        return $this->db->getQueryBuilder();
29
    }
30
31 4
    public function insertWithReturningPks(string $table, array $columns): bool|array
32
    {
33 4
        $params = [];
34 4
        $sql = $this->queryBuilder()->insert($table, $columns, $params);
35
36 4
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
37
38 4
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
39 4
        $columnSchemas = $tableSchema?->getColumns() ?? [];
40
41 4
        $returnParams = [];
42 4
        $returning = [];
43
44 4
        foreach ($returnColumns as $name) {
45
            /** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */
46 4
            $phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
47
48 4
            $returnParams[$phName] = [
49 4
                'column' => $name,
50 4
                'value' => '',
51 4
            ];
52
53 4
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) {
54 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
55
            } else {
56 3
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
57
            }
58
59 4
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
60
61 4
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
62
        }
63
64 4
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
65
66 4
        $this->setSql($sql)->bindValues($params);
67 4
        $this->prepare(false);
68
69
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
70 4
        foreach ($returnParams as $name => &$value) {
71 4
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
72
        }
73 4
        unset($value);
74
75 4
        if (!$this->execute()) {
76
            return false;
77
        }
78
79 4
        $result = [];
80
81 4
        foreach ($returnParams as $value) {
82
            /** @psalm-var mixed */
83 4
            $result[$value['column']] = $value['value'];
84
        }
85
86 4
        return $result;
87
    }
88
89 227
    protected function bindPendingParams(): void
90
    {
91 227
        $paramsPassedByReference = [];
92
93 227
        $params = $this->params;
94
95 227
        foreach ($params as $name => $value) {
96 194
            if (PDO::PARAM_STR === $value->getType()) {
97
                /** @var mixed */
98 191
                $paramsPassedByReference[$name] = $value->getValue();
99 191
                $this->pdoStatement?->bindParam(
100 191
                    $name,
101 191
                    $paramsPassedByReference[$name],
102 191
                    $value->getType(),
103 191
                    strlen((string) $value->getValue())
104 191
                );
105
            } else {
106 32
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
107
            }
108
        }
109
    }
110
111
    /**
112
     * @psalm-suppress UnusedClosureParam
113
     *
114
     * @throws Throwable
115
     */
116 226
    protected function internalExecute(?string $rawSql): void
117
    {
118 226
        $attempt = 0;
119
120 226
        while (true) {
121
            try {
122
                if (
123 226
                    ++$attempt === 1
124 226
                    && $this->isolationLevel !== null
125 226
                    && $this->db->getTransaction() === null
126
                ) {
127 1
                    $this->db->transaction(
128 1
                        fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql),
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed. ( Ignorable by Annotation )

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

128
                        fn (/** @scrutinizer ignore-unused */ ConnectionPDOInterface $db) => $this->internalExecute($rawSql),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129 1
                        $this->isolationLevel
130 1
                    );
131
                } else {
132 226
                    $this->pdoStatement?->execute();
133
                }
134 226
                break;
135 7
            } catch (PDOException $e) {
136 7
                $rawSql = $rawSql ?: $this->getRawSql();
137 7
                $e = (new ConvertException($e, $rawSql))->run();
138
139 7
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
140 7
                    throw $e;
141
                }
142
            }
143
        }
144
    }
145
}
146