Skip to content

Commit 8c34085

Browse files
authored
Merge pull request #143 from mairo744/refactor/rector
refactor: enforce strict typing
2 parents 4bdb7e1 + 6246606 commit 8c34085

File tree

84 files changed

+686
-753
lines changed

Some content is hidden

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

84 files changed

+686
-753
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ 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+
extensions: swoole
25+
tools: composer
26+
- uses: ramsey/composer-install@v3
27+
- 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 & 113 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>
@@ -191,14 +115,6 @@
191115
<code><![CDATA[bool]]></code>
192116
</PossiblyUnusedReturnValue>
193117
</file>
194-
<file src="src/StaticMappedResourceHandler.php">
195-
<MixedPropertyTypeCoercion>
196-
<code><![CDATA[$middleware]]></code>
197-
</MixedPropertyTypeCoercion>
198-
<RiskyTruthyFalsyComparison>
199-
<code><![CDATA[! $filename]]></code>
200-
</RiskyTruthyFalsyComparison>
201-
</file>
202118
<file src="src/StaticResourceHandler.php">
203119
<MixedOperand>
204120
<code><![CDATA[$request->server['request_uri']]]></code>
@@ -214,9 +130,6 @@
214130
<MixedPropertyTypeCoercion>
215131
<code><![CDATA[$cacheControlDirectives]]></code>
216132
</MixedPropertyTypeCoercion>
217-
<RiskyTruthyFalsyComparison>
218-
<code><![CDATA[$cacheControl]]></code>
219-
</RiskyTruthyFalsyComparison>
220133
</file>
221134
<file src="src/StaticResourceHandler/ETagMiddleware.php">
222135
<ArgumentTypeCoercion>
@@ -228,11 +141,6 @@
228141
<PossiblyFalseArgument>
229142
<code><![CDATA[$etag]]></code>
230143
</PossiblyFalseArgument>
231-
<RiskyTruthyFalsyComparison>
232-
<code><![CDATA[$ifMatch]]></code>
233-
<code><![CDATA[filemtime($filename)]]></code>
234-
<code><![CDATA[filesize($filename)]]></code>
235-
</RiskyTruthyFalsyComparison>
236144
<UnusedProperty>
237145
<code><![CDATA[$allowedETagValidationTypes]]></code>
238146
</UnusedProperty>
@@ -269,17 +177,11 @@
269177
<ArgumentTypeCoercion>
270178
<code><![CDATA[$regexp]]></code>
271179
</ArgumentTypeCoercion>
272-
<RiskyTruthyFalsyComparison>
273-
<code><![CDATA[filemtime($filename)]]></code>
274-
</RiskyTruthyFalsyComparison>
275180
</file>
276181
<file src="src/StaticResourceHandler/StaticResourceResponse.php">
277182
<PossiblyFalsePropertyAssignmentValue>
278183
<code><![CDATA[filesize($filename)]]></code>
279184
</PossiblyFalsePropertyAssignmentValue>
280-
<RiskyTruthyFalsyComparison>
281-
<code><![CDATA[$responseContentCallback]]></code>
282-
</RiskyTruthyFalsyComparison>
283185
</file>
284186
<file src="src/StaticResourceHandler/ValidateRegexTrait.php">
285187
<ArgumentTypeCoercion>
@@ -295,10 +197,6 @@
295197
<PossiblyNullReference>
296198
<code><![CDATA[asString]]></code>
297199
</PossiblyNullReference>
298-
<RiskyTruthyFalsyComparison>
299-
<code><![CDATA[$cookie->getDomain()]]></code>
300-
<code><![CDATA[$cookie->getPath()]]></code>
301-
</RiskyTruthyFalsyComparison>
302200
</file>
303201
<file src="src/SwooleRequestHandlerRunner.php">
304202
<PossiblyUnusedReturnValue>
@@ -319,11 +217,6 @@
319217
<PossiblyUnusedReturnValue>
320218
<code><![CDATA[int]]></code>
321219
</PossiblyUnusedReturnValue>
322-
<RiskyTruthyFalsyComparison>
323-
<code><![CDATA[$key]]></code>
324-
<code><![CDATA[$this->body]]></code>
325-
<code><![CDATA[$this->request->rawContent()]]></code>
326-
</RiskyTruthyFalsyComparison>
327220
</file>
328221
<file src="src/Task/DeferredListenerDelegator.php">
329222
<UnusedParam>
@@ -360,12 +253,6 @@
360253
<code><![CDATA[assertAttributeEmpty]]></code>
361254
<code><![CDATA[assertAttributeEmpty]]></code>
362255
</PossiblyUnusedMethod>
363-
<PossiblyUnusedParam>
364-
<code><![CDATA[$canonicalize]]></code>
365-
<code><![CDATA[$delta]]></code>
366-
<code><![CDATA[$ignoreCase]]></code>
367-
<code><![CDATA[$maxDepth]]></code>
368-
</PossiblyUnusedParam>
369256
</file>
370257
<file src="test/Command/ReloadCommandTest.php">
371258
<DeprecatedMethod>

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/Command/StopCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class StopCommand extends Command
5959

6060
public function __construct(private PidManager $pidManager)
6161
{
62-
$this->killProcess = Closure::fromCallable([SwooleProcess::class, 'kill']);
62+
$this->killProcess = SwooleProcess::kill(...);
6363
parent::__construct();
6464
}
6565

0 commit comments

Comments
 (0)