Skip to content

Commit 94aa72a

Browse files
committed
refactor: enforce strict typing
Signed-off-by: mmalac <[email protected]>
1 parent 4bdb7e1 commit 94aa72a

File tree

81 files changed

+673
-716
lines changed

Some content is hidden

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

81 files changed

+673
-716
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ on:
66
branches:
77
tags:
88

9+
env:
10+
default_php: 8.2
11+
912
jobs:
1013
ci:
1114
uses: laminas/workflow-continuous-integration/.github/workflows/[email protected]
15+
16+
rector:
17+
runs-on: ubuntu-latest
18+
name: Run Rector on PHP
19+
steps:
20+
- uses: actions/[email protected]
21+
- uses: shivammathur/[email protected]
22+
with:
23+
php-version: ${{ env.default_php }}
24+
tools: composer
25+
- uses: ramsey/composer-install@v3
26+
- run: composer rector

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"laminas/laminas-servicemanager": "^4.0",
5959
"phpunit/phpunit": "^10.5",
6060
"psalm/plugin-phpunit": "^0.19.5",
61+
"rector/rector": "^2.1",
6162
"swoole/ide-helper": "^6.0",
6263
"vimeo/psalm": "^6.13"
6364
},
@@ -77,11 +78,16 @@
7778
"scripts": {
7879
"check": [
7980
"@cs-check",
81+
"@rector",
8082
"@test"
8183
],
8284
"cs-check": "phpcs",
8385
"cs-fix": "phpcbf",
86+
"rector": "rector -n -vv",
87+
"rector:fix": "rector -vv",
8488
"static-analysis": "psalm --shepherd --stats",
89+
"static-analysis:b": "psalm --update-baseline",
90+
"static-analysis:clear": "psalm --clear-cache",
8591
"test": "phpunit --colors=always",
8692
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
8793
},

composer.lock

Lines changed: 403 additions & 278 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<!-- Paths to check -->
1515
<file>src</file>
1616
<file>test</file>
17+
<file>rector.php</file>
1718

1819
<!-- Include all rules from Laminas Coding Standard -->
1920
<rule ref="LaminasCodingStandard" />

psalm-baseline.xml

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44
<MixedArrayOffset>
55
<code><![CDATA[$cacheControlDirectives[$regex]]]></code>
66
</MixedArrayOffset>
7-
<RiskyTruthyFalsyComparison>
8-
<code><![CDATA[$clearStatCacheInterval]]></code>
9-
</RiskyTruthyFalsyComparison>
10-
</file>
11-
<file src="src/Command/IsRunningTrait.php">
12-
<RiskyTruthyFalsyComparison>
13-
<code><![CDATA[$managerPid]]></code>
14-
<code><![CDATA[$masterPid]]></code>
15-
<code><![CDATA[$masterPid]]></code>
16-
</RiskyTruthyFalsyComparison>
177
</file>
188
<file src="src/Command/ReloadCommand.php">
199
<DeprecatedMethod>
@@ -64,14 +54,6 @@
6454
<PossiblyFalseIterator>
6555
<code><![CDATA[scandir($path)]]></code>
6656
</PossiblyFalseIterator>
67-
<RedundantCondition>
68-
<code><![CDATA[isArray]]></code>
69-
</RedundantCondition>
70-
</file>
71-
<file src="src/HttpServerFactory.php">
72-
<RiskyTruthyFalsyComparison>
73-
<code><![CDATA[$enableCoroutine]]></code>
74-
</RiskyTruthyFalsyComparison>
7557
</file>
7658
<file src="src/Log/AccessLogDataMap.php">
7759
<FalsableReturnStatement>
@@ -99,64 +81,6 @@
9981
<PossiblyNullReference>
10082
<code><![CDATA[getHeaderSize]]></code>
10183
</PossiblyNullReference>
102-
<RiskyTruthyFalsyComparison>
103-
<code><![CDATA[$port]]></code>
104-
<code><![CDATA[(string) $this->psrResponse->getBody()->getSize()]]></code>
105-
<code><![CDATA[getenv($name)]]></code>
106-
</RiskyTruthyFalsyComparison>
107-
</file>
108-
<file src="src/Log/AccessLogFormatter.php">
109-
<InvalidNullableReturnType>
110-
<code><![CDATA[string]]></code>
111-
<code><![CDATA[string]]></code>
112-
</InvalidNullableReturnType>
113-
<NullableReturnStatement>
114-
<code><![CDATA[preg_replace_callback(
115-
'#%(?:[<>])?([%aABbDfhHklLmpPqrRstTuUvVXIOS])#',
116-
static fn(array $matches) => match ($matches[1]) {
117-
'%' => '%',
118-
'a' => $map->getClientIp(),
119-
'A' => $map->getLocalIp(),
120-
'B' => $map->getBodySize('0'),
121-
'b' => $map->getBodySize('-'),
122-
'D' => $map->getRequestDuration('ms'),
123-
'f' => $map->getFilename(),
124-
'h' => $map->getRemoteHostname(),
125-
'H' => $map->getProtocol(),
126-
'm' => $map->getMethod(),
127-
'p' => $map->getPort('canonical'),
128-
'q' => $map->getQuery(),
129-
'r' => $map->getRequestLine(),
130-
's' => $map->getStatus(),
131-
't' => $map->getRequestTime('begin:%d/%b/%Y:%H:%M:%S %z'),
132-
'T' => $map->getRequestDuration('s'),
133-
'u' => $map->getRemoteUser(),
134-
'U' => $map->getPath(),
135-
'v' => $map->getHost(),
136-
'V' => $map->getServerName(),
137-
'I' => (string) ($map->getRequestMessageSize() ?? '-'),
138-
'O' => (string) ($map->getResponseMessageSize() ?? '-'),
139-
'S' => $map->getTransferredSize(),
140-
default => '-',
141-
},
142-
$format
143-
)]]></code>
144-
<code><![CDATA[preg_replace_callback(
145-
'#%(?:[<>])?{([^}]+)}([aCeinopPtT])#',
146-
static fn(array $matches) => match ($matches[2]) {
147-
'a' => $map->getClientIp(),
148-
'C' => $map->getCookie($matches[1]),
149-
'e' => $map->getEnv($matches[1]),
150-
'i' => $map->getRequestHeader($matches[1]),
151-
'o' => $map->getResponseHeader($matches[1]),
152-
'p' => $map->getPort($matches[1]),
153-
't' => $map->getRequestTime($matches[1]),
154-
'T' => $map->getRequestDuration($matches[1]),
155-
default => '-',
156-
},
157-
$format
158-
)]]></code>
159-
</NullableReturnStatement>
16084
</file>
16185
<file src="src/Log/Psr3AccessLogDecorator.php">
16286
<MissingParamType>
@@ -195,9 +119,6 @@
195119
<MixedPropertyTypeCoercion>
196120
<code><![CDATA[$middleware]]></code>
197121
</MixedPropertyTypeCoercion>
198-
<RiskyTruthyFalsyComparison>
199-
<code><![CDATA[! $filename]]></code>
200-
</RiskyTruthyFalsyComparison>
201122
</file>
202123
<file src="src/StaticResourceHandler.php">
203124
<MixedOperand>
@@ -214,9 +135,6 @@
214135
<MixedPropertyTypeCoercion>
215136
<code><![CDATA[$cacheControlDirectives]]></code>
216137
</MixedPropertyTypeCoercion>
217-
<RiskyTruthyFalsyComparison>
218-
<code><![CDATA[$cacheControl]]></code>
219-
</RiskyTruthyFalsyComparison>
220138
</file>
221139
<file src="src/StaticResourceHandler/ETagMiddleware.php">
222140
<ArgumentTypeCoercion>
@@ -228,11 +146,6 @@
228146
<PossiblyFalseArgument>
229147
<code><![CDATA[$etag]]></code>
230148
</PossiblyFalseArgument>
231-
<RiskyTruthyFalsyComparison>
232-
<code><![CDATA[$ifMatch]]></code>
233-
<code><![CDATA[filemtime($filename)]]></code>
234-
<code><![CDATA[filesize($filename)]]></code>
235-
</RiskyTruthyFalsyComparison>
236149
<UnusedProperty>
237150
<code><![CDATA[$allowedETagValidationTypes]]></code>
238151
</UnusedProperty>
@@ -269,17 +182,11 @@
269182
<ArgumentTypeCoercion>
270183
<code><![CDATA[$regexp]]></code>
271184
</ArgumentTypeCoercion>
272-
<RiskyTruthyFalsyComparison>
273-
<code><![CDATA[filemtime($filename)]]></code>
274-
</RiskyTruthyFalsyComparison>
275185
</file>
276186
<file src="src/StaticResourceHandler/StaticResourceResponse.php">
277187
<PossiblyFalsePropertyAssignmentValue>
278188
<code><![CDATA[filesize($filename)]]></code>
279189
</PossiblyFalsePropertyAssignmentValue>
280-
<RiskyTruthyFalsyComparison>
281-
<code><![CDATA[$responseContentCallback]]></code>
282-
</RiskyTruthyFalsyComparison>
283190
</file>
284191
<file src="src/StaticResourceHandler/ValidateRegexTrait.php">
285192
<ArgumentTypeCoercion>
@@ -295,10 +202,6 @@
295202
<PossiblyNullReference>
296203
<code><![CDATA[asString]]></code>
297204
</PossiblyNullReference>
298-
<RiskyTruthyFalsyComparison>
299-
<code><![CDATA[$cookie->getDomain()]]></code>
300-
<code><![CDATA[$cookie->getPath()]]></code>
301-
</RiskyTruthyFalsyComparison>
302205
</file>
303206
<file src="src/SwooleRequestHandlerRunner.php">
304207
<PossiblyUnusedReturnValue>
@@ -319,11 +222,6 @@
319222
<PossiblyUnusedReturnValue>
320223
<code><![CDATA[int]]></code>
321224
</PossiblyUnusedReturnValue>
322-
<RiskyTruthyFalsyComparison>
323-
<code><![CDATA[$key]]></code>
324-
<code><![CDATA[$this->body]]></code>
325-
<code><![CDATA[$this->request->rawContent()]]></code>
326-
</RiskyTruthyFalsyComparison>
327225
</file>
328226
<file src="src/Task/DeferredListenerDelegator.php">
329227
<UnusedParam>

rector.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
7+
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
8+
9+
return RectorConfig::configure()
10+
->withPhpSets(php82: true)
11+
->withPaths([
12+
__DIR__ . '/src',
13+
__DIR__ . '/test',
14+
])
15+
->withPreparedSets(
16+
typeDeclarations: true,
17+
)
18+
->withSkip([
19+
__DIR__ . '/test/TestAsset',
20+
// $this->httpServer->on('managerstart', [$this, 'onManagerStart']);
21+
// vs
22+
// $this->httpServer->on('managerstart', $this->onManagerStart(...)
23+
// is not the same
24+
FirstClassCallableRector::class => [
25+
__DIR__ . '/src/SwooleRequestHandlerRunner.php',
26+
__DIR__ . '/test/SwooleRequestHandlerRunnerTest.php',
27+
__DIR__ . '/test/Task/TaskTest.php',
28+
],
29+
// Rector try to set callable as Closure. These are not the same — Closure is a subtype of callable.
30+
TypedPropertyFromStrictSetUpRector::class => [
31+
__DIR__ . '/test/StaticResourceHandler/LastModifiedMiddlewareTest.php',
32+
__DIR__ . '/test/StaticResourceHandler/HeadMiddlewareTest.php',
33+
__DIR__ . '/test/StaticResourceHandler/GzipMiddlewareTest.php',
34+
__DIR__ . '/test/StaticResourceHandler/ETagMiddlewareTest.php',
35+
],
36+
]);

src/AbstractStaticResourceHandlerFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
abstract class AbstractStaticResourceHandlerFactory
2525
{
26-
/** @return StaticResourceHandlerInterface */
27-
abstract public function __invoke(ContainerInterface $container);
26+
abstract public function __invoke(ContainerInterface $container): StaticResourceHandlerInterface;
2827

2928
/**
3029
* Prepare the list of middleware based on configuration provided.
@@ -70,7 +69,7 @@ protected function configureMiddleware(array $config): array
7069
}
7170

7271
$clearStatCacheInterval = $config['clearstatcache-interval'] ?? false;
73-
if ($clearStatCacheInterval) {
72+
if ($clearStatCacheInterval !== false) {
7473
$middleware[] = new ClearStatCacheMiddleware((int) $clearStatCacheInterval);
7574
}
7675

src/Command/IsRunningTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public function isRunning(): bool
3030

3131
[$masterPid, $managerPid] = array_pad($pids, 2, null);
3232

33-
if ($managerPid) {
33+
if ($managerPid !== null) {
3434
// Swoole process mode
35-
return $masterPid && SwooleProcess::kill((int) $managerPid, 0);
35+
return $masterPid !== null && SwooleProcess::kill((int) $managerPid, 0);
3636
}
3737

3838
// Swoole base mode, no manager process
39-
return $masterPid && SwooleProcess::kill((int) $masterPid, 0);
39+
return $masterPid !== null && SwooleProcess::kill((int) $masterPid, 0);
4040
}
4141
}

src/Command/ReloadCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ReloadCommand extends Command
4242
*/
4343
public static $defaultName = 'mezzio:swoole:reload';
4444

45-
public function __construct(private int $serverMode)
45+
public function __construct(private readonly int $serverMode)
4646
{
4747
parent::__construct();
4848
}

src/Event/AbstractTaskEvent.php

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

1111
abstract class AbstractTaskEvent extends AbstractServerAwareEvent
1212
{
13-
/** @var mixed */
14-
protected $data;
13+
protected mixed $data;
1514

1615
protected int $taskId;
1716

@@ -20,8 +19,7 @@ public function getTaskId(): int
2019
return $this->taskId;
2120
}
2221

23-
/** @return mixed */
24-
public function getData()
22+
public function getData(): mixed
2523
{
2624
return $this->data;
2725
}

0 commit comments

Comments
 (0)