Skip to content

Commit dede7b9

Browse files
authored
[phpstan] Resolve couple PHPStan errors (#29)
* misc * phsptan fixes
1 parent 3adffe1 commit dede7b9

8 files changed

Lines changed: 54 additions & 73 deletions

File tree

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"clue/ndjson-react": "^1.3",
1717
"composer/pcre": "^3.4",
1818
"composer/xdebug-handler": "^3.0.5",
19-
"entropy/entropy": "^0.4",
19+
"entropy/entropy": "^0.4.6",
2020
"fidry/cpu-core-counter": "^1.3",
21-
"friendsofphp/php-cs-fixer": "^3.95.5",
21+
"friendsofphp/php-cs-fixer": "^3.95.10",
2222
"nette/utils": "^4.1",
2323
"react/child-process": "^0.6.7",
2424
"react/event-loop": "^1.6",
@@ -37,7 +37,7 @@
3737
"rector/jack": "^1.0",
3838
"rector/rector": "^2.4",
3939
"rector/type-perfect": "^2.1",
40-
"symplify/phpstan-rules": "^14.11",
40+
"symplify/phpstan-rules": "^14.12",
4141
"symplify/vendor-patches": "^11.5",
4242
"tomasvotruba/class-leak": "^2.1.7",
4343
"tomasvotruba/type-coverage": "^2.2",

packages/coding-standard/src/Fixer/LineLength/LineLengthFixer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
/**
2727
* @see \Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\LineLengthFixerTest
2828
* @see \Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\ConfiguredLineLengthFixerTest
29+
*
30+
* @implements ConfigurableFixerInterface<
31+
* array{
32+
* self::LINE_LENGTH?: int,
33+
* self::BREAK_LONG_LINES?: bool,
34+
* self::INLINE_SHORT_LINES?: bool
35+
* }, array{
36+
* self::LINE_LENGTH: int,
37+
* self::BREAK_LONG_LINES: bool,
38+
* self::INLINE_SHORT_LINES: bool
39+
* }
40+
* >
2941
*/
3042
final class LineLengthFixer extends AbstractSymplifyFixer implements ConfigurableFixerInterface
3143
{

phpstan.neon

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
parameters:
22
level: 8
33

4-
# reportUnmatchedIgnoredErrors: false
5-
6-
# requires exact closure types
7-
checkMissingCallableSignature: true
8-
9-
# symplify - see https://github.com/symplify/phpstan-rules#usage
10-
symplify:
11-
pathStrings: true
12-
134
paths:
145
- packages
156
- src
@@ -23,6 +14,10 @@ parameters:
2314
- '*/Source/*'
2415
- '*/Fixture/*'
2516

17+
# see https://github.com/symplify/phpstan-rules#usage
18+
symplify:
19+
pathStrings: true
20+
2621
# see https://github.com/tomasVotruba/unused-public
2722
unused_public:
2823
methods: true
@@ -32,23 +27,20 @@ parameters:
3227
# see https://github.com/TomasVotruba/type-coverage
3328
type_coverage:
3429
return: 99
35-
param: 94.4
30+
param: 99
3631
property: 99
3732

3833
bootstrapFiles:
3934
- tests/bootstrap.php
4035

41-
treatPhpDocTypesAsCertain: true
36+
errorFormat: symplify
37+
treatPhpDocTypesAsCertain: false
4238

4339
ignoreErrors:
44-
# set above
45-
-
46-
path: src/Parallel/Application/ParallelFileProcessor.php
47-
message: '#Cannot call method (.*?)\(\) on Symplify\\EasyCodingStandard\\Parallel\\ValueObject\\ProcessPool\|null#'
48-
40+
# @todo revisit: split return into typed file_diffs/coding_standard_errors buckets
4941
- '#Method Symplify\\EasyCodingStandard\\Application\\SingleFileProcessor\:\:processFilePath\(\) should return array\{file_diffs\?\: array<Symplify\\EasyCodingStandard\\ValueObject\\Error\\FileDiff>, coding_standard_errors\?\: array<Symplify\\EasyCodingStandard\\SniffRunner\\ValueObject\\Error\\CodingStandardError>\} but returns array<(.*?), array<Symplify\\EasyCodingStandard\\SniffRunner\\ValueObject\\Error\\CodingStandardError\|Symplify\\EasyCodingStandard\\ValueObject\\Error\\FileDiff>>#'
5042

51-
# false positive on custom config tets
43+
# on purpose to override a config
5244
-
5345
message: '#Missing call to parent\:\:setUp\(\) method#'
5446
paths:
@@ -57,43 +49,16 @@ parameters:
5749
- tests/Skipper/SkipCriteriaResolver/SkippedPathsResolver/SkippedPathsResolverTest.php
5850
- src/Testing/PHPUnit/AbstractCheckerTestCase.php
5951

60-
# testing instance of on purpose
61-
-
62-
message: '#Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf#'
63-
path: tests/*
64-
65-
# overly detailed
66-
- '#PHPDoc tag @var with type string\|false is not subtype of native type non\-empty\-string\|false#'
67-
68-
# array validation on purpose
69-
- '#Call to static method Webmozart\\Assert\\Assert\:\:allString\(\) with (non-empty-array|list|array)<string> will always evaluate to true#'
70-
- '#Call to static method Webmozart\\Assert\\Assert\:\:allIsArray\(\) with array<class\-string<PHP_CodeSniffer\\Sniffs\\Sniff\|PhpCsFixer\\Fixer\\FixerInterface>, array<mixed>> will always evaluate to true#'
71-
72-
# hack to autoload contants
52+
# intentional: hack to autoload contants
7353
- '#Call to new PHP_CodeSniffer\\Util\\Tokens\(\) on a separate line has no effect#'
7454

75-
# php version condition
76-
-
77-
identifier: smaller.alwaysFalse
78-
path: src/Configuration/ConfigInitializer.php
79-
80-
# false positive
81-
-
82-
identifier: offsetAssign.dimType
83-
path: src/Console/Output/JsonOutputFormatter.php
84-
8555
# coding-standard: runtime-defined PHP_CodeSniffer/php-cs-fixer token constants
8656
- '#Constant T_OPEN_CURLY_BRACKET|T_START_NOWDOC not found#'
8757

88-
# coding-standard: php-cs-fixer interface generics intentionally left unspecified
89-
- '#Class (.*?) implements generic interface PhpCsFixer\\Fixer\\ConfigurableFixerInterface but does not specify its types\: TFixerInputConfig, TFixerComputedConfig#'
90-
9158
# coding-standard: intentional cross-version condition
9259
-
9360
message: '#Comparison operation ">\=" between int<\d+, \d+> and (.*?) is always true#'
9461
path: packages/coding-standard/src/TokenAnalyzer/DocblockRelatedParamNamesResolver.php
95-
96-
# coding-standard: intentional null removal
9762
-
98-
message: '#Parameter \#1 \$array \(array<int, PhpCsFixer\\Tokenizer\\Token>\) to function array_filter does not contain falsy values, the array will always stay the same#'
99-
path: packages/coding-standard/src/TokenRunner/Traverser/TokenReverser.php
63+
identifier: smaller.alwaysFalse
64+
path: src/Configuration/ConfigInitializer.php

src/Console/Output/JsonOutputFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function createJsonContent(ErrorAndDiffResult $errorAndDiffResult, bool $
7676
}
7777

7878
/**
79-
* @return array{totals: array{errors: int, diffs: int}, files: string[]}
79+
* @return array{totals: array{errors: int, diffs: int}, files: array<string, array<string, list<array<string, mixed>>>>}
8080
*/
8181
private function createBaseErrorsJson(ErrorAndDiffResult $errorAndDiffResult): array
8282
{

src/Parallel/Application/ParallelFileProcessor.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@
3434
*
3535
* https://github.com/phpstan/phpstan-src/commit/b84acd2e3eadf66189a64fdbc6dd18ff76323f67#diff-7f625777f1ce5384046df08abffd6c911cfbb1cfc8fcb2bdeaf78f337689e3e2R150
3636
*/
37-
final class ParallelFileProcessor
37+
final readonly class ParallelFileProcessor
3838
{
3939
private const int SYSTEM_ERROR_LIMIT = 50;
4040

41-
private ProcessPool|null $processPool = null;
42-
4341
public function __construct(
44-
private readonly WorkerCommandLineFactory $workerCommandLineFactory,
42+
private WorkerCommandLineFactory $workerCommandLineFactory,
4543
) {
4644
}
4745

@@ -70,24 +68,32 @@ public function check(
7068
$systemErrors = [];
7169

7270
$tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);
73-
$this->processPool = new ProcessPool($tcpServer);
71+
$processPool = new ProcessPool($tcpServer);
7472

75-
$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (&$jobs): void {
73+
$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (
74+
&$jobs,
75+
$processPool
76+
): void {
7677
$inDecoder = new Decoder($connection, true, 512, 0, 4 * 1024 * 1024);
7778
$outEncoder = new Encoder($connection);
7879

79-
$inDecoder->on(ReactEvent::DATA, function (array $data) use (&$jobs, $inDecoder, $outEncoder): void {
80+
$inDecoder->on(ReactEvent::DATA, function (array $data) use (
81+
&$jobs,
82+
$inDecoder,
83+
$outEncoder,
84+
$processPool
85+
): void {
8086
$action = $data[ReactCommand::ACTION];
8187
if ($action !== Action::HELLO) {
8288
return;
8389
}
8490

8591
$processIdentifier = $data[Option::PARALLEL_IDENTIFIER];
86-
$parallelProcess = $this->processPool->getProcess($processIdentifier);
92+
$parallelProcess = $processPool->getProcess($processIdentifier);
8793
$parallelProcess->bindConnection($inDecoder, $outEncoder);
8894

8995
if ($jobs === []) {
90-
$this->processPool->quitProcess($processIdentifier);
96+
$processPool->quitProcess($processIdentifier);
9197
return;
9298
}
9399

@@ -112,13 +118,14 @@ public function check(
112118
$handleErrorCallable = function (Throwable $throwable) use (
113119
&$systemErrors,
114120
&$systemErrorsCount,
115-
&$reachedSystemErrorsCountLimit
121+
&$reachedSystemErrorsCountLimit,
122+
$processPool
116123
): void {
117124
$systemErrors[] = new SystemError($throwable->getLine(), $throwable->getMessage(), $throwable->getFile());
118125

119126
++$systemErrorsCount;
120127
$reachedSystemErrorsCountLimit = true;
121-
$this->processPool->quitAll();
128+
$processPool->quitAll();
122129
};
123130

124131
$timeoutInSeconds = SimpleParameterProvider::getIntParameter(Option::PARALLEL_TIMEOUT_IN_SECONDS);
@@ -161,7 +168,8 @@ function (array $json) use (
161168
$postFileCallback,
162169
&$systemErrorsCount,
163170
&$reachedInternalErrorsCountLimit,
164-
$processIdentifier
171+
$processIdentifier,
172+
$processPool
165173
): void {
166174
// decode arrays to objects
167175
foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) {
@@ -186,11 +194,11 @@ function (array $json) use (
186194
$systemErrorsCount += $json[Bridge::SYSTEM_ERRORS_COUNT];
187195
if ($systemErrorsCount >= self::SYSTEM_ERROR_LIMIT) {
188196
$reachedInternalErrorsCountLimit = true;
189-
$this->processPool->quitAll();
197+
$processPool->quitAll();
190198
}
191199

192200
if ($jobs === []) {
193-
$this->processPool->quitProcess($processIdentifier);
201+
$processPool->quitProcess($processIdentifier);
194202
return;
195203
}
196204

@@ -205,8 +213,8 @@ function (array $json) use (
205213
$handleErrorCallable,
206214

207215
// 3. callable on exit
208-
function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): void {
209-
$this->processPool->tryQuitProcess($processIdentifier);
216+
function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier, $processPool): void {
217+
$processPool->tryQuitProcess($processIdentifier);
210218
if ($exitCode === ExitCode::SUCCESS) {
211219
return;
212220
}
@@ -219,7 +227,7 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
219227
}
220228
);
221229

222-
$this->processPool->attachProcess($processIdentifier, $parallelProcess);
230+
$processPool->attachProcess($processIdentifier, $parallelProcess);
223231
}
224232

225233
$streamSelectLoop->run();

src/Skipper/FileSystem/FnMatchPathNormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function normalizeForFnmatch(string $path): string
1616
}
1717

1818
if (\str_contains($path, '..')) {
19-
/** @var string|false $realPath */
2019
$realPath = realpath($path);
2120
if ($realPath === false) {
2221
return '';

src/Skipper/RealpathMatcher.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ final class RealpathMatcher
88
{
99
public function match(string $matchingPath, string $filePath): bool
1010
{
11-
/** @var string|false $realPathMatchingPath */
11+
/** @var non-empty-string|false $realPathMatchingPath */
1212
$realPathMatchingPath = realpath($matchingPath);
1313
if ($realPathMatchingPath === false) {
1414
return false;
1515
}
1616

17-
/** @var string|false $realpathFilePath */
1817
$realpathFilePath = realpath($filePath);
1918
if ($realpathFilePath === false) {
2019
return false;

tests/SniffRunner/File/FileFactoryTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Symplify\EasyCodingStandard\Tests\SniffRunner\File;
66

7-
use PHP_CodeSniffer\Files\File as PhpCodeSnifferFile;
87
use PHP_CodeSniffer\Fixer;
98
use Symplify\EasyCodingStandard\SniffRunner\File\FileFactory;
109
use Symplify\EasyCodingStandard\SniffRunner\ValueObject\File;
@@ -20,7 +19,6 @@ public function test(): void
2019
$file = $fileFactory->createFromFile($filePath);
2120

2221
$this->assertInstanceOf(File::class, $file);
23-
$this->assertInstanceOf(PhpCodeSnifferFile::class, $file);
2422
$this->assertInstanceOf(Fixer::class, $file->fixer);
2523
$this->assertSame($filePath, $file->getFilename());
2624
}

0 commit comments

Comments
 (0)