Skip to content

Commit 14956b6

Browse files
committed
Enhanced typing
1 parent 0570dc4 commit 14956b6

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

src/Builder/Insert.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
2+
23
namespace Kir\MySQL\Builder;
34

4-
use DateTimeInterface;
5+
use Kir\MySQL\Builder\Internal\Types;
56
use Kir\MySQL\Tools\AliasReplacer;
67
use RuntimeException;
78
use Traversable;
89
use UnexpectedValueException;
910

11+
/**
12+
* @phpstan-import-type DBParameterValueType from Types
13+
*/
1014
abstract class Insert extends InsertUpdateStatement {
11-
/** @var array<string|int, mixed> */
12-
private $fields = [];
13-
/** @var array<string|int, mixed> */
14-
private $update = [];
15-
/** @var string */
16-
private $table;
17-
/** @var string */
18-
private $keyField;
19-
/** @var bool */
20-
private $ignore = false;
21-
/** @var Select */
22-
private $from;
15+
/** @var array<string|int, DBParameterValueType> */
16+
private array $fields = [];
17+
/** @var array<string|int, DBParameterValueType> */
18+
private array $update = [];
19+
private ?string $table = null;
20+
private ?string $keyField = null;
21+
private bool $ignore = false;
22+
private ?Select $from = null;
2323

2424
/**
2525
* @param string $table
@@ -54,7 +54,7 @@ public function setKey(string $field) {
5454

5555
/**
5656
* @param string $field
57-
* @param null|bool|int|float|string|DateTimeInterface $value
57+
* @param DBParameterValueType $value
5858
* @return $this
5959
*/
6060
public function add(string $field, $value) {
@@ -64,7 +64,7 @@ public function add(string $field, $value) {
6464

6565
/**
6666
* @param string $field
67-
* @param null|bool|int|float|string|DateTimeInterface $value
67+
* @param DBParameterValueType $value
6868
* @return $this
6969
*/
7070
public function update(string $field, $value) {
@@ -74,7 +74,7 @@ public function update(string $field, $value) {
7474

7575
/**
7676
* @param string $field
77-
* @param null|bool|int|float|string|DateTimeInterface $value
77+
* @param DBParameterValueType $value
7878
* @return $this
7979
*/
8080
public function addOrUpdate(string $field, $value) {
@@ -85,7 +85,7 @@ public function addOrUpdate(string $field, $value) {
8585

8686
/**
8787
* @param string $str
88-
* @param mixed ...$args
88+
* @param DBParameterValueType ...$args
8989
* @return $this
9090
*/
9191
public function addExpr(string $str, ...$args) {
@@ -99,7 +99,7 @@ public function addExpr(string $str, ...$args) {
9999

100100
/**
101101
* @param string $str
102-
* @param mixed ...$args
102+
* @param DBParameterValueType ...$args
103103
* @return $this
104104
*/
105105
public function updateExpr(string $str, ...$args) {
@@ -113,7 +113,7 @@ public function updateExpr(string $str, ...$args) {
113113

114114
/**
115115
* @param string $expr
116-
* @param mixed ...$args
116+
* @param DBParameterValueType ...$args
117117
* @return $this
118118
*/
119119
public function addOrUpdateExpr(string $expr, ...$args) {
@@ -128,7 +128,7 @@ public function addOrUpdateExpr(string $expr, ...$args) {
128128
}
129129

130130
/**
131-
* @param array<string, null|bool|int|float|string|DateTimeInterface> $data
131+
* @param array<string, DBParameterValueType> $data
132132
* @param null|string[] $mask
133133
* @param null|string[] $excludeFields
134134
* @return $this
@@ -141,7 +141,7 @@ public function addAll(array $data, ?array $mask = null, ?array $excludeFields =
141141
}
142142

143143
/**
144-
* @param array<string, null|bool|int|float|string|DateTimeInterface> $data
144+
* @param array<string, DBParameterValueType> $data
145145
* @param null|string[] $mask
146146
* @param null|string[] $excludeFields
147147
* @return $this
@@ -156,7 +156,7 @@ public function updateAll(array $data, ?array $mask = null, ?array $excludeField
156156
}
157157

158158
/**
159-
* @param array<string, null|bool|int|float|string|DateTimeInterface> $data
159+
* @param array<string, DBParameterValueType> $data
160160
* @param null|string[] $mask
161161
* @param array<int, string>|null $excludeFields
162162
* @return $this
@@ -226,7 +226,7 @@ public function __toString(): string {
226226
/**
227227
* @param array<string|int, mixed> $fields
228228
* @param string $field
229-
* @param null|bool|int|float|string|DateTimeInterface $value
229+
* @param DBParameterValueType $value
230230
* @return array<string|int, mixed>
231231
*/
232232
private function addTo(array $fields, string $field, $value): array {
@@ -241,8 +241,8 @@ private function addTo(array $fields, string $field, $value): array {
241241

242242
/**
243243
* @param array<string, mixed> $data
244-
* @param array<int, string>|null $mask
245-
* @param array<int, string>|null $excludeFields
244+
* @param string[]|null $mask
245+
* @param string[]|null $excludeFields
246246
* @param callable(string, mixed): void $fn
247247
*/
248248
private function addAllTo(array $data, ?array $mask, ?array $excludeFields, $fn): void {

src/Builder/InsertUpdateStatement.php

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

44
use Kir\MySQL\Builder\Internal\DefaultValue;
5+
use Kir\MySQL\Builder\Internal\Types;
56

7+
/**
8+
* @phpstan-import-type DBParameterValueType from Types
9+
*/
610
abstract class InsertUpdateStatement extends Statement {
711
/** @var array<int, string> */
8-
private $mask;
12+
private ?array $mask = null;
913

1014
/**
1115
* @return array<int, string>|null
@@ -24,7 +28,7 @@ public function setMask(array $mask) {
2428
}
2529

2630
/**
27-
* @param array<int|string, null|string|array<int, string>|DBExpr|Select|DefaultValue> $fields
31+
* @param array<int|string, DBParameterValueType> $fields
2832
* @param array<int, string> $query
2933
* @return string[]
3034
*/
@@ -38,14 +42,18 @@ protected function buildFieldList(array $fields, array $query = []): array {
3842
}
3943
if(is_int($fieldName)) {
4044
if (is_array($fieldValue)) {
45+
// @phpstan-ignore-next-line
4146
$fieldValue = $this->db()->quoteExpression($fieldValue[0], array_slice($fieldValue, 1));
4247
}
48+
// @phpstan-ignore-next-line
4349
$query[] = "\t{$fieldValue}";
4450
} else {
4551
$fieldName = $this->db()->quoteField($fieldName);
4652
if (is_array($fieldValue)) {
53+
// @phpstan-ignore-next-line
4754
$fieldValue = $this->db()->quoteExpression($fieldValue[0], array_slice($fieldValue, 1));
4855
}
56+
// @phpstan-ignore-next-line
4957
$query[] = "\t{$fieldName}={$fieldValue}";
5058
}
5159
}

src/Builder/Internal/Types.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @phpstan-type DBParameterValueType null|scalar|Stringable|DBExpr|Select|DateTimeInterface|array<null|scalar|Stringable>
1717
*
18-
* @phpstan-type DBWhereKeyValueExpressionType string|array<string, DBParameterValueType>|object|OptionalExpression
18+
* @phpstan-type DBWhereKeyValueExpressionType array<string, DBParameterValueType>
1919
* @phpstan-type DBWhereExpressionType string|DBWhereKeyValueExpressionType|object|OptionalExpression
2020
*/
2121
class Types {

src/Builder/Traits/HavingBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
trait HavingBuilder {
1313
use AbstractDB;
1414

15-
/** @var array<int, array{DBWhereExpressionType, array<DBParameterValueType>}> */
15+
/** @var array<int, array{DBWhereExpressionType, DBParameterValueType[]}> */
1616
private array $having = [];
1717

1818
/**

src/Builder/Traits/JoinBuilder.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
<?php
22
namespace Kir\MySQL\Builder\Traits;
33

4-
use Kir\MySQL\Builder\DBExpr;
4+
use Kir\MySQL\Builder\Internal\Types;
55
use Kir\MySQL\Builder\Select;
66
use Kir\MySQL\Tools\VirtualTable;
77

8+
/**
9+
* @phpstan-import-type DBParameterValueType from Types
10+
* @phpstan-import-type DBTableNameType from Types
11+
*/
812
trait JoinBuilder {
913
use AbstractDB;
1014
use AbstractTableNameBuilder;
1115

12-
/** @var array<int, array{type: string, alias: string, name: string|array<int, array<string, mixed>>|Select|VirtualTable, expression: string|null, arguments: array<int, null|string|array<int, string>|DBExpr|Select>}> */
13-
private $joinTables = [];
16+
/** @var array<int, array{type: string, alias: string, name: string|array<int, array<string, mixed>>|Select|VirtualTable, expression: string|null, arguments: array<int, DBParameterValueType}> */
17+
private array $joinTables = [];
1418

1519
/**
1620
* @param string $alias
17-
* @param string|array<int, array<string, mixed>>|Select|VirtualTable $table
21+
* @param DBTableNameType $table
1822
* @param string|null $expression
19-
* @param null|int|float|string|array<int, string>|DBExpr|Select ...$args
23+
* @param DBParameterValueType ...$args
2024
* @return $this
2125
*/
2226
public function joinInner(string $alias, $table, ?string $expression = null, ...$args) {
@@ -25,9 +29,9 @@ public function joinInner(string $alias, $table, ?string $expression = null, ...
2529

2630
/**
2731
* @param string $alias
28-
* @param string|array<int, array<string, mixed>>|Select|VirtualTable $table
32+
* @param DBTableNameType $table
2933
* @param string $expression
30-
* @param string|int|float|array<int, string>|DBExpr|Select ...$args
34+
* @param DBParameterValueType ...$args
3135
* @return $this
3236
*/
3337
public function joinLeft(string $alias, $table, string $expression, ...$args) {
@@ -36,9 +40,9 @@ public function joinLeft(string $alias, $table, string $expression, ...$args) {
3640

3741
/**
3842
* @param string $alias
39-
* @param string|array<int, array<string, mixed>>|Select|VirtualTable $table
43+
* @param DBTableNameType $table
4044
* @param string $expression
41-
* @param string|int|float|array<int, string>|DBExpr|Select ...$args
45+
* @param DBParameterValueType ...$args
4246
* @return $this
4347
*/
4448
public function joinRight(string $alias, $table, string $expression, ...$args) {
@@ -68,9 +72,9 @@ protected function buildJoins(string $query): string {
6872
/**
6973
* @param string $type
7074
* @param string $alias
71-
* @param string|array<int, array<string, mixed>>|Select|VirtualTable $name
75+
* @param DBTableNameType $name
7276
* @param string|null $expression
73-
* @param array<int, mixed> $arguments
77+
* @param array<DBParameterValueType> $arguments
7478
* @return $this
7579
*/
7680
private function addJoin(string $type, string $alias, $name, ?string $expression = null, array $arguments = []) {

src/Builder/Traits/WhereBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
trait WhereBuilder {
1515
use AbstractDB;
1616

17-
/** @var array<int, array{DBWhereExpressionType, array<DBParameterValueType>}> */
17+
/** @var array<int, array{DBWhereExpressionType, DBParameterValueType[]}> */
1818
private array $where = [];
1919

2020
/**
21-
* @param string|array<string, mixed>|object|OptionalExpression $expression
21+
* @param DBWhereExpressionType $expression
2222
* @param DBParameterValueType ...$args
2323
* @return $this
2424
*/

0 commit comments

Comments
 (0)