Passed
Branch psalm-3 (d5d890)
by Wilmer
02:56
created

CommandPdoTrait::getPdoStatement()   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
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Command;
6
7
use PDOStatement;
8
9
trait CommandPdoTrait
10
{
11
    /**
12
     * @psalm-var ParamInterface[]
13
     */
14
    protected array $params = [];
15
16
    protected ?PDOStatement $pdoStatement = null;
17
18
    public function getPdoStatement(): ?PDOStatement
19
    {
20
        return $this->pdoStatement;
21
    }
22
23
    public function cancel(): void
24
    {
25
        $this->pdoStatement = null;
26
    }
27
28
    /**
29
     * Binds pending parameters that were registered via {@see bindValue()} and {@see bindValues()}.
30
     *
31
     * Note that this method requires an active {@see pdoStatement}.
32
     */
33
    protected function bindPendingParams(): void
34
    {
35
        foreach ($this->params as $name => $value) {
36
            $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
37
        }
38
    }
39
40
    public function bindParam(
41
        int|string $name,
42
        mixed &$value,
43
        ?int $dataType = null,
44
        ?int $length = null,
45
        mixed $driverOptions = null
46
    ): static {
47
        $this->prepare();
0 ignored issues
show
Bug introduced by
It seems like prepare() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        $this->/** @scrutinizer ignore-call */ 
48
               prepare();
Loading history...
48
49
        if ($dataType === null) {
50
            $dataType = $this->queryBuilder()->schema()->getPdoType($value);
0 ignored issues
show
Bug introduced by
It seems like queryBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
            $dataType = $this->/** @scrutinizer ignore-call */ queryBuilder()->schema()->getPdoType($value);
Loading history...
51
        }
52
53
        if ($length === null) {
54
            $this->pdoStatement?->bindParam($name, $value, $dataType);
55
        } elseif ($driverOptions === null) {
56
            $this->pdoStatement?->bindParam($name, $value, $dataType, $length);
57
        } else {
58
            $this->pdoStatement?->bindParam($name, $value, $dataType, $length, $driverOptions);
59
        }
60
61
        return $this;
62
    }
63
}
64