xref: /dokuwiki/_test/rector.php (revision e7dae73bcd947f44c901faaac9dd45de67633a3b)
1077b7fe2SAndreas Gohr<?php
2077b7fe2SAndreas Gohr
3077b7fe2SAndreas Gohrdeclare(strict_types=1);
4077b7fe2SAndreas Gohr
52e0b7f74SAndreas Gohrdefine('DOKU_INC', __DIR__ . '/../');
62e0b7f74SAndreas Gohr
7f9a94e78SAndreas Gohruse dokuwiki\test\rector\DokuWikiPtlnRector;
826dfc232SAndreas Gohruse dokuwiki\test\rector\DokuWikiRenamePrintToEcho;
9077b7fe2SAndreas Gohruse Rector\Caching\ValueObject\Storage\FileCacheStorage;
101e467458SAndreas Gohruse Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector;
11e2d055f5SAndreas Gohruse Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
121e467458SAndreas Gohruse Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector;
13077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
14077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\CombineIfRector;
151fc5061dSAndreas Gohruse Rector\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector;
16077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
17077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
181490c177SAndreas Gohruse Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
19077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector;
20077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
21093fe67eSAndreas Gohruse Rector\CodingStyle\Rector\ClassLike\NewlineBetweenClassLikeStmtsRector;
22077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
236723156fSAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
24a469bafbSAndreas Gohruse Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
25077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
26093fe67eSAndreas Gohruse Rector\CodingStyle\Rector\String_\SimplifyQuoteEscapeRector;
27077b7fe2SAndreas Gohruse Rector\Config\RectorConfig;
28093fe67eSAndreas Gohruse Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
29*e7dae73bSAndreas Gohruse Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
30ed46af86SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
31077b7fe2SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
3205fd0e83SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
3324870174SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
34093fe67eSAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveDeadIfBlockRector;
35bcaec9f4SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
36104a3b7cSAndreas Gohruse Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
376723156fSAndreas Gohruse Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
3824870174SAndreas Gohruse Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
396e695190SAndreas Gohruse Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
40093fe67eSAndreas Gohruse Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
41093fe67eSAndreas Gohruse Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
42093fe67eSAndreas Gohruse Rector\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector;
43a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
44a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\Name\RenameClassRector;
45077b7fe2SAndreas Gohruse Rector\Set\ValueObject\LevelSetList;
46077b7fe2SAndreas Gohruse Rector\Set\ValueObject\SetList;
47077b7fe2SAndreas Gohruse Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4805fd0e83SAndreas Gohruse Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
498b9fc108SAndreas Gohruse Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
50894c1577SAndreas Gohruse Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;
51077b7fe2SAndreas Gohr
52077b7fe2SAndreas Gohrreturn static function (RectorConfig $rectorConfig): void {
53f9a94e78SAndreas Gohr    // FIXME we may want to autoload these later
54f9a94e78SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiPtlnRector.php';
5526dfc232SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiRenamePrintToEcho.php';
56f9a94e78SAndreas Gohr
57462a5601SAndreas Gohr    // tune parallel task settings (see rectorphp/rector#8396)
58462a5601SAndreas Gohr    $rectorConfig->parallel(120, 16, 10);
59462a5601SAndreas Gohr
60077b7fe2SAndreas Gohr    $rectorConfig->paths([
61b1f206e1SAndreas Gohr        __DIR__ . '/../inc/',
62b1f206e1SAndreas Gohr        __DIR__ . '/../lib/',
63b1f206e1SAndreas Gohr        __DIR__ . '/../bin/',
64b1f206e1SAndreas Gohr        __DIR__ . '/../*.php',
65077b7fe2SAndreas Gohr    ]);
66077b7fe2SAndreas Gohr
671490c177SAndreas Gohr    $rectorConfig->bootstrapFiles([
6824870174SAndreas Gohr        __DIR__ . '/../inc/init.php',
691490c177SAndreas Gohr    ]);
701490c177SAndreas Gohr
71077b7fe2SAndreas Gohr    $rectorConfig->importNames();
72077b7fe2SAndreas Gohr    $rectorConfig->importShortClasses(false);
73077b7fe2SAndreas Gohr    $rectorConfig->cacheClass(FileCacheStorage::class);
74077b7fe2SAndreas Gohr    $rectorConfig->cacheDirectory(__DIR__ . '/.rector-cache');
75077b7fe2SAndreas Gohr
7626586410SAndreas Gohr    // supported minimum PHP version can be overridden by environment variable
77093fe67eSAndreas Gohr    [$major, $minor] = explode('.', $_SERVER['RECTOR_MIN_PHP'] ?? '' ?: '8.2');
7826586410SAndreas Gohr    $phpset = LevelSetList::class . '::UP_TO_PHP_' . $major . $minor;
79108f17cfSAndreas Gohr    fwrite(STDERR, "Using PHP set $phpset\n");
8026586410SAndreas Gohr
81077b7fe2SAndreas Gohr    // define sets of rules
82077b7fe2SAndreas Gohr    $rectorConfig->sets([
8326586410SAndreas Gohr        constant($phpset),
84077b7fe2SAndreas Gohr        SetList::CODE_QUALITY,
85077b7fe2SAndreas Gohr        SetList::DEAD_CODE,
86077b7fe2SAndreas Gohr        SetList::CODING_STYLE,
87077b7fe2SAndreas Gohr    ]);
88077b7fe2SAndreas Gohr
89077b7fe2SAndreas Gohr    $rectorConfig->skip([
90077b7fe2SAndreas Gohr        // skip paths
91077b7fe2SAndreas Gohr        __DIR__ . '/../inc/lang/*',
92077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/_test/*',
93077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/_test/*',
94077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/lang/*',
95077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/lang/*',
96467c1427SAndreas Gohr        __DIR__ . '/../lib/plugins/*/conf/*', // maybe later
97467c1427SAndreas Gohr        __DIR__ . '/../lib/tpl/*/conf/*',  // maybe later
98077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/vendor/*',
99077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/vendor/*',
100077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/skel/*', // dev plugin
10124870174SAndreas Gohr        __DIR__ . '/../inc/deprecated.php',
10224870174SAndreas Gohr        __DIR__ . '/../inc/form.php',
103077b7fe2SAndreas Gohr
104077b7fe2SAndreas Gohr        // third party libs, not yet moved to composer
105077b7fe2SAndreas Gohr        __DIR__ . '/../inc/DifferenceEngine.php',
106077b7fe2SAndreas Gohr        __DIR__ . '/../inc/JpegMeta.php',
107077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/authad/adLDAP',
108077b7fe2SAndreas Gohr
109077b7fe2SAndreas Gohr        // skip rules
1101fc5061dSAndreas Gohr        CompleteMissingIfElseBracketRector::class, // keep one-line guardians
111077b7fe2SAndreas Gohr        SimplifyIfElseToTernaryRector::class,
112077b7fe2SAndreas Gohr        NewlineAfterStatementRector::class,
113077b7fe2SAndreas Gohr        CombineIfRector::class,
114077b7fe2SAndreas Gohr        ExplicitBoolCompareRector::class,
115077b7fe2SAndreas Gohr        IssetOnPropertyObjectToPropertyExistsRector::class, // maybe?
116093fe67eSAndreas Gohr        SimplifyQuoteEscapeRector::class,
117077b7fe2SAndreas Gohr        CatchExceptionNameMatchingTypeRector::class,
118077b7fe2SAndreas Gohr        EncapsedStringsToSprintfRector::class,
119077b7fe2SAndreas Gohr        SimplifyUselessVariableRector::class, // seems to strip constructor property initializations
120077b7fe2SAndreas Gohr        DisallowedEmptyRuleFixerRector::class,
1216723156fSAndreas Gohr        RemoveParentCallWithoutParentRector::class,
1226723156fSAndreas Gohr        WrapEncapsedVariableInCurlyBracesRector::class,
1231490c177SAndreas Gohr        SimplifyIfReturnBoolRector::class,
124a469bafbSAndreas Gohr        StrictArraySearchRector::class, // we cannot assume strict search is always wanted
125e2d055f5SAndreas Gohr        JoinStringConcatRector::class, // this does not count variables, so it creates overlong lines
1266e695190SAndreas Gohr        RemoveExtraParametersRector::class, // this actually broke code
127ed46af86SAndreas Gohr        RemoveUnusedConstructorParamRector::class, // see rectorphp/rector#8580
128bcaec9f4SAndreas Gohr        RemoveUnusedNonEmptyArrayBeforeForeachRector::class, // seems unreliable when checking on array keys
12924870174SAndreas Gohr        RemoveAlwaysTrueIfConditionRector::class, // fails with if(defined(...)) constructs
130ab9790caSAndreas Gohr        RemoveUnreachableStatementRector::class, // fails GOTO in authpdo -> should be rewritten with exceptions
13105fd0e83SAndreas Gohr        ReturnNeverTypeRector::class,
13205fd0e83SAndreas Gohr        RemoveUselessParamTagRector::class, // keep doc blocks
13305fd0e83SAndreas Gohr        RemoveUselessVarTagRector::class, // keep doc blocks
13405fd0e83SAndreas Gohr        RemoveUselessReturnTagRector::class, // keep doc blocks
1351e467458SAndreas Gohr        ExplicitReturnNullRector::class, // we sometimes return void or string intentionally
1361e467458SAndreas Gohr        UseIdenticalOverEqualWithSameTypeRector::class, // probably a good idea, maybe later
1378b9fc108SAndreas Gohr        ReduceAlwaysFalseIfOrRector::class, // see rectorphp/rector#8916
138093fe67eSAndreas Gohr        NewlineBetweenClassLikeStmtsRector::class, // looks ugly
139093fe67eSAndreas Gohr        NullToStrictStringFuncCallArgRector::class, // might hide warnings we want to see explicitly
140093fe67eSAndreas Gohr        ClassPropertyAssignToConstructorPromotionRector::class, // not sure I like the syntax, maybe later
141093fe67eSAndreas Gohr        ReplaceBlockToItsStmtsRector::class, // blocks sometimes help readability
142093fe67eSAndreas Gohr        Utf8DecodeEncodeToMbConvertEncodingRector::class, // we probably want our own mapping to the UTF8/* functions
143093fe67eSAndreas Gohr        RemoveDeadIfBlockRector::class, // creates harder to read statements
144894c1577SAndreas Gohr
145*e7dae73bSAndreas Gohr        // keep explicit (int)/(string) casts that document PREG_OFFSET_CAPTURE
146*e7dae73bSAndreas Gohr        // result types for static analysers
147*e7dae73bSAndreas Gohr        RecastingRemovalRector::class => [
148*e7dae73bSAndreas Gohr            __DIR__ . '/../inc/Parsing/Lexer/ParallelRegex.php',
149*e7dae73bSAndreas Gohr        ],
150*e7dae73bSAndreas Gohr
151894c1577SAndreas Gohr        // we're not ready for full type safety. though this rule is probably safe I am not comfortable to add it yet
152894c1577SAndreas Gohr        // https://getrector.com/blog/introducing-safe-and-progressive-strict-type-adoption-rule
153894c1577SAndreas Gohr        SafeDeclareStrictTypesRector::class,
154077b7fe2SAndreas Gohr    ]);
155a0aeafaaSAndreas Gohr
156a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
157a0aeafaaSAndreas Gohr        // see inc/deprecated.php
158a0aeafaaSAndreas Gohr        'RemoteAccessDeniedException' => 'dokuwiki\Remote\AccessDeniedException',
159a0aeafaaSAndreas Gohr        'RemoteException' => 'dokuwiki\Remote\RemoteException',
160a0aeafaaSAndreas Gohr        'setting' => 'dokuwiki\plugin\config\core\Setting\Setting',
161a0aeafaaSAndreas Gohr        'setting_authtype' => 'dokuwiki\plugin\config\core\Setting\SettingAuthtype',
162a0aeafaaSAndreas Gohr        'setting_string' => 'dokuwiki\plugin\config\core\Setting\SettingString',
163a0aeafaaSAndreas Gohr        'PageChangelog' => 'dokuwiki\ChangeLog\PageChangeLog',
164a0aeafaaSAndreas Gohr        'MediaChangelog' => 'dokuwiki\ChangeLog\MediaChangeLog',
165a0aeafaaSAndreas Gohr        'Input' => 'dokuwiki\Input\Input',
166a0aeafaaSAndreas Gohr        'PostInput' => 'dokuwiki\Input\Post',
167a0aeafaaSAndreas Gohr        'GetInput' => 'dokuwiki\Input\Get',
168a0aeafaaSAndreas Gohr        'ServerInput' => 'dokuwiki\Input\Server',
169a0aeafaaSAndreas Gohr        'PassHash' => 'dokuwiki\PassHash',
170a0aeafaaSAndreas Gohr        'HTTPClientException' => 'dokuwiki\HTTP\HTTPClientException',
171a0aeafaaSAndreas Gohr        'HTTPClient' => 'dokuwiki\HTTP\HTTPClient',
172a0aeafaaSAndreas Gohr        'DokuHTTPClient' => 'dokuwiki\HTTP\DokuHTTPClient',
173a0aeafaaSAndreas Gohr        'Doku_Plugin_Controller' => 'dokuwiki\Extension\PluginController',
174a0aeafaaSAndreas Gohr        'Doku_Indexer' => 'dokuwiki\Search\Indexer',
175a0aeafaaSAndreas Gohr        'IXR_Client' => 'dokuwiki\Remote\IXR\Client',
176a0aeafaaSAndreas Gohr        'IXR_ClientMulticall' => 'IXR\Client\ClientMulticall',
177a0aeafaaSAndreas Gohr        'IXR_Server' => 'IXR\Server\Server',
178a0aeafaaSAndreas Gohr        'IXR_IntrospectionServer' => 'IXR\Server\IntrospectionServer',
179a0aeafaaSAndreas Gohr        'IXR_Request' => 'IXR\Request\Request',
180a0aeafaaSAndreas Gohr        'IXR_Message' => 'R\Message\Message',
181a0aeafaaSAndreas Gohr        'IXR_Error' => 'XR\Message\Error',
182a0aeafaaSAndreas Gohr        'IXR_Date' => 'IXR\DataType\Date',
183a0aeafaaSAndreas Gohr        'IXR_Base64' => 'IXR\DataType\Base64',
184a0aeafaaSAndreas Gohr        'IXR_Value' => 'IXR\DataType\Value',
185a0aeafaaSAndreas Gohr
186a0aeafaaSAndreas Gohr        // see inc/legacy.php
187a0aeafaaSAndreas Gohr        'Doku_Event_Handler' => 'dokuwiki\Extension\EventHandler',
188a0aeafaaSAndreas Gohr        'Doku_Event' => 'dokuwiki\Extension\Event',
189a0aeafaaSAndreas Gohr        'DokuWiki_Action_Plugin' => 'dokuwiki\Extension\ActionPlugin',
190a0aeafaaSAndreas Gohr        'DokuWiki_Admin_Plugin' => 'dokuwiki\Extension\AdminPlugin',
191a0aeafaaSAndreas Gohr        'DokuWiki_Auth_Plugin' => 'dokuwiki\Extension\AuthPlugin',
192a0aeafaaSAndreas Gohr        'DokuWiki_CLI_Plugin' => 'dokuwiki\Extension\CLIPlugin',
193a0aeafaaSAndreas Gohr        'DokuWiki_Plugin' => 'dokuwiki\Extension\Plugin',
194a0aeafaaSAndreas Gohr        'DokuWiki_Remote_Plugin' => 'dokuwiki\Extension\RemotePlugin',
195a0aeafaaSAndreas Gohr        'DokuWiki_Syntax_Plugin' => 'dokuwiki\Extension\SyntaxPlugin',
19671096e46SAndreas Gohr        'Doku_Handler' => 'dokuwiki\Parsing\Handler',
197a0aeafaaSAndreas Gohr    ]);
198a0aeafaaSAndreas Gohr
199a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
200a0aeafaaSAndreas Gohr        // see inc/deprecated.php
201a0aeafaaSAndreas Gohr        'Doku_Lexer_Escape' => 'dokuwiki\Parsing\Lexer\Lexer::escape',
20271096e46SAndreas Gohr        'Doku_Handler_Parse_Media' => 'dokuwiki\Parsing\ParserMode\Media::parseMedia',
203a0aeafaaSAndreas Gohr
204a0aeafaaSAndreas Gohr        // see inc/utf8.php
205a0aeafaaSAndreas Gohr        'utf8_isASCII' => 'dokuwiki\Utf8\Clean::isASCII',
206a0aeafaaSAndreas Gohr        'utf8_strip' => 'dokuwiki\Utf8\Clean::strip',
207a0aeafaaSAndreas Gohr        'utf8_check' => 'dokuwiki\Utf8\Clean::isUtf8',
208a0aeafaaSAndreas Gohr        'utf8_basename' => 'dokuwiki\Utf8\PhpString::basename',
209a0aeafaaSAndreas Gohr        'utf8_strlen' => 'dokuwiki\Utf8\PhpString::strlen',
210a0aeafaaSAndreas Gohr        'utf8_substr' => 'dokuwiki\Utf8\PhpString::substr',
211a0aeafaaSAndreas Gohr        'utf8_substr_replace' => 'dokuwiki\Utf8\PhpString::substr_replace',
212a0aeafaaSAndreas Gohr        'utf8_ltrim' => 'dokuwiki\Utf8\PhpString::ltrim',
213a0aeafaaSAndreas Gohr        'utf8_rtrim' => 'dokuwiki\Utf8\PhpString::rtrim',
214a0aeafaaSAndreas Gohr        'utf8_trim' => 'dokuwiki\Utf8\PhpString::trim',
215a0aeafaaSAndreas Gohr        'utf8_strtolower' => 'dokuwiki\Utf8\PhpString::strtolower',
216a0aeafaaSAndreas Gohr        'utf8_strtoupper' => 'dokuwiki\Utf8\PhpString::strtoupper',
217a0aeafaaSAndreas Gohr        'utf8_ucfirst' => 'dokuwiki\Utf8\PhpString::ucfirst',
218a0aeafaaSAndreas Gohr        'utf8_ucwords' => 'dokuwiki\Utf8\PhpString::ucwords',
219a0aeafaaSAndreas Gohr        'utf8_deaccent' => 'dokuwiki\Utf8\Clean::deaccent',
220a0aeafaaSAndreas Gohr        'utf8_romanize' => 'dokuwiki\Utf8\Clean::romanize',
221a0aeafaaSAndreas Gohr        'utf8_stripspecials' => 'dokuwiki\Utf8\Clean::stripspecials',
222a0aeafaaSAndreas Gohr        'utf8_strpos' => 'dokuwiki\Utf8\PhpString::strpos',
223a0aeafaaSAndreas Gohr        'utf8_tohtml' => 'dokuwiki\Utf8\Conversion::toHtml',
224a0aeafaaSAndreas Gohr        'utf8_unhtml' => 'dokuwiki\Utf8\Conversion::fromHtml',
225a0aeafaaSAndreas Gohr        'utf8_to_unicode' => 'dokuwiki\Utf8\Conversion::fromUtf8',
226a0aeafaaSAndreas Gohr        'unicode_to_utf8' => 'dokuwiki\Utf8\Conversion::toUtf8',
227a0aeafaaSAndreas Gohr        'utf8_to_utf16be' => 'dokuwiki\Utf8\Conversion::toUtf16be',
228a0aeafaaSAndreas Gohr        'utf16be_to_utf8' => 'dokuwiki\Utf8\Conversion::fromUtf16be',
229a0aeafaaSAndreas Gohr        'utf8_bad_replace' => 'dokuwiki\Utf8\Clean::replaceBadBytes',
230a0aeafaaSAndreas Gohr        'utf8_correctIdx' => 'dokuwiki\Utf8\Clean::correctIdx',
231a0aeafaaSAndreas Gohr    ]);
232f9a94e78SAndreas Gohr
233f9a94e78SAndreas Gohr    $rectorConfig->rule(DokuWikiPtlnRector::class);
23426dfc232SAndreas Gohr    $rectorConfig->rule(DokuWikiRenamePrintToEcho::class);
235077b7fe2SAndreas Gohr};
236