Skip to content

Commit 71bad8b

Browse files
committed
chore(typehint): add tomasvotruba/type-coverage and more typehinting
1 parent 726e16f commit 71bad8b

21 files changed

Lines changed: 102 additions & 212 deletions

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
"symfony/uid": "^6.0 || ^7.0",
3737
"symfony/cache": "^6.0 || ^7.0",
3838
"phpstan/phpstan": "^2.0",
39-
"rector/rector": "^2.1"
39+
"rector/rector": "^2.1",
40+
"tomasvotruba/type-coverage": "^2.0",
41+
"phpstan/extension-installer": "^1.4",
42+
"rector/type-perfect": "^2.1"
4043
},
4144
"suggest": {
4245
"brick/geo": "Allow support of MariaDB Geometry type",
@@ -53,5 +56,10 @@
5356
"tests\\fixtures\\": "tests/fixtures/",
5457
"CCMBenchmark\\Ting\\PHPStan\\": "phpstan/"
5558
}
59+
},
60+
"config": {
61+
"allow-plugins": {
62+
"phpstan/extension-installer": true
63+
}
5664
}
5765
}

phpstan.neon.dist

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
includes:
2-
- phpstan-baseline.neon
2+
- phpstan-baseline.neon
33

44
parameters:
5-
phpVersion: 80100
6-
level: 7
7-
paths:
8-
- src/
9-
- phpstan/
10-
tips:
11-
treatPhpDocTypesAsCertain: false
12-
ignoreErrors:
13-
- identifier: missingType.iterableValue
14-
- identifier: missingType.generics
5+
phpVersion: 80100
6+
level: 7
7+
paths:
8+
- src/
9+
- phpstan/
10+
tips:
11+
treatPhpDocTypesAsCertain: false
12+
ignoreErrors:
13+
- identifier: missingType.iterableValue
14+
- identifier: missingType.generics
15+
- identifier: typePerfect.noArrayAccessOnObject
16+
type_coverage:
17+
return: 97
18+
param: 85
19+
# enable on PHP 7.4+
20+
property: 83
21+
# enable on PHP 8.3+
22+
# constant: 5
23+
type_perfect:
24+
null_over_false: true
25+
# no_mixed: true
26+
# narrow_param: true
27+
narrow_return: true
28+
1529
services:
16-
-
17-
class: CCMBenchmark\Ting\PHPStan\PropertiesExtension
18-
tags:
19-
- phpstan.properties.readWriteExtension
30+
- class: CCMBenchmark\Ting\PHPStan\PropertiesExtension
31+
tags:
32+
- phpstan.properties.readWriteExtension

src/Ting/ConnectionPool.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public function __construct(protected ?DriverLoggerInterface $logger = null)
5656
{
5757
}
5858

59-
/**
60-
* @param array $config
61-
*/
62-
public function setConfig($config): void
59+
public function setConfig(array $config): void
6360
{
6461
$this->connectionConfig = $config;
6562
}
@@ -95,12 +92,9 @@ public function master(string $name, string $database): DriverInterface
9592
/**
9693
* Return always the same slave connection
9794
*
98-
* @param string $name
99-
* @param string $database
100-
* @return DriverInterface
10195
* @throws ConnectionException
10296
*/
103-
public function slave($name, $database): DriverInterface
97+
public function slave(string $name, string $database): DriverInterface
10498
{
10599
if (isset($this->connectionConfig[$name]) === false) {
106100
throw new ConnectionException('Connection not found: ' . $name);
@@ -138,13 +132,9 @@ public function slave($name, $database): DriverInterface
138132
/**
139133
* @param array{host: string, port: int, user?: string, password?: string} $config
140134
* @param class-string<DriverInterface> $driverClass
141-
* @param string $database
142-
* @param string $name connection name
143-
* @param string $charset
144-
* @return DriverInterface
145135
* @throws Exception
146136
*/
147-
protected function connect($config, $driverClass, $database, $name, $charset = null): DriverInterface
137+
protected function connect(array $config, string $driverClass, string $database, string $name, ?string $charset = null): DriverInterface
148138
{
149139

150140
if (isset($config['user']) === false) {

src/Ting/ConnectionPoolInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ interface ConnectionPoolInterface
3535
*/
3636
public function __construct(DriverLoggerInterface $logger);
3737

38-
/**
39-
* @param array $config
40-
*/
41-
public function setConfig($config): void;
38+
public function setConfig(array $config): void;
4239

4340
/**
4441
* @throws Exception

src/Ting/Driver/CacheResult.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ class CacheResult implements ResultInterface
4040
*/
4141
protected ?string $objectToFetch = null;
4242

43-
/**
44-
* @param string $connectionName
45-
* @return $this
46-
*/
4743
public function setConnectionName(string $connectionName): static
4844
{
49-
$this->connectionName = (string) $connectionName;
45+
$this->connectionName = $connectionName;
5046
return $this;
5147
}
5248

src/Ting/Driver/DriverInterface.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,16 @@
3030

3131
interface DriverInterface
3232
{
33-
/**
34-
* @param string $hostname
35-
* @param string $username
36-
* @param string $password
37-
* @param int $port
38-
*/
39-
public function connect($hostname, $username, $password, $port): static;
33+
public function connect(string $hostname, string $username, string $password, int $port): static;
4034

4135
/**
4236
* Close the connection to the database
4337
*/
4438
public function close(): static;
4539

46-
/**
47-
* @param string $name
48-
*/
49-
public function setName($name): static;
40+
public function setName(string $name): static;
5041

51-
/**
52-
* @param string $charset
53-
* @return void
54-
*/
55-
public function setCharset($charset): void;
42+
public function setCharset(string $charset): void;
5643

5744
/**
5845
* @return ($collection is CollectionInterface ? CollectionInterface : bool|array|int|string)
@@ -67,10 +54,7 @@ public function execute(string $sql, array $params = [], ?CollectionInterface $c
6754
*/
6855
public function prepare(string $sql): StatementInterface;
6956

70-
/**
71-
* @param string $database
72-
*/
73-
public function setDatabase($database): static;
57+
public function setDatabase(string $database): static;
7458

7559
/**
7660
* @param callable $callback
@@ -97,11 +81,7 @@ public function getAffectedRows(): int|string;
9781

9882
public function setLogger(?DriverLoggerInterface $logger = null): static;
9983

100-
/**
101-
* @param array $connectionConfig
102-
* @param string $database
103-
*/
104-
public static function getConnectionKey(array $connectionConfig, $database): string;
84+
public static function getConnectionKey(array $connectionConfig, string $database): string;
10585

10686
/**
10787
* @param $statement

src/Ting/Driver/Mysqli/Driver.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ public function __construct($connection = null, $driver = null)
113113
$this->driver = $driver ?? new mysqli_driver();
114114
}
115115

116-
/**
117-
* @param array $connectionConfig
118-
* @param string $database
119-
*/
120-
public static function getConnectionKey(array $connectionConfig, $database): string
116+
public static function getConnectionKey(array $connectionConfig, string $database): string
121117
{
122118
return
123119
$connectionConfig['host'] . '|' .
@@ -127,14 +123,9 @@ public static function getConnectionKey(array $connectionConfig, $database): str
127123
}
128124

129125
/**
130-
* @param string $hostname
131-
* @param string $username
132-
* @param string $password
133-
* @param int $port
134-
*
135126
* @throws ConnectionException
136127
*/
137-
public function connect($hostname, $username, $password, $port = 3306): static
128+
public function connect(string $hostname, string $username, string $password, int $port = 3306): static
138129
{
139130
$this->driver->report_mode = MYSQLI_REPORT_STRICT;
140131

@@ -168,11 +159,9 @@ public function close(): static
168159
}
169160

170161
/**
171-
* @param string $charset
172-
* @return void
173162
* @throws DriverException
174163
*/
175-
public function setCharset($charset): void
164+
public function setCharset(string $charset): void
176165
{
177166
if ($this->currentCharset === $charset) {
178167
return;
@@ -195,23 +184,17 @@ public function setLogger(?DriverLoggerInterface $logger = null): static
195184
return $this;
196185
}
197186

198-
/**
199-
* @param string $name
200-
* @return $this
201-
*/
202-
public function setName($name): static
187+
public function setName(string $name): static
203188
{
204-
$this->name = (string) $name;
189+
$this->name = $name;
205190

206191
return $this;
207192
}
208193

209194
/**
210-
* @param string $database
211-
* @return $this
212195
* @throws DatabaseException
213196
*/
214-
public function setDatabase($database): static
197+
public function setDatabase(string $database): static
215198
{
216199
if ($this->currentDatabase === $database) {
217200
return $this;
@@ -290,9 +273,8 @@ function (array $match) use ($params) {
290273

291274
/**
292275
* Quote value according to the type of variable
293-
* @param mixed $value
294276
*/
295-
protected function quoteValue($value): string | int | float
277+
protected function quoteValue(mixed $value): string | int | float
296278
{
297279
return match (\gettype($value)) {
298280
"boolean" => (int) $value,

src/Ting/Driver/Pgsql/Driver.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ class Driver implements DriverInterface
7979
*/
8080
protected $dsn;
8181

82-
/**
83-
* Return a unique connection key identifier
84-
* @param array $connectionConfig
85-
* @param string $database
86-
*/
87-
public static function getConnectionKey(array $connectionConfig, $database): string
82+
public static function getConnectionKey(array $connectionConfig, string $database): string
8883
{
8984
return
9085
$connectionConfig['host'] . '|' .
@@ -96,13 +91,8 @@ public static function getConnectionKey(array $connectionConfig, $database): str
9691

9792
/**
9893
* Construct connection information
99-
* @param string $hostname
100-
* @param string $username
101-
* @param string $password
102-
* @param int $port
103-
* @return $this
10494
*/
105-
public function connect($hostname, $username, $password, $port): static
95+
public function connect(string $hostname, string $username, string $password, int $port): static
10696
{
10797
$this->dsn = 'host=' . $hostname . ' user=' . $username . ' password=' . $password . ' port=' . $port;
10898
return $this;
@@ -123,11 +113,9 @@ public function close(): static
123113
}
124114

125115
/**
126-
* @param string $charset
127-
* @return void
128116
* @throws DriverException
129117
*/
130-
public function setCharset($charset): void
118+
public function setCharset(string $charset): void
131119
{
132120
if ($this->currentCharset === $charset) {
133121
return;
@@ -140,11 +128,7 @@ public function setCharset($charset): void
140128
$this->currentCharset = $charset;
141129
}
142130

143-
/**
144-
* @param string $name
145-
* @return $this
146-
*/
147-
public function setName($name): static
131+
public function setName(string $name): static
148132
{
149133
$this->name = $name;
150134

@@ -153,11 +137,9 @@ public function setName($name): static
153137

154138
/**
155139
* Connect the driver to the given database
156-
* @param string $database
157-
* @return $this
158140
* @throws DriverException
159141
*/
160-
public function setDatabase($database): static
142+
public function setDatabase(string $database): static
161143
{
162144
if ($this->connection !== null) {
163145
return $this;

src/Ting/Driver/Pgsql/Result.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,23 @@ class Result implements ResultInterface
3939
protected ?string $database = null;
4040
/** @var \PgSql\Result|null */
4141
protected $result = null;
42+
/** @var array<int, stdClass> $fields */
4243
protected array $fields = [];
4344
protected int $iteratorOffset = 0;
4445
/** @var array|object|false|null */
4546
protected $iteratorCurrent = null;
4647
/** @var class-string|null */
4748
protected ?string $objectToFetch = null;
4849

49-
/**
50-
* @param string $connectionName
51-
* @return $this
52-
*/
5350
public function setConnectionName(string $connectionName): static
5451
{
55-
$this->connectionName = (string) $connectionName;
52+
$this->connectionName = $connectionName;
5653
return $this;
5754
}
5855

59-
/**
60-
* @param string $database
61-
* @return $this
62-
*/
63-
public function setDatabase($database): static
56+
public function setDatabase(string $database): static
6457
{
65-
$this->database = (string) $database;
58+
$this->database = $database;
6659
return $this;
6760
}
6861

src/Ting/Driver/SphinxQL/Driver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ class Driver extends Mysqli\Driver
3131
{
3232
/**
3333
* Quote value according to the type of variable
34-
* @param mixed $value
3534
*
3635
* @internal
3736
*/
38-
protected function quoteValue($value): int|float|string
37+
protected function quoteValue(mixed $value): int|float|string
3938
{
4039
return match (\gettype($value)) {
4140
"integer", "double" => $value,

0 commit comments

Comments
 (0)