Skip to content

Commit 7012894

Browse files
committed
refactor: apply rector
1 parent 3413928 commit 7012894

35 files changed

+264
-308
lines changed

.php-cs-fixer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
return Factory::fromRuleSet(Php81::create()
3838
->withHeader(
3939
(static function (): string {
40-
$license = MIT::text(
40+
$mit = MIT::text(
4141
__DIR__.'/LICENSE',
4242
Range::since(
4343
Year::fromString('2019'),
@@ -47,9 +47,9 @@
4747
Url::fromString('https://github.com/guanguans/soar-php'),
4848
);
4949

50-
$license->save();
50+
$mit->save();
5151

52-
return $license->header();
52+
return $mit->header();
5353
})()
5454
)
5555
->withCustomFixers(Fixers::fromFixers(...$phpCsFixerCustomFixers = array_filter(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Trying to invoke \(callable\(class\-string, string\)\: bool\)\|null but it might not be a callable\.$#'
7+
count: 1
8+
path: ../src/Support/helpers.php

baselines/loader.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# total 3 errors
1+
# total 4 errors
2+
23
includes:
4+
- callable.nonCallable.neon
35
- disallowed.function.neon
46
- new.static.neon
57
- typePerfect.narrowPublicClassMethodParamType.neon

benchmarks/SoarBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @see https://github.com/guanguans/soar-php
1515
*/
1616

17-
namespace Guanguans\PackageSkeleton\Tests\Benchmark;
17+
namespace Guanguans\SoarPHPBenchmarks;
1818

1919
use Guanguans\SoarPHP\Soar;
2020
use PhpBench\Attributes\BeforeMethods;

composer-dependency-analyser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
],
3939
[ErrorType::SHADOW_DEPENDENCY],
4040
)
41+
->ignoreErrorsOnPackageAndPath(
42+
'illuminate/collections',
43+
__DIR__.'/src/Support/helpers.php',
44+
[ErrorType::SHADOW_DEPENDENCY]
45+
)
4146
->ignoreErrorsOnPackageAndPath(
4247
'symfony/var-dumper',
4348
__DIR__.'/src/Concerns/WithDumpable.php',

composer-updater

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@ $status = (new SingleCommandApplication)
5353
new SymfonyStyle($input, $output),
5454
new Differ(new UnifiedDiffOutputBuilder("--- Original\n+++ New\n", true)),
5555
) {
56-
private string $composerJsonPath;
57-
private string $composerJsonContents;
58-
private string $highestComposerBinary;
59-
private string $composerBinary;
60-
private array $exceptPackages;
61-
private array $exceptDependencyVersions;
62-
private SymfonyStyle $symfonyStyle;
63-
private Differ $differ;
56+
private readonly string $composerJsonPath;
57+
private readonly string $composerJsonContents;
58+
private readonly string $highestComposerBinary;
59+
private readonly string $composerBinary;
60+
61+
/** @var list<string> */
62+
private readonly array $exceptPackages;
63+
64+
/** @var list<string> */
65+
private readonly array $exceptDependencyVersions;
66+
private readonly SymfonyStyle $symfonyStyle;
67+
private readonly Differ $differ;
6468

6569
/**
6670
* @noinspection ParameterDefaultsNullInspection
@@ -71,7 +75,7 @@ $status = (new SingleCommandApplication)
7175
?string $composerBinary = null,
7276
array $exceptPackages = [],
7377
array $exceptDependencyVersions = [],
74-
private bool $dryRun = false,
78+
private readonly bool $dryRun = false,
7579
?SymfonyStyle $symfonyStyle = null,
7680
?Differ $differ = null
7781
) {

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
},
9595
"autoload-dev": {
9696
"psr-4": {
97+
"Guanguans\\SoarPHPBenchmarks\\": "benchmarks/",
9798
"Guanguans\\SoarPHPTests\\": "tests/"
9899
}
99100
},

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ parameters:
7171
# - identifier: typePerfect.noMixedMethodCaller
7272
- identifier: argument.type
7373
- identifier: binaryOp.invalid
74-
- identifier: encapsedStringPart.nonString
74+
# - identifier: encapsedStringPart.nonString
7575
- identifier: logicalAnd.resultUnused
7676
- identifier: method.nonObject
7777
- identifier: missingType.generics

rector-soar-options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
->withPaths([
2525
__DIR__.'/examples/soar-options.php',
2626
])
27-
->withPhpVersion(PhpVersion::PHP_80)
27+
->withPhpVersion(PhpVersion::PHP_81)
2828
->withoutParallel()
2929
// ->withImportNames(importNames: false)
3030
->withImportNames(importDocBlockNames: false, importShortClasses: false)

rector.php

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@
1515
* @see https://github.com/guanguans/soar-php
1616
*/
1717

18-
use Composer\Autoload\ClassLoader;
1918
use Ergebnis\Rector\Rules\Arrays\SortAssociativeArrayByKeyRector;
19+
use Guanguans\MonorepoBuilderWorker\Support\Rectors\AddNoinspectionsDocCommentToDeclareRector;
20+
use Guanguans\MonorepoBuilderWorker\Support\Rectors\NewExceptionToNewAnonymousExtendsExceptionImplementsRector;
21+
use Guanguans\MonorepoBuilderWorker\Support\Rectors\RemoveNamespaceRector;
22+
use Guanguans\SoarPHP\Contracts\Throwable;
2023
use Guanguans\SoarPHP\Support\Rectors\AddHasOptionsDocCommentRector;
21-
use Guanguans\SoarPHP\Support\Rectors\TransformToInternalExceptionRector;
2224
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2325
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
2426
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
2527
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
2628
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
2729
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
2830
use Rector\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
31+
use Rector\CodingStyle\Rector\FunctionLike\FunctionLikeToFirstClassCallableRector;
2932
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
3033
use Rector\Config\RectorConfig;
3134
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
32-
use Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector;
3335
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
3436
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
3537
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
@@ -38,74 +40,85 @@
3840
use Rector\PHPUnit\Set\PHPUnitSetList;
3941
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
4042
use Rector\ValueObject\PhpVersion;
41-
42-
function classes(): array
43-
{
44-
static $classes;
45-
46-
foreach (spl_autoload_functions() as $loader) {
47-
if (\is_array($loader) && $loader[0] instanceof ClassLoader) {
48-
return $classes ??= array_keys($loader[0]->getClassMap());
49-
}
50-
}
51-
52-
return $classes ??= [];
53-
}
43+
use function Guanguans\SoarPHP\Support\classes;
5444

5545
return RectorConfig::configure()
5646
->withPaths([
57-
__DIR__.'/benchmarks',
58-
__DIR__.'/examples',
59-
__DIR__.'/src',
60-
__DIR__.'/tests',
61-
...glob(__DIR__.'/{*,.*}.php', \GLOB_BRACE),
47+
__DIR__.'/benchmarks/',
48+
__DIR__.'/examples/',
49+
__DIR__.'/src/',
50+
__DIR__.'/tests/',
6251
__DIR__.'/composer-updater',
6352
])
6453
->withRootFiles()
65-
// ->withSkipPath(__DIR__.'/tests.php')
54+
->withAutoloadPaths([
55+
// (new ReflectionClass(Throwable::class))->getFileName(),
56+
])
57+
->withBootstrapFiles([
58+
// __DIR__.'/vendor/symplify/monorepo-builder/vendor/autoload.php',
59+
// __DIR__.'/vendor/symplify/monorepo-builder/vendor/scoper-autoload.php',
60+
])
6661
->withSkip([
6762
'**/__snapshots__/*',
6863
'**/Fixtures/*',
64+
__DIR__.'/_ide_helper.php',
65+
__DIR__.'/tests.php',
6966
// __FILE__,
7067
])
7168
->withCache(__DIR__.'/.build/rector/')
69+
// ->withoutParallel()
7270
->withParallel()
73-
->withoutParallel()
74-
// ->withImportNames(importNames: false)
7571
->withImportNames(importDocBlockNames: false, importShortClasses: false)
72+
// ->withImportNames(importNames: false)
73+
// ->withEditorUrl()
7674
->withFluentCallNewLine()
75+
->withTreatClassesAsFinal()
7776
->withAttributesSets(phpunit: true, all: true)
78-
->withComposerBased(phpunit: true)
79-
->withPhpVersion(PhpVersion::PHP_80)
80-
->withDowngradeSets(php80: true)
81-
->withPhpSets(php80: true)
77+
->withComposerBased(phpunit: true/* , laravel: true */)
78+
->withPhpVersion(PhpVersion::PHP_81)
79+
->withDowngradeSets(php81: true)
80+
->withPhpSets(php81: true)
8281
->withPreparedSets(
8382
deadCode: true,
8483
codeQuality: true,
8584
codingStyle: true,
8685
typeDeclarations: true,
86+
typeDeclarationDocblocks: true,
8787
privatization: true,
8888
naming: true,
8989
instanceOf: true,
9090
earlyReturn: true,
91-
carbon: true,
92-
rectorPreset: true,
93-
phpunitCodeQuality: true,
91+
// carbon: true,
9492
)
9593
->withSets([
96-
PHPUnitSetList::PHPUNIT_90,
94+
PHPUnitSetList::PHPUNIT_100,
9795
])
9896
->withRules([
9997
ArraySpreadInsteadOfArrayMergeRector::class,
10098
JsonThrowOnErrorRector::class,
101-
SortAssociativeArrayByKeyRector::class,
10299
StaticArrowFunctionRector::class,
103100
StaticClosureRector::class,
101+
SortAssociativeArrayByKeyRector::class,
104102
AddHasOptionsDocCommentRector::class,
105-
TransformToInternalExceptionRector::class,
103+
])
104+
->withConfiguredRule(AddNoinspectionsDocCommentToDeclareRector::class, [
105+
'AnonymousFunctionStaticInspection',
106+
'NullPointerExceptionInspection',
107+
'PhpPossiblePolymorphicInvocationInspection',
108+
'PhpUndefinedClassInspection',
109+
'PhpUnhandledExceptionInspection',
110+
'PhpVoidFunctionResultUsedInspection',
111+
'StaticClosureCanBeUsedInspection',
112+
])
113+
->withConfiguredRule(NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class, [
114+
Throwable::class,
115+
])
116+
->withConfiguredRule(RemoveNamespaceRector::class, [
117+
'Guanguans\SoarPHPTests',
106118
])
107119
->withConfiguredRule(RemoveAnnotationRector::class, [
108120
'codeCoverageIgnore',
121+
'inheritDoc',
109122
'phpstan-ignore',
110123
'phpstan-ignore-next-line',
111124
'psalm-suppress',
@@ -119,6 +132,7 @@ function classes(): array
119132
'test' => 'it',
120133
] + array_reduce(
121134
[
135+
'classes',
122136
'escape_argument',
123137
'str_snake',
124138
],
@@ -133,23 +147,18 @@ static function (array $carry, string $func): array {
133147
)
134148
->withConfiguredRule(
135149
AnnotationToAttributeRector::class,
136-
array_map(
137-
static fn (string $class): AnnotationToAttribute => new AnnotationToAttribute(
138-
(new ReflectionClass($class))->getShortName(),
139-
$class,
150+
classes(static fn (string $class): bool => str_starts_with($class, 'PhpBench\Attributes'))
151+
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isInstantiable())
152+
->map(static fn (ReflectionClass $reflectionClass): AnnotationToAttribute => new AnnotationToAttribute(
153+
$reflectionClass->getShortName(),
154+
$reflectionClass->getName(),
140155
[],
141156
true
142-
),
143-
array_filter(
144-
classes(),
145-
static fn (string $class): bool => str_starts_with($class, 'PhpBench\Attributes')
146-
&& (new ReflectionClass($class))->isInstantiable()
147-
)
148-
)
157+
))
158+
->all(),
149159
)
150160
->withSkip([
151161
ChangeOrIfContinueToMultiContinueRector::class,
152-
DowngradeArraySpreadRector::class,
153162
EncapsedStringsToSprintfRector::class,
154163
ExplicitBoolCompareRector::class,
155164
LogicalToBooleanRector::class,
@@ -158,14 +167,48 @@ classes(),
158167
WrapEncapsedVariableInCurlyBracesRector::class,
159168
])
160169
->withSkip([
170+
FunctionLikeToFirstClassCallableRector::class => [
171+
__DIR__.'/src/Support/helpers.php',
172+
__DIR__.'/tests/Concerns/HasOptionsTest.php',
173+
],
161174
StaticArrowFunctionRector::class => $staticClosureSkipPaths = [
162-
__DIR__.'/tests',
175+
__DIR__.'/tests/',
163176
],
164177
StaticClosureRector::class => $staticClosureSkipPaths,
165178
SortAssociativeArrayByKeyRector::class => [
166-
__DIR__.'/benchmarks',
167-
__DIR__.'/examples',
168-
__DIR__.'/src',
169-
__DIR__.'/tests',
179+
__DIR__.'/benchmarks/',
180+
__DIR__.'/examples/',
181+
__DIR__.'/src/',
182+
__DIR__.'/tests/',
183+
],
184+
AddNoinspectionsDocCommentToDeclareRector::class => [
185+
__DIR__.'/benchmarks/',
186+
__DIR__.'/examples/',
187+
__DIR__.'/src/',
188+
// __DIR__.'/tests/',
189+
...$rootFiles = array_filter(
190+
glob(__DIR__.'/{*,.*}.php', \GLOB_BRACE),
191+
static fn (string $filename): bool => !\in_array(
192+
$filename,
193+
[
194+
__DIR__.'/_ide_helper.php',
195+
__DIR__.'/tests.php',
196+
],
197+
true
198+
)
199+
),
200+
__DIR__.'/composer-updater',
201+
],
202+
NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class => [
203+
__DIR__.'/src/Support/Rectors/',
204+
],
205+
RemoveNamespaceRector::class => [
206+
__DIR__.'/benchmarks/',
207+
__DIR__.'/examples/',
208+
__DIR__.'/src/',
209+
// __DIR__.'/tests/',
210+
...$rootFiles,
211+
__DIR__.'/composer-updater',
212+
__DIR__.'/tests/TestCase.php',
170213
],
171214
]);

0 commit comments

Comments
 (0)