Skip to content

Commit 2699026

Browse files
authored
refactor: use latest rector syntax (#151)
1 parent 25c0e45 commit 2699026

File tree

3 files changed

+83
-238
lines changed

3 files changed

+83
-238
lines changed

rector.php

Lines changed: 41 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
6-
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
7-
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
8-
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
9-
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
10-
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
11-
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
12-
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
13-
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
14-
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
156
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
16-
use Rector\CodeQuality\Rector\If_\CombineIfRector;
17-
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
18-
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
19-
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
20-
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
21-
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
22-
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
237
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
248
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
259
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
@@ -32,113 +16,53 @@
3216
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
3317
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
3418
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
35-
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
36-
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
37-
use Rector\PHPUnit\Set\PHPUnitSetList;
3819
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
39-
use Rector\Set\ValueObject\LevelSetList;
40-
use Rector\Set\ValueObject\SetList;
4120
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4221
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
4322
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
44-
use Rector\ValueObject\PhpVersion;
4523

46-
return static function (RectorConfig $rectorConfig): void {
47-
$rectorConfig->sets([
48-
SetList::DEAD_CODE,
49-
LevelSetList::UP_TO_PHP_74,
50-
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
51-
PHPUnitSetList::PHPUNIT_100,
52-
]);
53-
54-
$rectorConfig->parallel();
55-
56-
// Github action cache
57-
$rectorConfig->cacheClass(FileCacheStorage::class);
58-
if (is_dir('/tmp')) {
59-
$rectorConfig->cacheDirectory('/tmp/rector');
60-
}
61-
62-
// The paths to refactor (can also be supplied with CLI arguments)
63-
$rectorConfig->paths([
64-
__DIR__ . '/src/',
65-
]);
66-
67-
// Include Composer's autoload - required for global execution, remove if running locally
68-
$rectorConfig->autoloadPaths([
24+
return RectorConfig::configure()
25+
->withPhpSets(php81: true)
26+
->withPreparedSets(deadCode: true, codeQuality: true)
27+
->withComposerBased(phpunit: true)
28+
->withParallel(120, 8, 10)
29+
->withCache(
30+
is_dir('/tmp') ? '/tmp/rector' : null,
31+
FileCacheStorage::class,
32+
)
33+
->withPaths([
34+
__DIR__ . '/src',
35+
])
36+
->withAutoloadPaths([
6937
__DIR__ . '/vendor/autoload.php',
70-
]);
71-
72-
// Do you need to include constants, class aliases, or a custom autoloader?
73-
$rectorConfig->bootstrapFiles([
74-
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
75-
]);
76-
77-
if (is_file(__DIR__ . '/phpstan.neon.dist')) {
78-
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
79-
}
80-
81-
// Set the target version for refactoring
82-
$rectorConfig->phpVersion(PhpVersion::PHP_74);
83-
84-
// Auto-import fully qualified class names
85-
$rectorConfig->importNames();
86-
87-
// Are there files or rules you need to skip?
88-
$rectorConfig->skip([
89-
__DIR__ . '/src/Views',
90-
38+
])
39+
->withBootstrapFiles([
40+
__DIR__ . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
41+
])
42+
->withPHPStanConfigs([
43+
__DIR__ . '/phpstan.neon.dist',
44+
])
45+
->withImportNames(removeUnusedImports: true)
46+
->withSkip([
9147
StringifyStrNeedlesRector::class,
92-
YieldDataProviderRector::class,
93-
94-
// Note: requires php 8
95-
RemoveUnusedPromotedPropertyRector::class,
96-
AnnotationWithValueToAttributeRector::class,
97-
98-
// May load view files directly when detecting classes
48+
RemoveUnusedPromotedPropertyRector::class, // Note: requires php 8
49+
])
50+
->withRules([
51+
ChangeIfElseValueAssignToEarlyReturnRector::class,
52+
ChangeNestedForeachIfsToEarlyContinueRector::class,
53+
CountArrayToEmptyArrayComparisonRector::class,
54+
DisallowedEmptyRuleFixerRector::class,
55+
EmptyOnNullableObjectToInstanceOfRector::class,
56+
FuncGetArgsToVariadicParamRector::class,
57+
MakeInheritedMethodVisibilitySameAsParentRector::class,
58+
PreparedValueToEarlyReturnRector::class,
59+
PrivatizeFinalClassPropertyRector::class,
60+
RemoveAlwaysElseRector::class,
61+
SimplifyUselessVariableRector::class,
9962
StringClassNameToClassConstantRector::class,
63+
VersionCompareFuncCallToConstantRector::class,
64+
])
65+
->withConfiguredRule(TypedPropertyFromAssignsRector::class, [
66+
// Allow public property inlining when BC breaks are acceptable.
67+
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
10068
]);
101-
102-
// auto import fully qualified class names
103-
$rectorConfig->importNames();
104-
105-
$rectorConfig->rule(SimplifyUselessVariableRector::class);
106-
$rectorConfig->rule(RemoveAlwaysElseRector::class);
107-
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
108-
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
109-
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
110-
$rectorConfig->rule(SimplifyStrposLowerRector::class);
111-
$rectorConfig->rule(CombineIfRector::class);
112-
$rectorConfig->rule(SimplifyIfReturnBoolRector::class);
113-
$rectorConfig->rule(InlineIfToExplicitIfRector::class);
114-
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
115-
$rectorConfig->rule(ShortenElseIfRector::class);
116-
$rectorConfig->rule(SimplifyIfElseToTernaryRector::class);
117-
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
118-
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
119-
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
120-
$rectorConfig->rule(SimplifyRegexPatternRector::class);
121-
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
122-
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
123-
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
124-
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
125-
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
126-
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
127-
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
128-
$rectorConfig
129-
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
130-
/**
131-
* The INLINE_PUBLIC value is default to false to avoid BC break,
132-
* if you use for libraries and want to preserve BC break, you don't
133-
* need to configure it, as it included in LevelSetList::UP_TO_PHP_74
134-
* Set to true for projects that allow BC break
135-
*/
136-
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
137-
]);
138-
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
139-
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
140-
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
141-
$rectorConfig->rule(SingleInArrayToCompareRector::class);
142-
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
143-
$rectorConfig->rule(ExplicitBoolCompareRector::class);
144-
};

src/Template/composer-unused.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
declare(strict_types=1);
44

55
use ComposerUnused\ComposerUnused\Configuration\Configuration;
6-
use ComposerUnused\ComposerUnused\Configuration\NamedFilter;
7-
use ComposerUnused\ComposerUnused\Configuration\PatternFilter;
86
use Webmozart\Glob\Glob;
97

108
return static fn (Configuration $config): Configuration => $config
11-
// ->addNamedFilter(NamedFilter::fromString('symfony/config'))
12-
// ->addPatternFilter(PatternFilter::fromString('/symfony-.*/'))
139
->setAdditionalFilesFor('codeigniter4/framework', [
1410
...Glob::glob(__DIR__ . '/vendor/codeigniter4/framework/system/Helpers/*.php'),
1511
]);

src/Template/rector.php

Lines changed: 42 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
6-
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
7-
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
8-
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
9-
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
10-
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
11-
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
12-
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
13-
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
14-
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
156
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
16-
use Rector\CodeQuality\Rector\If_\CombineIfRector;
17-
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
18-
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
19-
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
20-
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
21-
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
22-
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
237
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
248
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
259
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
@@ -32,114 +16,55 @@
3216
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
3317
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
3418
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
35-
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
36-
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
37-
use Rector\PHPUnit\Set\PHPUnitSetList;
3819
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
39-
use Rector\Set\ValueObject\LevelSetList;
40-
use Rector\Set\ValueObject\SetList;
4120
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4221
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
4322
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
44-
use Rector\ValueObject\PhpVersion;
4523

46-
return static function (RectorConfig $rectorConfig): void {
47-
$rectorConfig->sets([
48-
SetList::DEAD_CODE,
49-
LevelSetList::UP_TO_PHP_81,
50-
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
51-
PHPUnitSetList::PHPUNIT_100,
52-
]);
53-
54-
$rectorConfig->parallel();
55-
56-
// Github action cache
57-
$rectorConfig->cacheClass(FileCacheStorage::class);
58-
if (is_dir('/tmp')) {
59-
$rectorConfig->cacheDirectory('/tmp/rector');
60-
}
61-
62-
// The paths to refactor (can also be supplied with CLI arguments)
63-
$rectorConfig->paths([
64-
__DIR__ . '/app/',
65-
__DIR__ . '/tests/',
66-
]);
67-
68-
// Include Composer's autoload - required for global execution, remove if running locally
69-
$rectorConfig->autoloadPaths([
24+
return RectorConfig::configure()
25+
->withPhpSets(php81: true)
26+
->withPreparedSets(deadCode: true, codeQuality: true)
27+
->withComposerBased(phpunit: true)
28+
->withParallel(120, 8, 10)
29+
->withCache(
30+
is_dir('/tmp') ? '/tmp/rector' : null,
31+
FileCacheStorage::class,
32+
)
33+
->withPaths([
34+
__DIR__ . '/app',
35+
__DIR__ . '/tests',
36+
])
37+
->withAutoloadPaths([
7038
__DIR__ . '/vendor/autoload.php',
71-
]);
72-
73-
// Do you need to include constants, class aliases, or a custom autoloader?
74-
$rectorConfig->bootstrapFiles([
75-
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
76-
]);
77-
78-
if (is_file(__DIR__ . '/phpstan.neon.dist')) {
79-
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
80-
}
81-
82-
// Set the target version for refactoring
83-
$rectorConfig->phpVersion(PhpVersion::PHP_81);
84-
85-
// Auto-import fully qualified class names
86-
$rectorConfig->importNames();
87-
88-
// Are there files or rules you need to skip?
89-
$rectorConfig->skip([
39+
])
40+
->withBootstrapFiles([
41+
__DIR__ . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
42+
])
43+
->withPHPStanConfigs([
44+
__DIR__ . '/phpstan.neon.dist',
45+
])
46+
->withImportNames(removeUnusedImports: true)
47+
->withSkip([
9048
__DIR__ . '/app/Views',
91-
9249
StringifyStrNeedlesRector::class,
93-
YieldDataProviderRector::class,
94-
95-
// Note: requires php 8
96-
RemoveUnusedPromotedPropertyRector::class,
97-
AnnotationWithValueToAttributeRector::class,
98-
99-
// May load view files directly when detecting classes
50+
RemoveUnusedPromotedPropertyRector::class, // Note: requires php 8
51+
])
52+
->withRules([
53+
ChangeIfElseValueAssignToEarlyReturnRector::class,
54+
ChangeNestedForeachIfsToEarlyContinueRector::class,
55+
CountArrayToEmptyArrayComparisonRector::class,
56+
DisallowedEmptyRuleFixerRector::class,
57+
EmptyOnNullableObjectToInstanceOfRector::class,
58+
FuncGetArgsToVariadicParamRector::class,
59+
MakeInheritedMethodVisibilitySameAsParentRector::class,
60+
PreparedValueToEarlyReturnRector::class,
61+
PrivatizeFinalClassPropertyRector::class,
62+
RemoveAlwaysElseRector::class,
63+
SimplifyUselessVariableRector::class,
10064
StringClassNameToClassConstantRector::class,
65+
VersionCompareFuncCallToConstantRector::class,
66+
])
67+
->withConfiguredRule(TypedPropertyFromAssignsRector::class, [
68+
// Allow public property inlining when BC breaks are acceptable.
69+
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
10170
]);
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(ChangeNestedForeachIfsToEarlyContinueRector::class);
110-
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
111-
$rectorConfig->rule(SimplifyStrposLowerRector::class);
112-
$rectorConfig->rule(CombineIfRector::class);
113-
$rectorConfig->rule(SimplifyIfReturnBoolRector::class);
114-
$rectorConfig->rule(InlineIfToExplicitIfRector::class);
115-
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
116-
$rectorConfig->rule(ShortenElseIfRector::class);
117-
$rectorConfig->rule(SimplifyIfElseToTernaryRector::class);
118-
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
119-
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
120-
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
121-
$rectorConfig->rule(SimplifyRegexPatternRector::class);
122-
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
123-
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
124-
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
125-
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
126-
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
127-
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
128-
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
129-
$rectorConfig
130-
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
131-
/**
132-
* The INLINE_PUBLIC value is default to false to avoid BC break,
133-
* if you use for libraries and want to preserve BC break, you don't
134-
* need to configure it, as it included in LevelSetList::UP_TO_PHP_74
135-
* Set to true for projects that allow BC break
136-
*/
137-
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
138-
]);
139-
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
140-
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
141-
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
142-
$rectorConfig->rule(SingleInArrayToCompareRector::class);
143-
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
144-
$rectorConfig->rule(ExplicitBoolCompareRector::class);
145-
};

0 commit comments

Comments
 (0)