Passed
Push — dbal ( 545eb7...a65111 )
by Greg
13:50 queued 06:40
created

DriverTrait::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2024 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB\Drivers;
21
22
use Doctrine\DBAL\Driver\PDO\Connection;
23
use PDO;
24
use PDOException;
25
use RuntimeException;
26
use SensitiveParameter;
1 ignored issue
show
Bug introduced by
The type SensitiveParameter 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...
27
28
use function is_bool;
29
use function is_int;
30
31
/**
32
 * Common functionality for all drivers.
33
 */
34
trait DriverTrait
35
{
36
    public function __construct(private readonly PDO $pdo)
37
    {
38
    }
39
40
    public function connect(
41
        #[SensitiveParameter]
0 ignored issues
show
Unused Code introduced by
The parameter $params 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

41
        /** @scrutinizer ignore-unused */ #[SensitiveParameter]

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...
42
        array $params,
43
    ): Connection {
44
        return new Connection($this->pdo);
45
    }
46
47
    /**
48
     * Prepare, bind and execute a select query.
49
     *
50
     * @param string                            $sql
51
     * @param array<bool|int|float|string|null> $bindings
52
     *
53
     * @return array<object>
54
     */
55
    public function query(string $sql, array $bindings = []): array
56
    {
57
        try {
58
            $statement = $this->pdo->prepare($sql);
59
        } catch (PDOException) {
60
            $statement = false;
61
        }
62
63
        if ($statement === false) {
64
            throw new RuntimeException('Failed to prepare statement: ' . $sql);
65
        }
66
67
        foreach ($bindings as $param => $value) {
68
            $type = match (true) {
69
                $value === null        => PDO::PARAM_NULL,
70
                is_bool(value: $value) => PDO::PARAM_BOOL,
71
                is_int(value: $value)  => PDO::PARAM_INT,
72
                default                => PDO::PARAM_STR,
73
            };
74
75
            if (is_int(value: $param)) {
76
                // Positional parameters are numeric, starting at 1.
77
                $statement->bindValue(param: $param + 1, value: $value, type: $type);
78
            } else {
79
                // Named parameters are (optionally) prefixed with a colon.
80
                $statement->bindValue(param: ':' . $param, value: $value, type: $type);
81
            }
82
        }
83
84
        if ($statement->execute()) {
85
            return $statement->fetchAll(PDO::FETCH_OBJ);
86
        }
87
88
        throw new RuntimeException('Failed to execute statement: ' . $sql);
89
    }
90
}
91