Skip to content

Commit 5d603a9

Browse files
committed
PHP8.0 specific improvements
1 parent 58ebbc1 commit 5d603a9

28 files changed

+90
-146
lines changed

src/Builder/DBExpr.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
namespace Kir\MySQL\Builder;
33

44
class DBExpr {
5-
/** @var string */
6-
private $expression;
7-
85
/**
96
* @param string $expression
107
*/
11-
public function __construct(string $expression) {
12-
$this->expression = $expression;
13-
}
8+
public function __construct(
9+
private string $expression
10+
) {}
1411

1512
/**
1613
* @return string

src/Builder/Expr/DBExprFilter.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
use RuntimeException;
66

77
class DBExprFilter implements OptionalExpression {
8-
/** @var mixed */
9-
private $expression;
108
/** @var bool */
119
private $hasValue;
1210
/** @var mixed */
1311
private $value;
14-
/** @var string[] */
15-
private $keyPath;
1612
/** @var null|callable(mixed): bool */
1713
private $validator;
1814
/** @var callable(bool, array{key: mixed, value: string}) */
1915
private $validationResultHandler;
16+
/** @var string[] */
17+
private array $keyPath;
2018

2119
/**
2220
* @param string $expression
@@ -25,8 +23,13 @@ class DBExprFilter implements OptionalExpression {
2523
* @param callable|null $validator
2624
* @param callable|null $validationResultHandler
2725
*/
28-
public function __construct(string $expression, array $data, $keyPath, $validator = null, $validationResultHandler = null) {
29-
$this->expression = $expression;
26+
public function __construct(
27+
private string $expression,
28+
array $data,
29+
string|array $keyPath,
30+
$validator = null,
31+
$validationResultHandler = null
32+
) {
3033
$this->keyPath = $this->buildKey($keyPath);
3134
$this->value = RecursiveStructureAccess::recursiveGet($data, $this->keyPath, null);
3235
$this->hasValue = is_scalar($this->value) ? trim((string) $this->value) !== '' : !empty($this->value);

src/Builder/Expr/OptionalDBFilterMap.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
namespace Kir\MySQL\Builder\Expr;
33

44
class OptionalDBFilterMap {
5-
/** @var array<string, mixed> */
6-
private array $map;
7-
85
/**
96
* @param array<string, mixed> $map
107
*/
11-
final public function __construct(array $map) {
12-
$this->map = $map;
13-
}
8+
final public function __construct(
9+
private array $map
10+
) {}
1411

1512
/**
1613
* @param array<string, mixed> $map

src/Builder/Expr/RequiredDBFilterMap.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
use Kir\MySQL\Builder\Helpers\RecursiveStructureAccess;
55

66
class RequiredDBFilterMap {
7-
/** @var array<string, mixed> */
8-
private $map;
9-
107
/**
118
* @param array<string, mixed> $map
129
*/
13-
final public function __construct(array $map) {
14-
$this->map = $map;
15-
}
10+
final public function __construct(
11+
private array $map
12+
) {}
1613

1714
/**
1815
* @param array<string, mixed> $map

src/Builder/Internal/DDLRunnable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
* @template T
88
*/
99
class DDLRunnable {
10-
/** @var DatabaseStatement */
11-
private $query;
1210
/** @var null|callable(scalar=): T */
1311
private $callbackFn;
1412

1513
/**
1614
* @param DatabaseStatement $query
1715
* @param null|callable(scalar=): T $callbackFn
1816
*/
19-
public function __construct(DatabaseStatement $query, ?callable $callbackFn = null) {
20-
$this->query = $query;
17+
public function __construct(
18+
private DatabaseStatement $query,
19+
?callable $callbackFn = null
20+
) {
2121
$this->callbackFn = $callbackFn;
2222
}
2323

src/Builder/Internal/StatementInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface StatementInterface {
88
* @param Database $db
99
* @param array<string, mixed> $options
1010
*/
11-
public function __construct($db, array $options = []);
11+
public function __construct(Database $db, array $options = []);
1212
}

src/Builder/InvalidValueException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
namespace Kir\MySQL\Builder;
44

55
class InvalidValueException extends Exception {
6-
76
}

src/Builder/QueryStatement.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,24 @@
1010
use PDOStatement;
1111

1212
class QueryStatement implements DatabaseStatement {
13-
/** @var PDOStatement<mixed> */
14-
private $statement;
15-
/** @var QueryLoggers */
16-
private $queryLoggers;
17-
/** @var string */
18-
private $query;
19-
/** @var MySQLExceptionInterpreter */
20-
private $exceptionInterpreter;
21-
2213
/**
2314
* @param PDOStatement<mixed> $stmt
2415
* @param string $query
2516
* @param MySQLExceptionInterpreter $exceptionInterpreter
2617
* @param QueryLoggers $queryLoggers
2718
*/
28-
public function __construct(PDOStatement $stmt, string $query, MySQLExceptionInterpreter $exceptionInterpreter, QueryLoggers $queryLoggers) {
29-
$this->statement = $stmt;
30-
$this->queryLoggers = $queryLoggers;
31-
$this->query = $query;
32-
$this->exceptionInterpreter = $exceptionInterpreter;
33-
}
19+
public function __construct(
20+
private PDOStatement $stmt,
21+
private string $query,
22+
private MySQLExceptionInterpreter $exceptionInterpreter,
23+
private QueryLoggers $queryLoggers
24+
) {}
3425

3526
/**
3627
* @return PDOStatement<mixed>
3728
*/
3829
public function getStatement(): PDOStatement {
39-
return $this->statement;
30+
return $this->stmt;
4031
}
4132

4233
/**
@@ -53,7 +44,7 @@ public function setFetchMode(int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?array
5344
if($arg1 !== null) {
5445
$args[] = $arg1;
5546
}
56-
$this->statement->setFetchMode(...$args);
47+
$this->stmt->setFetchMode(...$args);
5748
return $this;
5849
}
5950

@@ -65,7 +56,7 @@ public function setFetchMode(int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?array
6556
public function execute(array $params = []) {
6657
$this->exceptionHandler(function() use ($params) {
6758
$this->queryLoggers->logRegion($this->query, function() use ($params) {
68-
$response = $this->statement->execute($params);
59+
$response = $this->stmt->execute($params);
6960
if(!$response) {
7061
throw new SqlException('Execution returned with "false".');
7162
}
@@ -83,9 +74,9 @@ public function execute(array $params = []) {
8374
public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []): array {
8475
$result = $x = $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
8576
if($fetchArgument !== null) {
86-
return $this->statement->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs);
77+
return $this->stmt->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs);
8778
}
88-
return $this->statement->fetchAll($fetchStyle);
79+
return $this->stmt->fetchAll($fetchStyle);
8980
});
9081
/** @var array<mixed, mixed>|false $x */
9182
if($x === false) {
@@ -101,29 +92,29 @@ public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null,
10192
* @return mixed
10293
*/
10394
public function fetch($fetchStyle = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
104-
return $this->exceptionHandler(fn() => $this->statement->fetch($fetchStyle, $cursorOrientation, $cursorOffset));
95+
return $this->exceptionHandler(fn() => $this->stmt->fetch($fetchStyle, $cursorOrientation, $cursorOffset));
10596
}
10697

10798
/**
10899
* @param int $columnNo
109100
* @return mixed
110101
*/
111102
public function fetchColumn($columnNo = 0) {
112-
return $this->exceptionHandler(fn() => $this->statement->fetchColumn($columnNo));
103+
return $this->exceptionHandler(fn() => $this->stmt->fetchColumn($columnNo));
113104
}
114105

115106
/**
116107
* @return bool
117108
*/
118109
public function closeCursor(): bool {
119-
return $this->exceptionHandler(fn() => $this->statement->closeCursor());
110+
return $this->exceptionHandler(fn() => $this->stmt->closeCursor());
120111
}
121112

122113
/**
123114
* @return int
124115
*/
125116
public function columnCount(): int {
126-
return $this->exceptionHandler(fn() => $this->statement->columnCount());
117+
return $this->exceptionHandler(fn() => $this->stmt->columnCount());
127118
}
128119

129120
/**
@@ -132,7 +123,7 @@ public function columnCount(): int {
132123
*/
133124
public function getColumnMeta(int $columnNo): ?array {
134125
return $this->exceptionHandler(function() use ($columnNo) {
135-
$columnMeta = $this->statement->getColumnMeta($columnNo);
126+
$columnMeta = $this->stmt->getColumnMeta($columnNo);
136127
if($columnMeta === false) {
137128
return null;
138129
}

src/Builder/RunnableSelect.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public function bindValue(string $key, $value);
3232
/**
3333
* @return $this
3434
*/
35-
public function clearValues();
35+
public function clearValues(): self;
3636

3737
/**
3838
* @param bool $preserveTypes
3939
* @return $this
4040
*/
41-
public function setPreserveTypes(bool $preserveTypes = true);
41+
public function setPreserveTypes(bool $preserveTypes = true): self;
4242

4343
/**
4444
* Fetches all rows using PDO::FETCH_NUM

src/Builder/Select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Select {
1616
* @param bool $preserveTypes
1717
* @return $this
1818
*/
19-
public function setPreserveTypes(bool $preserveTypes = true);
19+
public function setPreserveTypes(bool $preserveTypes = true): self;
2020

2121
/**
2222
* @param bool $enabled

0 commit comments

Comments
 (0)