Skip to content

Commit b151111

Browse files
committed
refactor: apply rector
1 parent 5b0df8e commit b151111

File tree

7 files changed

+33
-90
lines changed

7 files changed

+33
-90
lines changed

.php-cs-fixer-custom.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\SqruffFixer;
2929
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TextlintFixer;
3030
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TombiFixer;
31+
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TyposFixer;
3132
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\XmllintFixer;
3233
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\YamlfmtFixer;
3334
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\ZhlintFixer;
@@ -39,12 +40,6 @@
3940
use PhpCsFixer\Finder;
4041
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
4142

42-
// putenv('PHP_CS_FIXER_ENFORCE_CACHE=1');
43-
// putenv('PHP_CS_FIXER_IGNORE_ENV=1');
44-
putenv('PHP_CS_FIXER_FUTURE_MODE=1');
45-
putenv('PHP_CS_FIXER_NON_MONOLITHIC=1');
46-
putenv('PHP_CS_FIXER_PARALLEL=1');
47-
4843
return (new Config)
4944
->registerCustomFixers($fixers = Fixers::make())
5045
->setRules([
@@ -89,6 +84,7 @@
8984
JsonFixer::name() => true,
9085
ShfmtFixer::name() => true,
9186
TombiFixer::name() => true,
87+
TyposFixer::name() => true,
9288
XmllintFixer::name() => true,
9389
YamlfmtFixer::name() => true,
9490
])
@@ -114,7 +110,8 @@
114110
'/\-lock\.json$/',
115111
// '/\.php$/',
116112
'/(?<!\.blade)\.php$/',
117-
'/zhlint\-.*\.zh_CN\.md$/',
113+
// Exclude temporary files created by `zhlint` in the current working directory.
114+
'/zhlint\-.*\..*$/',
118115
])
119116
->ignoreDotFiles(false)
120117
->ignoreUnreadableDirs(false)

monorepo-builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
]))
7676
->setEnv(['COMPOSER_MEMORY_LIMIT' => -1])
7777
->setTimeout(600)
78-
->mustRun(static function (string $type, string $buffer): void {
78+
->mustRun(static function (string $_, string $buffer): void {
7979
$symfonyStyle ??= new SymfonyStyle(new ArgvInput, new ConsoleOutput);
8080
$symfonyStyle->write($buffer);
8181
});

peck.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"preset": "base",
33
"ignore": {
44
"words": [
5-
"php",
65
"sudo",
76
"makeable",
87
"dumpable",

rector.php renamed to rector.dist.php

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616
*/
1717

1818
use Ergebnis\Rector\Rules\Arrays\SortAssociativeArrayByKeyRector;
19-
use Guanguans\RectorRules\Rector\Array_\SimplifyListIndexRector;
20-
use Guanguans\RectorRules\Rector\Class_\UpdateRectorRefactorParamDocblockFromNodeTypesRector;
21-
use Guanguans\RectorRules\Rector\Declare_\AddNoinspectionDocblockToDeclareRector;
22-
use Guanguans\RectorRules\Rector\Name\RenameToPsrNameRector;
23-
use Guanguans\RectorRules\Rector\Namespace_\RemoveNamespaceRector;
24-
use Guanguans\RectorRules\Rector\New_\NewExceptionToNewAnonymousExtendsExceptionImplementsRector;
25-
use Guanguans\SoarPHP\Contracts\Throwable;
19+
use Ergebnis\Rector\Rules\Faker\GeneratorPropertyFetchToMethodCallRector;
20+
use Guanguans\RectorRules\Rector\File\AddNoinspectionDocblockToFileFirstStmtRector;
21+
use Guanguans\RectorRules\Rector\Name\RenameToConventionalCaseNameRector;
22+
use Guanguans\RectorRules\Set\SetList;
2623
use Guanguans\SoarPHP\Support\Rectors\AddDocCommentForHasOptionsRector;
27-
use Illuminate\Support\Str;
2824
use PhpParser\NodeVisitor\ParentConnectingVisitor;
2925
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
3026
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
@@ -34,24 +30,16 @@
3430
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
3531
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
3632
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
33+
use Rector\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector;
3734
use Rector\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
3835
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
3936
use Rector\Config\RectorConfig;
4037
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
4138
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
4239
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
4340
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
44-
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
45-
use Rector\Php80\ValueObject\AnnotationToAttribute;
46-
use Rector\PHPUnit\Set\PHPUnitSetList;
47-
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
48-
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
49-
use Rector\Transform\ValueObject\StaticCallToFuncCall;
41+
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;
5042
use Rector\ValueObject\PhpVersion;
51-
use Rector\ValueObject\Visibility;
52-
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
53-
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
54-
use function Guanguans\SoarPHP\Support\classes;
5543

5644
return RectorConfig::configure()
5745
->withPaths([
@@ -90,24 +78,24 @@
9078
naming: true,
9179
instanceOf: true,
9280
earlyReturn: true,
81+
// strictBooleans: true,
9382
// carbon: true,
83+
rectorPreset: true,
84+
phpunitCodeQuality: true,
9485
)
95-
->withSets([
96-
PHPUnitSetList::PHPUNIT_100,
97-
])
86+
->withSets([SetList::ALL])
9887
->withRules([
9988
AddDocCommentForHasOptionsRector::class,
100-
RemoveNamespaceRector::class,
101-
SimplifyListIndexRector::class,
102-
SortAssociativeArrayByKeyRector::class,
103-
UpdateRectorRefactorParamDocblockFromNodeTypesRector::class,
104-
10589
ArraySpreadInsteadOfArrayMergeRector::class,
90+
EnumCaseToPascalCaseRector::class,
91+
GeneratorPropertyFetchToMethodCallRector::class,
10692
JsonThrowOnErrorRector::class,
93+
SafeDeclareStrictTypesRector::class,
94+
SortAssociativeArrayByKeyRector::class,
10795
StaticArrowFunctionRector::class,
10896
StaticClosureRector::class,
10997
])
110-
->withConfiguredRule(AddNoinspectionDocblockToDeclareRector::class, [
98+
->withConfiguredRule(AddNoinspectionDocblockToFileFirstStmtRector::class, [
11199
'*/tests/*' => [
112100
'AnonymousFunctionStaticInspection',
113101
'NullPointerExceptionInspection',
@@ -119,9 +107,8 @@
119107
'StaticClosureCanBeUsedInspection',
120108
],
121109
])
122-
->withConfiguredRule(NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class, [Throwable::class])
123110
->registerDecoratingNodeVisitor(ParentConnectingVisitor::class)
124-
->withConfiguredRule(RenameToPsrNameRector::class, [
111+
->withConfiguredRule(RenameToConventionalCaseNameRector::class, [
125112
'assertMatches*Snapshot',
126113
'MIT',
127114
])
@@ -132,44 +119,6 @@
132119
'phpstan-ignore-next-line',
133120
'psalm-suppress',
134121
])
135-
->withConfiguredRule(StaticCallToFuncCallRector::class, [
136-
new StaticCallToFuncCall(Str::class, 'of', 'str'),
137-
])
138-
->withConfiguredRule(
139-
AnnotationToAttributeRector::class,
140-
classes(static fn (string $class): bool => str_starts_with($class, 'PhpBench\Attributes'))
141-
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isInstantiable())
142-
->map(static fn (ReflectionClass $reflectionClass): AnnotationToAttribute => new AnnotationToAttribute(
143-
lcfirst($reflectionClass->getShortName()),
144-
$reflectionClass->getName(),
145-
// [],
146-
// true
147-
))
148-
->all(),
149-
)
150-
->withConfiguredRule(
151-
ChangeMethodVisibilityRector::class,
152-
classes(static fn (string $class, string $file): bool => str_starts_with($class, 'Guanguans\SoarPHP'))
153-
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isTrait())
154-
->map(
155-
static fn (ReflectionClass $reflectionClass): array => collect($reflectionClass->getMethods(ReflectionMethod::IS_PRIVATE))
156-
->reject(static fn (ReflectionMethod $reflectionMethod): bool => $reflectionMethod->isFinal() || $reflectionMethod->isInternal())
157-
->map(static fn (ReflectionMethod $reflectionMethod): ChangeMethodVisibility => new ChangeMethodVisibility(
158-
$reflectionClass->getName(),
159-
$reflectionMethod->getName(),
160-
Visibility::PROTECTED
161-
))
162-
->all()
163-
)
164-
->flatten()
165-
// ->dd()
166-
->all(),
167-
)
168-
->withConfiguredRule(RenameFunctionRector::class, [
169-
'Pest\Faker\fake' => 'fake',
170-
'Pest\Faker\faker' => 'fake',
171-
'test' => 'it',
172-
])
173122
->withSkip([
174123
ChangeOrIfContinueToMultiContinueRector::class,
175124
EncapsedStringsToSprintfRector::class,

src/Concerns/HasOptions.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22

3-
/** @noinspection PhpClassHasTooManyDeclaredMembersInspection */
4-
53
/** @noinspection LongLine */
4+
/** @noinspection PhpClassHasTooManyDeclaredMembersInspection */
65
/** @noinspection PhpFullyQualifiedNameUsageInspection */
7-
86
declare(strict_types=1);
97

108
/**
@@ -562,7 +560,7 @@ protected function getNormalizedOptions(): array
562560
*
563561
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
564562
*/
565-
private function normalizeOptions(array $options): array
563+
protected function normalizeOptions(array $options): array
566564
{
567565
foreach ($options as $name => $value) {
568566
$options[$name] = $this->normalizeOption($name, $value);
@@ -574,7 +572,7 @@ private function normalizeOptions(array $options): array
574572
/**
575573
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
576574
*/
577-
private function normalizeOption(string $name, mixed $value): string
575+
protected function normalizeOption(string $name, mixed $value): string
578576
{
579577
$normalizedValue = $this->normalizeValue($name, $value);
580578

@@ -587,7 +585,7 @@ private function normalizeOption(string $name, mixed $value): string
587585
* @noinspection PhpUnhandledExceptionInspection
588586
* @noinspection PhpDocMissingThrowsInspection
589587
*/
590-
private function normalizeValue(string $name, mixed $value): ?string
588+
protected function normalizeValue(string $name, mixed $value): ?string
591589
{
592590
if (\is_string($value) || null === $value) {
593591
return $value;
@@ -632,7 +630,7 @@ private function normalizeValue(string $name, mixed $value): ?string
632630
*
633631
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
634632
*/
635-
private function normalizeDsn(string $name, array $dsn): ?string
633+
protected function normalizeDsn(string $name, array $dsn): ?string
636634
{
637635
$dsn += $default = [
638636
// 'user' => '',

src/Support/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
function classes(?callable $filter = null): Collection
4646
{
47-
$filter ??= static fn (string $class, string $file): bool => true;
47+
$filter ??= static fn (string $_, string $__): bool => true;
4848

4949
/** @var null|\Illuminate\Support\Collection $classes */
5050
static $classes;
@@ -56,7 +56,7 @@ function classes(?callable $filter = null): Collection
5656

5757
return $classes
5858
->filter(static fn (string $file, string $class): bool => $filter($class, $file))
59-
->mapWithKeys(static function (string $file, string $class): array {
59+
->mapWithKeys(static function (string $_, string $class): array {
6060
try {
6161
return [$class => new \ReflectionClass($class)];
6262
} catch (\Throwable $throwable) {

tests/Pest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ function class_namespace(object|string $class): string
9494
return (new ReflectionClass($class))->getNamespaceName();
9595
}
9696

97-
function fixtures_path(string $path = ''): string
98-
{
99-
return __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.($path ? \DIRECTORY_SEPARATOR.$path : $path);
100-
}
101-
10297
if (!\function_exists('fake')) {
10398
/**
10499
* @see https://github.com/laravel/framework/blob/12.x/src/Illuminate/Foundation/helpers.php#L515
@@ -109,6 +104,11 @@ function fake(string $locale = Factory::DEFAULT_LOCALE): Generator
109104
}
110105
}
111106

107+
function fixtures_path(string $path = ''): string
108+
{
109+
return __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.($path ? \DIRECTORY_SEPARATOR.$path : $path);
110+
}
111+
112112
function running_in_github_action(): bool
113113
{
114114
return 'true' === getenv('GITHUB_ACTIONS');

0 commit comments

Comments
 (0)