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

ICanBoogie / ActiveRecord / 8366801652

20 Mar 2024 10:35PM UTC coverage: 84.036% (-1.7%) from 85.731%
8366801652

push

github

olvlvl
Remove query method forwarding and ArrayAccessor implementation

3 of 3 new or added lines in 3 files covered. (100.0%)

93 existing lines in 5 files now uncovered.

1395 of 1660 relevant lines covered (84.04%)

21.2 hits per line

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

68.97
/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
 * @uses self::get_all()
27
 * @property-read array $all
28
 *     An array with the matching records.
29
 * @uses self::get_pairs()
30
 * @property-read array $pairs
31
 *     An array of key/value pairs, where _key_ is the value of the first column and _value_ the value of the second
32
 *     column.
33
 * @uses self::get_one()
34
 * @property-read mixed $one
35
 *     The first row of the result set (the cursor is closed).
36
 * @uses self::get_rc()
37
 * @property-read string $rc
38
 *     The value of the first column of the first row.
39
 */
40
final class Statement
41
{
42
    use AccessorTrait;
43

44
    /**
45
     * @param PDOStatement<mixed> $pdo_statement
46
     */
47
    public function __construct(
48
        public readonly PDOStatement $pdo_statement,
49
        public readonly Connection $connection
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;
×
80
    }
81

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

95
        $this->connection->queries_count++;
40✔
96

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

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

110
    /**
111
     * Set the fetch mode for the statement.
112
     *
113
     * @return $this
114
     *
115
     * @throws UnableToSetFetchMode if the mode cannot be set.
116
     *
117
     * @link http://www.php.net/manual/en/pdostatement.setfetchmode.php
118
     */
119
    public function mode(int $mode, mixed ...$params): self
120
    {
UNCOV
121
        $this->pdo_statement->setFetchMode(...func_get_args())
×
UNCOV
122
        or throw new UnableToSetFetchMode($mode);
×
123

UNCOV
124
        return $this;
×
125
    }
126

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

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

UNCOV
141
        return $rc;
×
142
    }
143

144
    /**
145
     * Alias for {@see one()}
146
     */
147
    private function get_one(): mixed
148
    {
UNCOV
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 PDOStatement::fetchColumn()
156
     */
157
    private function get_rc(): mixed
158
    {
159
        $rc = $this->pdo_statement->fetchColumn();
1✔
160

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

163
        return $rc;
1✔
164
    }
165

166
    /**
167
     * Alias for {@see PDOStatement::fetchAll()}
168
     *
169
     * @param mixed $mode
170
     *
171
     * @return array<mixed>
172
     */
173
    public function all(...$mode): array
174
    {
175
        return $this->pdo_statement->fetchAll(...$mode);
37✔
176
    }
177

178
    /**
179
     * Alias for {@see all()}.
180
     *
181
     * @return array<mixed>
182
     *
183
     * @used-by self
184
     */
185
    private function get_all(): array
186
    {
187
        return $this->pdo_statement->fetchAll();
1✔
188
    }
189

190
    /**
191
     * Alias for `all(\PDO::FETCH_KEY_PAIR`).
192
     *
193
     * @return array<mixed, mixed>
194
     *     An array of key/value pairs, where _key_ is the value of the first column and _value_ the value of the
195
     *     second column.
196
     *
197
     * @used-by self
198
     */
199
    private function get_pairs(): array
200
    {
UNCOV
201
        return $this->all(mode: PDO::FETCH_KEY_PAIR);
×
202
    }
203
}
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