Skip to content

Commit e949c04

Browse files
authored
Add strict type declarations and modernize codebase (#96)
Add strict type declarations to all method parameters, return types, and properties throughout the codebase. Remove redundant runtime type checks now enforced by the type system. Update PHPDoc to use nullable shorthand and compact format. Replace `list()` with array destructuring, and `switch/case` with `match` expressions. Remove the `InAndNotInUtils` trait, which was introduced to suppress PHPStan errors before strict typing was available. Clean up deprecated test helpers. Raise the minimum required `ipl/stdlib` version to adopt its strict type declarations.
2 parents ffbf95e + bb0bdc2 commit e949c04

43 files changed

Lines changed: 631 additions & 927 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"require": {
99
"php": ">=8.2",
1010
"ext-pdo": "*",
11-
"ipl/stdlib": ">=0.12.0"
11+
"ipl/stdlib": ">=0.15.0"
1212
},
1313
"require-dev": {
1414
"ipl/stdlib": "dev-main"

src/Adapter/BaseAdapter.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ abstract class BaseAdapter implements Adapter
2121
*
2222
* @var array
2323
*/
24-
protected $quoteCharacter = ['"', '"'];
24+
protected array $quoteCharacter = ['"', '"'];
2525

2626
/** @var string Character to use for escaping quote characters */
27-
protected $escapeCharacter = '\\"';
27+
protected string $escapeCharacter = '\\"';
2828

2929
/** @var array Default PDO connect options */
30-
protected $options = [
30+
protected array $options = [
3131
PDO::ATTR_CASE => PDO::CASE_NATURAL,
3232
PDO::ATTR_EMULATE_PREPARES => false,
3333
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
3434
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
3535
PDO::ATTR_STRINGIFY_FETCHES => false
3636
];
3737

38-
public function getDsn(Config $config)
38+
public function getDsn(Config $config): string
3939
{
4040
$dsn = "{$config->db}:";
4141

@@ -50,7 +50,7 @@ public function getDsn(Config $config)
5050
return $dsn . implode(';', $parts);
5151
}
5252

53-
public function getOptions(Config $config)
53+
public function getOptions(Config $config): array
5454
{
5555
if (is_array($config->options)) {
5656
return $config->options + $this->options;
@@ -59,12 +59,12 @@ public function getOptions(Config $config)
5959
return $this->options;
6060
}
6161

62-
public function setClientTimezone(Connection $db)
62+
public function setClientTimezone(Connection $db): static
6363
{
6464
return $this;
6565
}
6666

67-
public function quoteIdentifier($identifiers)
67+
public function quoteIdentifier(string|array $identifiers): string
6868
{
6969
if (is_string($identifiers)) {
7070
$identifiers = explode('.', $identifiers);
@@ -83,11 +83,11 @@ public function quoteIdentifier($identifiers)
8383
return implode('.', $identifiers);
8484
}
8585

86-
public function registerQueryBuilderCallbacks(QueryBuilder $queryBuilder)
86+
public function registerQueryBuilderCallbacks(QueryBuilder $queryBuilder): static
8787
{
8888
$queryBuilder->on(QueryBuilder::ON_ASSEMBLE_SELECT, function (Select $select): void {
8989
if ($select->hasOrderBy()) {
90-
foreach ($select->getOrderBy() as list($_, $direction)) {
90+
foreach ($select->getOrderBy() as [$_, $direction]) {
9191
switch (strtolower($direction ?? '')) {
9292
case '':
9393
case 'asc':
@@ -105,7 +105,7 @@ public function registerQueryBuilderCallbacks(QueryBuilder $queryBuilder)
105105
return $this;
106106
}
107107

108-
protected function getTimezoneOffset()
108+
protected function getTimezoneOffset(): string
109109
{
110110
$tz = new DateTimeZone(date_default_timezone_get());
111111
$offset = $tz->getOffset(new DateTime());

src/Adapter/Mssql.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
class Mssql extends BaseAdapter
1212
{
13-
protected $quoteCharacter = ['[', ']'];
13+
protected array $quoteCharacter = ['[', ']'];
1414

15-
protected $escapeCharacter = '[[]';
15+
protected string $escapeCharacter = '[[]';
1616

17-
public function getDsn(Config $config)
17+
public function getDsn(Config $config): string
1818
{
1919
$drivers = array_intersect(['sqlsrv', 'dblib', 'mssql', 'sybase'], PDO::getAvailableDrivers());
2020

@@ -62,7 +62,7 @@ public function getDsn(Config $config)
6262
return $dsn;
6363
}
6464

65-
public function registerQueryBuilderCallbacks(QueryBuilder $queryBuilder)
65+
public function registerQueryBuilderCallbacks(QueryBuilder $queryBuilder): static
6666
{
6767
parent::registerQueryBuilderCallbacks($queryBuilder);
6868

src/Adapter/Mysql.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
class Mysql extends BaseAdapter
1010
{
11-
protected $quoteCharacter = ['`', '`'];
11+
protected array $quoteCharacter = ['`', '`'];
1212

13-
protected $escapeCharacter = '``';
13+
protected string $escapeCharacter = '``';
1414

15-
public function setClientTimezone(Connection $db)
15+
public function setClientTimezone(Connection $db): static
1616
{
1717
$db->exec('SET time_zone = ' . $db->quote($this->getTimezoneOffset()));
1818

1919
return $this;
2020
}
2121

22-
public function getOptions(Config $config)
22+
public function getOptions(Config $config): array
2323
{
2424
$options = parent::getOptions($config);
2525

src/Adapter/Oracle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Oracle extends BaseAdapter
99
{
10-
public function getDsn(Config $config)
10+
public function getDsn(Config $config): string
1111
{
1212
$dsn = 'oci:dbname=';
1313

@@ -30,7 +30,7 @@ public function getDsn(Config $config)
3030
return $dsn;
3131
}
3232

33-
public function setClientTimezone(Connection $db)
33+
public function setClientTimezone(Connection $db): static
3434
{
3535
$db->prepexec('ALTER SESSION SET TIME_ZONE = ?', [$this->getTimezoneOffset()]);
3636

src/Adapter/Pgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Pgsql extends BaseAdapter
88
{
9-
public function setClientTimezone(Connection $db)
9+
public function setClientTimezone(Connection $db): static
1010
{
1111
$db->exec(sprintf('SET TIME ZONE INTERVAL %s HOUR TO MINUTE', $db->quote($this->getTimezoneOffset())));
1212

src/Adapter/Sqlite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Sqlite extends BaseAdapter
88
{
9-
public function getDsn(Config $config)
9+
public function getDsn(Config $config): string
1010
{
1111
return "sqlite:{$config->dbname}";
1212
}

src/CommonTableExpression.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ trait CommonTableExpression
1717
*
1818
* @var array[]
1919
*/
20-
protected $with = [];
20+
protected array $with = [];
2121

22-
public function getWith()
22+
public function getWith(): array
2323
{
2424
return $this->with;
2525
}
2626

27-
public function with(Select $query, $alias, $recursive = false)
27+
public function with(Select $query, string $alias, bool $recursive = false): static
2828
{
2929
$this->with[] = [$query, $alias, $recursive];
3030

3131
return $this;
3232
}
3333

34-
public function resetWith()
34+
public function resetWith(): static
3535
{
3636
$this->with = [];
3737

@@ -43,7 +43,7 @@ public function resetWith()
4343
*
4444
* Shall be called by using classes in their __clone()
4545
*/
46-
protected function cloneCte()
46+
protected function cloneCte(): void
4747
{
4848
foreach ($this->with as &$cte) {
4949
$cte[0] = clone $cte[0];

src/CommonTableExpressionInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ interface CommonTableExpressionInterface
1717
*
1818
* @return array[]
1919
*/
20-
public function getWith();
20+
public function getWith(): array;
2121

2222
/**
2323
* Add a CTE
2424
*
2525
* @param Select $query
2626
* @param string $alias
27-
* @param bool $recursive
27+
* @param bool $recursive
2828
*
2929
* @return $this
3030
*/
31-
public function with(Select $query, $alias, $recursive = false);
31+
public function with(Select $query, string $alias, bool $recursive = false): static;
3232

3333
/**
3434
* Reset all CTEs
3535
*
3636
* @return $this
3737
*/
38-
public function resetWith();
38+
public function resetWith(): static;
3939
}

src/Compat/FilterProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class FilterProcessor
1515
{
16-
public static function assembleFilter(Filter\Rule $filter, $level = 0)
16+
public static function assembleFilter(Filter\Rule $filter, int $level = 0): ?array
1717
{
1818
$condition = null;
1919

@@ -60,7 +60,7 @@ public static function assembleFilter(Filter\Rule $filter, $level = 0)
6060
return $condition;
6161
}
6262

63-
public static function assemblePredicate(Filter\Condition $filter)
63+
public static function assemblePredicate(Filter\Condition $filter): array
6464
{
6565
$column = $filter->getColumn();
6666
$expression = $filter->getValue();

0 commit comments

Comments
 (0)