• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / ActiveRecord / 11642569649

02 Nov 2024 12:10PM UTC coverage: 83.671% (-0.4%) from 84.036%
11642569649

push

github

olvlvl
Add ConnectionTelemetry

30 of 41 new or added lines in 4 files covered. (73.17%)

5 existing lines in 3 files now uncovered.

1404 of 1678 relevant lines covered (83.67%)

21.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.0
/lib/ActiveRecord/Statement.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\ActiveRecord;
13

14
use ICanBoogie\Accessor\AccessorTrait;
15
use PDO;
16
use PDOException;
17
use PDOStatement;
18

19
use function is_array;
20
use function json_encode;
21

22
/**
23
 * A database statement.
24
 *
25
 * @see self::get_as_assoc()
26
 * @property-read $this $as_assoc
27
 *     Equivalent to `mode(PDO::FETCH_ASSOC)`.
28
 *
29
 * @see self::get_all()
30
 * @property-read array $all
31
 *     An array with the matching records.
32
 * @see self::get_pairs()
33
 * @property-read array $pairs
34
 *     An array of key/value pairs, where _key_ is the value of the first column and _value_ the value of the second
35
 *     column.
36
 * @see self::get_one()
37
 * @property-read mixed $one
38
 *     The first row of the result set (the cursor is closed).
39
 * @see self::get_rc()
40
 * @property-read int|string|false|null $rc
41
 *     The value of the first column of the first row.
42
 */
43
final class Statement
44
{
45
    use AccessorTrait;
46

47
    public function __construct(
48
        public readonly PDOStatement $pdo_statement,
49
        private readonly ConnectionTelemetry $telemetry,
50
    ) {
51
    }
40✔
52

53
    /**
54
     * Alias of {@see execute()}.
55
     *
56
     * The arguments can be provided as an array or a list of arguments:
57
     *
58
     *     $statement(1, 2, 3);
59
     *     $statement([ 1, 2, 3 ]);
60
     *
61
     * @return $this
62
     */
63
    public function __invoke(mixed ...$args): self
64
    {
65
        if ($args && is_array($args[0])) {
36✔
66
            $args = $args[0];
36✔
67
        }
68

69
        $this->execute($args);
36✔
70

71
        return $this;
36✔
72
    }
73

74
    /**
75
     * Return the {@see queryString} property of the statement.
76
     */
77
    public function __toString(): string
78
    {
79
        return $this->pdo_statement->queryString;
39✔
80
    }
81

82
    /**
83
     * Executes the statement.
84
     *
85
     * The connection queries count is incremented.
86
     *
87
     * @param mixed[] $params
88
     *
89
     * @throws StatementNotValid when the execution of the statement fails.
90
     */
91
    public function execute(array $params = []): void
92
    {
93
        try {
94
            $this->telemetry->record_execute_duration(
40✔
95
                statement: $this->pdo_statement->queryString . ' ' . json_encode($params),
40✔
96
                closure: fn() => $this->pdo_statement->execute($params)
40✔
97
            );
40✔
98
        } catch (PDOException $e) {
1✔
99
            throw new StatementNotValid($this->pdo_statement->queryString, args: $params, original: $e);
1✔
100
        }
101
    }
102

103
    /**
104
     * Set the fetch mode for the statement.
105
     *
106
     * @return $this
107
     *
108
     * @throws UnableToSetFetchMode if the mode cannot be set.
109
     *
110
     * @see PDOStatement::setFetchMode()
111
     */
112
    public function mode(int $mode, string $class_name = null, mixed ...$params): self
113
    {
114
        // @phpstan-ignore-next-line
115
        $this->pdo_statement->setFetchMode(...func_get_args())
×
NEW
116
            ?: throw new UnableToSetFetchMode(func_get_args());
×
117

118
        return $this;
119
    }
120

121
    private function get_as_assoc(): self
122
    {
NEW
123
        return $this->mode(PDO::FETCH_ASSOC);
×
124
    }
125

126
    /**
127
     * Fetches the first row of the result set and closes the cursor.
128
     *
129
     * @see PDOStatement::fetch()
130
     */
131
    public function one(
132
        int $mode = PDO::FETCH_BOTH,
133
        int $cursor_orientation = PDO::FETCH_ORI_NEXT,
134
        int $cursor_offset = 0
135
    ): mixed {
136
        // @phpstan-ignore-next-line
UNCOV
137
        $rc = $this->pdo_statement->fetch(...func_get_args());
×
138

139
        $this->pdo_statement->closeCursor();
×
140

141
        return $rc;
×
142
    }
143

144
    /**
145
     * @see $one
146
     */
147
    private function get_one(): mixed
148
    {
149
        return $this->one();
×
150
    }
151

152
    /**
153
     * Fetches the first column of the first row of the result set and closes the cursor.
154
     *
155
     * @see $rc
156
     */
157
    private function get_rc(): int|string|false|null
158
    {
159
        $rc = $this->pdo_statement->fetchColumn();
1✔
160

161
        $this->pdo_statement->closeCursor();
1✔
162

163
        return $rc;
1✔
164
    }
165

166
    /**
167
     * Returns an array containing all the result-set rows.
168
     *
169
     * @return mixed[]
170
     *
171
     * @see PDOStatement::fetchAll()
172
     */
173
    public function all(int $mode = PDO::FETCH_DEFAULT, mixed ...$args): array
174
    {
175
        // @phpstan-ignore-next-line
176
        return $this->pdo_statement->fetchAll(...func_get_args());
37✔
177
    }
178

179
    /**
180
     * @return mixed[]
181
     *
182
     * @see $all
183
     */
184
    private function get_all(): array
185
    {
186
        return $this->pdo_statement->fetchAll();
1✔
187
    }
188

189
    /**
190
     * @return array<string, string>
191
     *     Where _key_ is the value of the first column and _value_ the value of the second column.
192
     *
193
     * @see $pairs
194
     */
195
    private function get_pairs(): array
196
    {
NEW
197
        return $this->pdo_statement->fetchAll(PDO::FETCH_KEY_PAIR);
×
198
    }
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc