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

ICanBoogie / ActiveRecord / 8587980914

07 Apr 2024 10:12AM UTC coverage: 85.614% (+1.7%) from 83.927%
8587980914

push

github

olvlvl
Tidy Query

13 of 22 new or added lines in 3 files covered. (59.09%)

116 existing lines in 8 files now uncovered.

1345 of 1571 relevant lines covered (85.61%)

24.54 hits per line

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

90.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
use function microtime;
22

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

48
    public function __construct(
49
        public readonly PDOStatement $pdo_statement,
50
        public readonly Connection $connection
51
    ) {
52
    }
44✔
53

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

70
        $this->execute($args);
40✔
71

72
        return $this;
40✔
73
    }
74

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

83
    /**
84
     * Executes the statement.
85
     *
86
     * The connection queries count is incremented.
87
     *
88
     * @param mixed[] $params
89
     *
90
     * @throws StatementNotValid when the execution of the statement fails.
91
     */
92
    public function execute(array $params = []): void
93
    {
94
        $start = microtime(true);
44✔
95

96
        $this->connection->queries_count++;
44✔
97

98
        try {
99
            $this->connection->profiling[] = [
44✔
100
                $start,
44✔
101
                microtime(true),
44✔
102
                $this->pdo_statement->queryString . ' ' . json_encode($params),
44✔
103
            ];
44✔
104

105
            $this->pdo_statement->execute($params);
44✔
106
        } catch (PDOException $e) {
3✔
107
            throw new StatementNotValid($this->pdo_statement->queryString, args: $params, original: $e);
3✔
108
        }
109
    }
110

111
    /**
112
     * Set the fetch mode for the statement.
113
     *
114
     * @return $this
115
     *
116
     * @throws UnableToSetFetchMode if the mode cannot be set.
117
     *
118
     * @see PDOStatement::setFetchMode()
119
     */
120
    public function mode(int $mode, string $class_name = null, mixed ...$params): self
121
    {
122
        // @phpstan-ignore-next-line
123
        $this->pdo_statement->setFetchMode(...func_get_args())
12✔
UNCOV
124
            ?: throw new UnableToSetFetchMode(func_get_args());
×
125

126
        return $this;
12✔
127
    }
128

129
    private function get_as_assoc(): self
130
    {
131
        return $this->mode(PDO::FETCH_ASSOC);
3✔
132
    }
133

134
    /**
135
     * Fetches the first row of the result set and closes the cursor.
136
     *
137
     * @see PDOStatement::fetch()
138
     */
139
    public function one(
140
        int $mode = PDO::FETCH_BOTH,
141
        int $cursor_orientation = PDO::FETCH_ORI_NEXT,
142
        int $cursor_offset = 0
143
    ): mixed {
144
        // @phpstan-ignore-next-line
145
        $rc = $this->pdo_statement->fetch(...func_get_args());
12✔
146

147
        $this->pdo_statement->closeCursor();
12✔
148

149
        return $rc;
12✔
150
    }
151

152
    /**
153
     * @see $one
154
     */
155
    private function get_one(): mixed
156
    {
157
        return $this->one();
12✔
158
    }
159

160
    /**
161
     * Fetches the first column of the first row of the result set and closes the cursor.
162
     *
163
     * @see $rc
164
     */
165
    private function get_rc(): int|string|false|null
166
    {
167
        $rc = $this->pdo_statement->fetchColumn();
2✔
168

169
        $this->pdo_statement->closeCursor();
2✔
170

171
        return $rc;
2✔
172
    }
173

174
    /**
175
     * Returns an array containing all the result-set rows.
176
     *
177
     * @return mixed[]
178
     *
179
     * @see PDOStatement::fetchAll()
180
     */
181
    public function all(int $mode = PDO::FETCH_DEFAULT, mixed ...$args): array
182
    {
183
        // @phpstan-ignore-next-line
184
        return $this->pdo_statement->fetchAll(...func_get_args());
37✔
185
    }
186

187
    /**
188
     * @return mixed[]
189
     *
190
     * @see $all
191
     */
192
    private function get_all(): array
193
    {
194
        return $this->pdo_statement->fetchAll();
1✔
195
    }
196

197
    /**
198
     * @return array<string, string>
199
     *     Where _key_ is the value of the first column and _value_ the value of the second column.
200
     *
201
     * @see $pairs
202
     */
203
    private function get_pairs(): array
204
    {
UNCOV
205
        return $this->pdo_statement->fetchAll(PDO::FETCH_KEY_PAIR);
×
206
    }
207
}
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