Skip to content

Commit 67a7e74

Browse files
authored
Merge pull request #83 from artemeon/feat/phpstan-level-8
2 parents 6d2a3ef + 5c3f625 commit 67a7e74

6 files changed

Lines changed: 89 additions & 50 deletions

File tree

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 7
5+
level: 8
66
paths:
77
- src
88
- tests

src/Connection.php

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Connection implements ConnectionInterface
7272
/**
7373
* Instance of the db-driver defined in the configs.
7474
*/
75-
protected ?DriverInterface $dbDriver = null;
75+
protected DriverInterface $dbDriver;
7676

7777
/**
7878
* The number of transactions currently opened.
@@ -120,7 +120,7 @@ public function close(): void
120120
$this->logger?->warning('Rolled back open transactions on deletion of current instance of Db!');
121121
}
122122

123-
if ($this->dbDriver !== null && $this->connected) {
123+
if ($this->connected) {
124124
$this->logger?->info('closing database-connection');
125125

126126
$this->dbDriver->dbclose();
@@ -216,7 +216,7 @@ public function selectRow(
216216
),
217217
);
218218

219-
$row = $this->getPRow($query, array_values($identifiers), 0, $cached, $escapes);
219+
$row = $this->getPRow($query, array_values($identifiers), 0, $cached, $escapes ?? []);
220220
if ($row === []) {
221221
return null;
222222
}
@@ -317,17 +317,15 @@ public function _pQuery(string $query, array $params = [], array $escapes = []):
317317
// Increasing the counter
318318
$this->number++;
319319

320-
if ($this->dbDriver !== null) {
321-
try {
322-
$output = $this->dbDriver->_pQuery($query, $this->dbsafeParams($params, $escapes));
323-
} catch (QueryException $e) {
324-
$prettifiedQuery = $this->prettifyQuery($e->getQuery(), $e->getParams());
320+
try {
321+
$output = $this->dbDriver->_pQuery($query, $this->dbsafeParams($params, $escapes));
322+
} catch (QueryException $e) {
323+
$prettifiedQuery = $this->prettifyQuery($e->getQuery(), $e->getParams());
325324

326-
$this->logger->error($e->getMessage());
327-
$this->logger->error('Query: ' . $prettifiedQuery);
325+
$this->logger?->error($e->getMessage());
326+
$this->logger?->error('Query: ' . $prettifiedQuery);
328327

329-
throw $e;
330-
}
328+
throw $e;
331329
}
332330

333331
if (!$output) {
@@ -618,10 +616,7 @@ private function getError(string $query, array $params): void
618616
$this->dbconnect();
619617
}
620618

621-
$error = '';
622-
if ($this->dbDriver !== null) {
623-
$error = $this->dbDriver->getError();
624-
}
619+
$error = $this->dbDriver->getError();
625620

626621
// reprocess query
627622
$query = str_ireplace(
@@ -665,10 +660,6 @@ public function beginTransaction(): void
665660
$this->dbconnect();
666661
}
667662

668-
if ($this->dbDriver === null) {
669-
return;
670-
}
671-
672663
// just start a new transaction, if no other transaction is open.
673664
if ($this->numberOfOpenTransactions === 0) {
674665
$this->dbDriver->beginTransaction();
@@ -700,10 +691,6 @@ public function commit(): void
700691
$this->dbconnect();
701692
}
702693

703-
if ($this->dbDriver === null) {
704-
return;
705-
}
706-
707694
// check, if the current tx is allowed to be committed.
708695
if ($this->numberOfOpenTransactions === 1) {
709696
$this->numberOfOpenTransactions--;
@@ -743,10 +730,6 @@ public function rollBack(): void
743730
$this->dbconnect();
744731
}
745732

746-
if ($this->dbDriver === null) {
747-
return;
748-
}
749-
750733
if ($this->numberOfOpenTransactions === 1) {
751734
$this->dbDriver->rollBack();
752735
$this->currentTransactionIsDirty = false;
@@ -802,15 +785,13 @@ public function getTables(?string $prefix = null): array
802785

803786
$this->tablesCache[$prefix] = [];
804787

805-
if ($this->dbDriver !== null) {
806-
// increase global counter
807-
$this->number++;
808-
$tables = $this->dbDriver->getTables();
788+
// increase global counter
789+
$this->number++;
790+
$tables = $this->dbDriver->getTables();
809791

810-
foreach ($tables as $table) {
811-
if (str_starts_with((string) $table['name'], $prefix)) {
812-
$this->tablesCache[$prefix][] = $table['name'];
813-
}
792+
foreach ($tables as $table) {
793+
if (str_starts_with((string) $table['name'], $prefix)) {
794+
$this->tablesCache[$prefix][] = $table['name'];
814795
}
815796
}
816797

@@ -845,7 +826,7 @@ public function getColumnsOfTable(string $tableName): array
845826
$columnName = $column->getName();
846827
$return[$columnName] = [
847828
'columnName' => $columnName,
848-
'columnType' => $column->getInternalType(),
829+
'columnType' => $column->getInternalType() ?? DataType::CHAR254,
849830
];
850831
}
851832

@@ -949,7 +930,7 @@ public function generateTableFromMetadata(Table $table): void
949930
{
950931
$columns = [];
951932
foreach ($table->getColumns() as $colDef) {
952-
$columns[$colDef->getName()] = [$colDef->getInternalType(), $colDef->isNullable()];
933+
$columns[$colDef->getName()] = [$colDef->getInternalType() ?? DataType::CHAR254, $colDef->isNullable()];
953934
}
954935

955936
$primary = [];
@@ -1195,10 +1176,6 @@ public function getDbInfo(): array
11951176
$this->dbconnect();
11961177
}
11971178

1198-
if ($this->dbDriver === null) {
1199-
return [];
1200-
}
1201-
12021179
return $this->dbDriver->getDbInfo();
12031180
}
12041181

src/Driver/DriverAbstract.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ abstract class DriverAbstract implements DriverInterface
3838

3939
protected ?ConnectionParameters $config = null;
4040

41+
/**
42+
* @phpstan-assert ConnectionParameters $this->config
43+
*/
4144
public function setConfig(ConnectionParameters $params): void
4245
{
4346
$this->config = $params;

src/Driver/MysqliDriver.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use mysqli_sql_exception;
2828
use mysqli_stmt;
2929
use Override;
30+
use RuntimeException;
3031
use Symfony\Component\Process\ExecutableFinder;
3132
use Symfony\Component\Process\Process;
3233

@@ -98,6 +99,8 @@ public function dbclose(): void
9899
return;
99100
}
100101

102+
$this->assertConnected();
103+
101104
$this->linkDB->close();
102105
$this->linkDB = null;
103106
$this->connected = false;
@@ -237,6 +240,8 @@ public function insertOrUpdate(string $table, array $columns, array $values, arr
237240
#[Override]
238241
public function getError(): string
239242
{
243+
$this->assertConnected();
244+
240245
$error = $this->errorMessage . ' ' . $this->linkDB->error;
241246
$this->errorMessage = '';
242247

@@ -274,7 +279,7 @@ public function getTableInformation(string $tableName): Table
274279
$table->addColumn(
275280
TableColumn::make($column['Field'])
276281
->setInternalType($this->getCoreTypeForDbType($column))
277-
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column)))
282+
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column) ?? DataType::CHAR254))
278283
->setNullable($column['Null'] === 'YES'),
279284
);
280285
}
@@ -454,6 +459,8 @@ public function deleteIndex(string $table, string $index): bool
454459
#[Override]
455460
public function beginTransaction(): void
456461
{
462+
$this->assertConnected();
463+
457464
$this->linkDB->begin_transaction();
458465
}
459466

@@ -472,6 +479,8 @@ public function transactionBegin(): void
472479
#[Override]
473480
public function commit(): void
474481
{
482+
$this->assertConnected();
483+
475484
$this->linkDB->commit();
476485
}
477486

@@ -487,6 +496,8 @@ public function transactionCommit(): void
487496
#[Override]
488497
public function rollBack(): void
489498
{
499+
$this->assertConnected();
500+
490501
$this->linkDB->rollback();
491502
}
492503

@@ -505,6 +516,8 @@ public function transactionRollback(): void
505516
#[Override]
506517
public function getDbInfo(): array
507518
{
519+
$this->assertConnected();
520+
508521
return [
509522
'dbbserver' => 'MySQL ' . $this->linkDB->server_info,
510523
'server_version' => $this->linkDB->server_version,
@@ -542,6 +555,10 @@ public function encloseTableName(string $table): string
542555
#[Override]
543556
public function dbExport(string &$fileName, array $tables): bool
544557
{
558+
if (!$this->config instanceof ConnectionParameters) {
559+
throw new RuntimeException('Connection parameters not set');
560+
}
561+
545562
$dumpBin = new ExecutableFinder()->find($this->dumpBin);
546563
$dumpParams = [
547564
$dumpBin,
@@ -578,7 +595,11 @@ public function dbExport(string &$fileName, array $tables): bool
578595
public function dbImport(string $fileName): bool
579596
{
580597
if (!in_array(pathinfo($fileName, PATHINFO_EXTENSION), ['sql', 'gz'])) {
581-
throw new \RuntimeException(trim($fileName . ' is not a valid import file'));
598+
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
599+
}
600+
601+
if (!$this->config instanceof ConnectionParameters) {
602+
throw new RuntimeException('Connection parameters not set');
582603
}
583604

584605
$restoreBin = new ExecutableFinder()->find($this->restoreBin);
@@ -598,7 +619,7 @@ public function dbImport(string $fileName): bool
598619
} elseif (pathinfo($fileName, PATHINFO_EXTENSION) === 'sql') {
599620
$fileCommand = sprintf('cat %s', escapeshellarg($fileName));
600621
} else {
601-
throw new \RuntimeException(trim($fileName . ' is not a valid import file'));
622+
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
602623
}
603624

604625
$process = new Process([
@@ -636,6 +657,8 @@ private function getPreparedStatement(string $query): false | mysqli_stmt
636657
$this->statementsCache = [];
637658
}
638659

660+
$this->assertConnected();
661+
639662
$statement = $this->linkDB->stmt_init();
640663

641664
try {
@@ -677,4 +700,14 @@ public function getNthLastElementFromSlug(string $column, int $position): string
677700
{
678701
return "SUBSTRING_INDEX(SUBSTRING_INDEX($column, '/', -$position), '/', 1)";
679702
}
703+
704+
/**
705+
* @phpstan-assert mysqli $this->linkDB
706+
*/
707+
private function assertConnected(): void
708+
{
709+
if ($this->linkDB === null) {
710+
throw new ConnectionException('Database not connected.');
711+
}
712+
}
680713
}

src/Driver/PostgresDriver.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function getTableInformation(string $tableName): Table
236236
$table->addColumn(
237237
TableColumn::make($column['column_name'])
238238
->setInternalType($this->getCoreTypeForDbType($column))
239-
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column)))
239+
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column) ?? DataType::CHAR254))
240240
->setNullable($column['is_nullable'] === 'YES'),
241241
);
242242
}
@@ -510,6 +510,10 @@ public function dbExport(string &$fileName, array $tables): bool
510510
$tablesString = '-t ' . implode(' -t ', array_map('escapeshellarg', $tables));
511511
}
512512

513+
if (!$this->config instanceof ConnectionParameters) {
514+
throw new RuntimeException('Connection parameters not set');
515+
}
516+
513517
$port = $this->config->getPort();
514518
if (empty($port)) {
515519
$port = 5432;
@@ -557,6 +561,10 @@ public function dbImport(string $fileName): bool
557561
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
558562
}
559563

564+
if (!$this->config instanceof ConnectionParameters) {
565+
throw new RuntimeException('Connection parameters not set');
566+
}
567+
560568
$restoreBin = new ExecutableFinder()->find($this->restoreBin);
561569
if ($this->handlesDumpCompression() && pathinfo($fileName, PATHINFO_EXTENSION) === 'gz') {
562570
$restoreParams = [
@@ -618,7 +626,7 @@ protected function processQuery(string $query): string
618626
$i++;
619627

620628
return '$' . $i;
621-
}, $query);
629+
}, $query) ?? '';
622630

623631
return str_replace(' LIKE ', ' ILIKE ', $query);
624632
}

0 commit comments

Comments
 (0)