Skip to content

Commit 1932bc5

Browse files
committed
fix(quality): resolve mago lint findings in the SQLite drivers
1 parent 1e84a29 commit 1932bc5

4 files changed

Lines changed: 29 additions & 31 deletions

File tree

mago-analyze-baseline.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8308,7 +8308,7 @@ count = 2
83088308
file = "phpmyfaq/src/phpMyFAQ/Database/PdoSqlite.php"
83098309
code = "mixed-assignment"
83108310
message = "Assigning `mixed` type to a variable may lead to unexpected behavior."
8311-
count = 7
8311+
count = 6
83128312

83138313
[[issues]]
83148314
file = "phpmyfaq/src/phpMyFAQ/Database/PdoSqlite.php"
@@ -8386,7 +8386,7 @@ count = 1
83868386
file = "phpmyfaq/src/phpMyFAQ/Database/PdoSqlite.php"
83878387
code = "possible-method-access-on-null"
83888388
message = "Attempting to call a method on `null`."
8389-
count = 8
8389+
count = 7
83908390

83918391
[[issues]]
83928392
file = "phpmyfaq/src/phpMyFAQ/Database/PdoSqlsrv.php"

mago-lint-baseline.toml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ code = "literal-named-argument"
156156
message = "Literal argument `true` should be passed as a named argument for clarity."
157157
count = 1
158158

159-
[[issues]]
160-
file = "phpmyfaq/src/phpMyFAQ/Database/Sqlite3.php"
161-
code = "no-error-control-operator"
162-
message = "Unsafe use of error control operator `@`."
163-
count = 1
164-
165159
[[issues]]
166160
file = "phpmyfaq/src/phpMyFAQ/Entity/FaqEntity.php"
167161
code = "too-many-properties"
@@ -204,12 +198,6 @@ code = "no-error-control-operator"
204198
message = "Unsafe use of error control operator `@`."
205199
count = 1
206200

207-
[[issues]]
208-
file = "phpmyfaq/src/phpMyFAQ/Faq.php"
209-
code = "kan-defect"
210-
message = "Class has a high kan defect score."
211-
count = 1
212-
213201
[[issues]]
214202
file = "phpmyfaq/src/phpMyFAQ/Faq.php"
215203
code = "too-many-methods"

phpmyfaq/src/phpMyFAQ/Database/PdoSqlite.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,17 @@ public function numRows(mixed $result): int
171171
$inner = rtrim($sql, characters: " \t\n\r\0\x0B;");
172172
$countSql = 'SELECT COUNT(*) AS c FROM (' . $inner . ') AS _pmf_cnt';
173173

174-
if ($result instanceof PDOStatement && isset($this->preparedParams[$result])) {
175-
// Re-bind the original parameters for prepared statements
176-
$stmt = $this->pdo->prepare($countSql);
177-
if ($stmt === false) {
178-
return 0;
179-
}
180-
181-
$stmt->execute($this->preparedParams[$result]);
182-
} else {
183-
$stmt = $this->pdo->query($countSql);
184-
if ($stmt === false) {
185-
return 0;
186-
}
174+
$stmt = $this->pdo->prepare($countSql);
175+
if ($stmt === false) {
176+
return 0;
187177
}
188178

179+
// Re-bind the original parameters for prepared statements
180+
$params = $result instanceof PDOStatement && $this->preparedParams->offsetExists($result)
181+
? $this->preparedParams[$result]
182+
: [];
183+
$stmt->execute($params);
184+
189185
$row = $stmt->fetch(PDO::FETCH_NUM);
190186
return is_array($row) && array_key_exists(0, $row) ? (int) $row[0] : 0;
191187
}
@@ -337,7 +333,6 @@ public function query(string $query, int $offset = 0, int $rowcount = 0): mixed
337333
}
338334

339335
try {
340-
/** @var PDOStatement|false $result */
341336
$result = $this->pdo?->query($query) ?? false;
342337
} catch (PDOException $pdoException) {
343338
throw new Exception($pdoException->getMessage());

phpmyfaq/src/phpMyFAQ/Database/Sqlite3.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function connect(
6363
?int $port = null,
6464
): ?bool {
6565
$this->conn = new \Sqlite3($host);
66+
$this->conn->enableExceptions(true);
6667

6768
return true;
6869
}
@@ -190,7 +191,11 @@ public function query(string $query, int $offset = 0, int $rowcount = 0): \SQLit
190191
$query .= sprintf(' LIMIT %d,%d', $offset, $rowcount);
191192
}
192193

193-
$result = @$this->conn->query($query);
194+
try {
195+
$result = $this->conn->query($query);
196+
} catch (\SQLite3Exception) {
197+
$result = false;
198+
}
194199

195200
if (!$result) {
196201
$this->sqlLog .= $this->error();
@@ -212,7 +217,12 @@ public function queryPrepared(string $query, array $params): \SQLite3Result|bool
212217
return false;
213218
}
214219

215-
$statement = @$this->conn->prepare($query);
220+
try {
221+
$statement = $this->conn->prepare($query);
222+
} catch (\SQLite3Exception) {
223+
$statement = false;
224+
}
225+
216226
if (!$statement instanceof \SQLite3Stmt) {
217227
$this->sqlLog .= $this->error();
218228

@@ -225,7 +235,12 @@ public function queryPrepared(string $query, array $params): \SQLite3Result|bool
225235
++$position;
226236
}
227237

228-
$result = @$statement->execute();
238+
try {
239+
$result = $statement->execute();
240+
} catch (\SQLite3Exception) {
241+
$result = false;
242+
}
243+
229244
if ($result === false) {
230245
$this->sqlLog .= $this->error();
231246
}

0 commit comments

Comments
 (0)