Skip to content

Commit 6d2a3ef

Browse files
authored
Merge pull request #82 from artemeon/feat/phpstan-level-7
2 parents 0133356 + b7e9529 commit 6d2a3ef

8 files changed

Lines changed: 55 additions & 13 deletions

File tree

phpstan.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
includes:
2-
- phar://phpstan.phar/conf/bleedingEdge.neon
32
- phpstan-baseline.neon
43

54
parameters:
6-
level: 6
5+
level: 7
76
paths:
87
- src
98
- tests

src/Connection.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use InvalidArgumentException;
2929
use Override;
3030
use Psr\Log\LoggerInterface;
31+
use RuntimeException;
3132

3233
/**
3334
* This class handles all traffic from and to the database and takes care of a correct tx-handling
@@ -155,6 +156,10 @@ public function multiInsert(string $tableName, array $columns, array $valueSets,
155156
$output = true;
156157
$setsPerInsert = (int) floor(970 / count($columns));
157158

159+
if ($setsPerInsert < 1) {
160+
throw new RuntimeException('At least one set per insert operation must be performed.');
161+
}
162+
158163
foreach (array_chunk($valueSets, $setsPerInsert) as $valueSet) {
159164
$output = $output && $this->dbDriver->triggerMultiInsert(
160165
$tableName,
@@ -1238,13 +1243,11 @@ public function getCacheSize(): int
12381243
* An internal wrapper to dbsafeString, used to process a complete array of parameters
12391244
* as used by prepared statements.
12401245
*
1241-
* @template TKey of array-key
1242-
*
1243-
* @param array<TKey, mixed> $params
1246+
* @param array<array-key, mixed> $params
12441247
* @param list<bool>|false $escapes An array of boolean for each param, used to block the escaping of html-special chars.
12451248
* If not passed, all params will be cleaned.
12461249
*
1247-
* @return array<TKey, mixed>
1250+
* @return list<mixed>
12481251
*
12491252
* @see Db::dbsafeString($string, $htmlSpecialChars = true)
12501253
*/
@@ -1275,7 +1278,7 @@ private function dbsafeParams(array $params, array | false $escapes = []): array
12751278
$replace[$key] = $param;
12761279
}
12771280

1278-
return $replace;
1281+
return array_values($replace);
12791282
}
12801283

12811284
/**

src/Driver/MysqliDriver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function _pQuery(string $query, array $params): bool
160160
throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params);
161161
}
162162

163-
$this->affectedRowsCount = $statement->affected_rows;
163+
$this->affectedRowsCount = (int) $statement->affected_rows;
164164
$statement->free_result();
165165

166166
return $output;
@@ -193,6 +193,10 @@ public function getPArray(string $query, array $params): Generator
193193

194194
$result = $statement->get_result();
195195

196+
if ($result === false) {
197+
return;
198+
}
199+
196200
while ($row = $result->fetch_assoc()) {
197201
yield $row;
198202
}

src/Driver/PostgresDriver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public function _pQuery(string $query, array $params): bool
9797
throw new QueryException('Could not prepare statement: ' . $this->getError(), $query, $params);
9898
}
9999

100+
$this->assertConnected();
101+
100102
$result = pg_execute($this->linkDB, $name, $params);
101103
if ($result === false) {
102104
throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params);
@@ -119,6 +121,8 @@ public function getPArray(string $query, array $params): Generator
119121
throw new QueryException('Could not prepare statement: ' . $this->getError(), $query, $params);
120122
}
121123

124+
$this->assertConnected();
125+
122126
$resultSet = pg_execute($this->linkDB, $name, $params);
123127

124128
if ($resultSet === false) {
@@ -193,6 +197,8 @@ public function insertOrUpdate(string $table, array $columns, array $values, arr
193197
#[Override]
194198
public function getError(): string
195199
{
200+
$this->assertConnected();
201+
196202
return pg_last_error($this->linkDB);
197203
}
198204

@@ -486,6 +492,8 @@ public function transactionRollback(): void
486492
#[Override]
487493
public function getDbInfo(): array
488494
{
495+
$this->assertConnected();
496+
489497
return pg_version($this->linkDB);
490498
}
491499

@@ -626,6 +634,8 @@ private function getPreparedStatementName(string $query): false | string
626634
return $sum;
627635
}
628636

637+
$this->assertConnected();
638+
629639
if (pg_prepare($this->linkDB, $sum, $query)) {
630640
$this->statementsCache[] = $sum;
631641
} else {
@@ -690,4 +700,14 @@ public function getNthLastElementFromSlug(string $column, int $position): string
690700
{
691701
return "SPLIT_PART(REVERSE(SPLIT_PART(REVERSE($column), '/', $position)), '/', 1)";
692702
}
703+
704+
/**
705+
* @phpstan-assert Connection $this->linkDB
706+
*/
707+
private function assertConnected(): void
708+
{
709+
if (!$this->linkDB instanceof Connection) {
710+
throw new ConnectionException('Database not connected.');
711+
}
712+
}
693713
}

src/DriverFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DriverFactory
2626
public function factory(string $driver): DriverInterface
2727
{
2828
$class = 'Artemeon\\Database\\Driver\\' . ucfirst($driver) . 'Driver';
29-
if (!class_exists($class)) {
29+
if (!class_exists($class) || !is_a($class, DriverInterface::class, true)) {
3030
throw new DriverNotFoundException('Configured driver ' . $class . ' does not exist');
3131
}
3232

src/MockConnection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
class MockConnection implements ConnectionInterface
4545
{
4646
/**
47-
* @var list<array<array-key, mixed>>
47+
* @var list<array<string, mixed>>
4848
*/
4949
private array $rows = [];
5050

@@ -76,13 +76,13 @@ public function getPArray(string $query, array $params = [], ?int $start = null,
7676
#[Override]
7777
public function getPRow(string $query, array $params = [], int $number = 0, bool $cache = true, array $escapes = []): array
7878
{
79-
return current($this->rows);
79+
return current($this->rows) ?: [];
8080
}
8181

8282
#[Override]
8383
public function selectRow(string $tableName, array $columns, array $identifiers, bool $cached = true, ?array $escapes = []): ?array
8484
{
85-
return current($this->rows);
85+
return current($this->rows) ?: [];
8686
}
8787

8888
#[Override]

src/Schema/TableColumn.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ class TableColumn implements JsonSerializable
2525
private string $databaseType = '';
2626
private bool $nullable = true;
2727

28+
/**
29+
* @param non-empty-string $name
30+
*/
2831
public static function make(string $name): self
2932
{
3033
return new self($name);
3134
}
3235

36+
/**
37+
* @param non-empty-string $name
38+
*/
3339
public function __construct(private string $name)
3440
{
3541
}
@@ -38,7 +44,7 @@ public function __construct(private string $name)
3844
* @inheritDoc
3945
*
4046
* @return array{
41-
* name: string,
47+
* name: non-empty-string,
4248
* internalType: string,
4349
* databaseType: string,
4450
* nullable: bool,
@@ -67,11 +73,17 @@ public function setNullable(bool $nullable): self
6773
return $this;
6874
}
6975

76+
/**
77+
* @return non-empty-string
78+
*/
7079
public function getName(): string
7180
{
7281
return $this->name;
7382
}
7483

84+
/**
85+
* @param non-empty-string $name
86+
*/
7587
public function setName(string $name): self
7688
{
7789
$this->name = $name;

tests/ConnectionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,10 @@ public function testIntComparison(string $id, int $date, int $expected): void
646646
{
647647
// note calculation does not work if we cross a year border.
648648
$objLeftDate = DateTime::createFromFormat('YmdHis', '' . $date);
649+
if ($objLeftDate === false) {
650+
self::fail('Invalid date given.');
651+
}
652+
649653
$objLeftDate->add(new DateInterval('P1M'));
650654
$left = $objLeftDate->format('YmdHis');
651655

0 commit comments

Comments
 (0)