Skip to content

Commit e680d4a

Browse files
committed
Fixed some issues reported by phpstan
1 parent 112334c commit e680d4a

File tree

7 files changed

+21
-30
lines changed

7 files changed

+21
-30
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: max
2+
level: 6
33
paths:
44
- src
55
- tests

src/Builder/Expr/DBExprFilter.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,4 @@ private function buildKey($keyPath): array {
8787
}
8888
return $keyPath;
8989
}
90-
91-
/**
92-
* @param array<string, mixed> $array
93-
* @return bool
94-
*/
95-
private function isValidArray(array $array): bool {
96-
$data = array_filter($array, function ($value) {
97-
if(is_array($value)) {
98-
return $this->isValidArray($value);
99-
}
100-
return (string) $value !== '';
101-
});
102-
return count($data) > 0;
103-
}
10490
}

src/Builder/Helpers/RecursiveStructureAccess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function recursiveHas($structure, $path): bool {
2424
public static function recursiveGet($structure, $path, $default) {
2525
$arrayPath = self::getArrayPath($path);
2626
$data = self::ensureArrayIsArray($structure);
27-
$count = count($arrayPath ?? []);
27+
$count = count($arrayPath);
2828
if (!$count) {
2929
return $default;
3030
}

src/Builder/QueryStatement.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ public function execute(array $params = []) {
7979
* @return array<mixed, mixed>
8080
*/
8181
public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []): array {
82-
return $this->exceptionHandler($this->query, function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
82+
$result = $this->exceptionHandler($this->query, function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
8383
if($fetchArgument !== null) {
8484
return $this->statement->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs);
8585
}
8686
return $this->statement->fetchAll($fetchStyle);
8787
});
88+
if(is_bool($result)) {
89+
return [];
90+
}
91+
return $result;
8892
}
8993

9094
/**

src/Builder/Traits/CreateDDLRunnable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
trait CreateDDLRunnable {
1111
/**
12-
* @template I
1312
* @template T
1413
* @param DatabaseStatement $query
1514
* @param callable(scalar=): T $callbackFn

src/Databases/MySQL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public function transaction(callable $callback) {
313313
$this->transactionCommit();
314314
return $result;
315315
} catch (Throwable $e) {
316+
// @phpstan-ignore-next-line; If condition is always false as it is not.
316317
if($this->pdo->inTransaction()) {
317318
$this->transactionRollback();
318319
}

tests/Databases/MySQLTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function testNestedTransactionWithException(): void {
6161
self::assertEquals(101, $this->getDB()->query('SELECT field1 FROM test1 WHERE id=1')->fetchColumn(0));
6262
}
6363
});
64+
// @phpstan-ignore-next-line - "Unreachable statement - code above always terminates."
6465
self::assertEquals(100, $this->getDB()->query('SELECT field1 FROM test1 WHERE id=1')->fetchColumn(0));
6566
}
6667

@@ -166,7 +167,7 @@ public function testFetchValue(): void {
166167
self::assertEquals('1', $value);
167168
}
168169

169-
public function testInfoLoggingFromQuery() {
170+
public function testInfoLoggingFromQuery(): void {
170171
$log = (object) ['queries' => []];
171172
$db = $this->getDB();
172173
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity) use ($log) {
@@ -177,21 +178,21 @@ public function testInfoLoggingFromQuery() {
177178
self::assertEquals($query, $log->queries[0]['query'] ?? null);
178179
}
179180

180-
public function testErrorLoggingFromQuery() {
181+
public function testErrorLoggingFromQuery(): void {
181182
$log = (object) ['queries' => []];
182183
$db = $this->getDB();
183-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, PDOException $e) use ($log) {
184+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
184185
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
185186
}));
186187
$query = 'SELECT COUNT(*) FROM test1_';
187188
try {
188189
$db->query($query)->fetchColumn(0);
189190
} catch (PDOException $e) {}
190191
self::assertEquals($query, $log->queries[0]['query'] ?? null);
191-
self::assertContains('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
192+
self::assertStringContainsString('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
192193
}
193194

194-
public function testInfoLoggingFromExec() {
195+
public function testInfoLoggingFromExec(): void {
195196
$log = (object) ['queries' => []];
196197
$db = $this->getDB();
197198
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration) use ($log) {
@@ -202,21 +203,21 @@ public function testInfoLoggingFromExec() {
202203
self::assertEquals($query, $log->queries[0]['query'] ?? null);
203204
}
204205

205-
public function testErrorLoggingFromExec() {
206+
public function testErrorLoggingFromExec(): void {
206207
$log = (object) ['queries' => []];
207208
$db = $this->getDB();
208-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, PDOException $e) use ($log) {
209+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
209210
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
210211
}));
211212
$query = 'UPDATE x SET y=1';
212213
try {
213214
$db->exec($query);
214215
} catch (PDOException $e) {}
215216
self::assertEquals($query, $log->queries[0]['query'] ?? null);
216-
self::assertContains('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
217+
self::assertStringContainsString('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
217218
}
218219

219-
public function testInfoLoggingFromGetTableFields() {
220+
public function testInfoLoggingFromGetTableFields(): void {
220221
$log = (object) ['queries' => []];
221222
$db = $this->getDB();
222223
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration) use ($log) {
@@ -227,17 +228,17 @@ public function testInfoLoggingFromGetTableFields() {
227228
self::assertEquals($query, $log->queries[0]['query'] ?? null);
228229
}
229230

230-
public function testErrorLoggingFromGetTableFields() {
231+
public function testErrorLoggingFromGetTableFields(): void {
231232
$log = (object) ['queries' => []];
232233
$db = $this->getDB();
233-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, PDOException $e) use ($log) {
234+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
234235
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
235236
}));
236237
$query = 'DESCRIBE test1_';
237238
try {
238239
$db->getTableFields('test1_');
239240
} catch (PDOException $e) {}
240241
self::assertEquals($query, $log->queries[0]['query'] ?? null);
241-
self::assertContains('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
242+
self::assertStringContainsString('SQLSTATE[42S02]', ($log->queries[0]['exception'] ?? null)->getMessage());
242243
}
243244
}

0 commit comments

Comments
 (0)