Skip to content

Commit e152989

Browse files
committed
chore: config Rector
1 parent 63ecead commit e152989

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"scripts": {
3838
"analyze": [
3939
"bash -c \"XDEBUG_MODE=off phpstan analyse\"",
40-
"psalm"
40+
"psalm",
41+
"rector process --dry-run"
4142
],
4243
"sa": "@analyze",
4344
"cs": "php-cs-fixer fix --ansi --verbose --dry-run --diff",

rector.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
4+
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
5+
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
6+
use Rector\CodeQuality\Rector\For_\ForToForeachRector;
7+
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
8+
use Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector;
9+
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
10+
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
11+
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
12+
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
13+
use Rector\CodeQuality\Rector\If_\CombineIfRector;
14+
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
15+
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
16+
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
17+
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
18+
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
19+
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
20+
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
21+
use Rector\Config\RectorConfig;
22+
use Rector\Core\ValueObject\PhpVersion;
23+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
24+
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
25+
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
26+
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
27+
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
28+
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
29+
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
30+
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
31+
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
32+
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
33+
use Rector\PHPUnit\Set\PHPUnitSetList;
34+
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
35+
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
36+
use Rector\Set\ValueObject\LevelSetList;
37+
use Rector\Set\ValueObject\SetList;
38+
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
39+
40+
return static function (RectorConfig $rectorConfig): void {
41+
$rectorConfig->sets([
42+
SetList::DEAD_CODE,
43+
LevelSetList::UP_TO_PHP_74,
44+
PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD,
45+
PHPUnitSetList::PHPUNIT_100,
46+
]);
47+
48+
$rectorConfig->parallel();
49+
50+
// The paths to refactor (can also be supplied with CLI arguments)
51+
$rectorConfig->paths([
52+
__DIR__ . '/src/',
53+
__DIR__ . '/tests/',
54+
]);
55+
56+
// Include Composer's autoload - required for global execution, remove if running locally
57+
$rectorConfig->autoloadPaths([
58+
__DIR__ . '/vendor/autoload.php',
59+
]);
60+
61+
// Do you need to include constants, class aliases, or a custom autoloader?
62+
$rectorConfig->bootstrapFiles([
63+
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
64+
]);
65+
66+
if (is_file(__DIR__ . '/phpstan.neon.dist')) {
67+
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
68+
}
69+
70+
// Set the target version for refactoring
71+
$rectorConfig->phpVersion(PhpVersion::PHP_74);
72+
73+
// Auto-import fully qualified class names
74+
$rectorConfig->importNames();
75+
76+
// Are there files or rules you need to skip?
77+
$rectorConfig->skip([
78+
__DIR__ . '/app/Views',
79+
80+
JsonThrowOnErrorRector::class,
81+
StringifyStrNeedlesRector::class,
82+
83+
// Note: requires php 8
84+
RemoveUnusedPromotedPropertyRector::class,
85+
86+
// Ignore tests that might make calls without a result
87+
RemoveEmptyMethodCallRector::class => [
88+
__DIR__ . '/tests',
89+
],
90+
91+
// Ignore files that should not be namespaced to their folder
92+
NormalizeNamespaceByPSR4ComposerAutoloadRector::class => [
93+
__DIR__ . '/app/Helpers',
94+
],
95+
96+
// May load view files directly when detecting classes
97+
StringClassNameToClassConstantRector::class,
98+
99+
// May be uninitialized on purpose
100+
AddDefaultValueForUndefinedVariableRector::class,
101+
]);
102+
103+
// auto import fully qualified class names
104+
$rectorConfig->importNames();
105+
106+
$rectorConfig->rule(SimplifyUselessVariableRector::class);
107+
$rectorConfig->rule(RemoveAlwaysElseRector::class);
108+
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
109+
$rectorConfig->rule(ForToForeachRector::class);
110+
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
111+
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
112+
$rectorConfig->rule(SimplifyStrposLowerRector::class);
113+
$rectorConfig->rule(CombineIfRector::class);
114+
$rectorConfig->rule(SimplifyIfReturnBoolRector::class);
115+
$rectorConfig->rule(InlineIfToExplicitIfRector::class);
116+
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
117+
$rectorConfig->rule(ShortenElseIfRector::class);
118+
$rectorConfig->rule(SimplifyIfElseToTernaryRector::class);
119+
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
120+
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
121+
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
122+
$rectorConfig->rule(AddPregQuoteDelimiterRector::class);
123+
$rectorConfig->rule(SimplifyRegexPatternRector::class);
124+
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
125+
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
126+
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
127+
$rectorConfig->rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class);
128+
$rectorConfig
129+
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
130+
/**
131+
* The INLINE_PUBLIC value is default to false to avoid BC break, if you use for libraries and want to preserve BC break, you don't need to configure it, as it included in LevelSetList::UP_TO_PHP_74
132+
* Set to true for projects that allow BC break
133+
*/
134+
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
135+
]);
136+
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
137+
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
138+
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
139+
};

0 commit comments

Comments
 (0)