xref: /dokuwiki/_test/rector.php (revision 8b9fc1084fcd4007f277703c77c81fc57865b945)
1077b7fe2SAndreas Gohr<?php
2077b7fe2SAndreas Gohr
3077b7fe2SAndreas Gohrdeclare(strict_types=1);
4077b7fe2SAndreas Gohr
5f9a94e78SAndreas Gohruse dokuwiki\test\rector\DokuWikiPtlnRector;
626dfc232SAndreas Gohruse dokuwiki\test\rector\DokuWikiRenamePrintToEcho;
7077b7fe2SAndreas Gohruse Rector\Caching\ValueObject\Storage\FileCacheStorage;
81e467458SAndreas Gohruse Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector;
9e2d055f5SAndreas Gohruse Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
101e467458SAndreas Gohruse Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector;
11077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
12077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\CombineIfRector;
131fc5061dSAndreas Gohruse Rector\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector;
14077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
15077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
161490c177SAndreas Gohruse Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
17077b7fe2SAndreas Gohruse Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector;
18077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
19077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
206723156fSAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
21a469bafbSAndreas Gohruse Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
22077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
23077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
24077b7fe2SAndreas Gohruse Rector\Config\RectorConfig;
25ed46af86SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
26077b7fe2SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
2705fd0e83SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
2824870174SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
29bcaec9f4SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
30104a3b7cSAndreas Gohruse Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
316723156fSAndreas Gohruse Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
3224870174SAndreas Gohruse Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
336e695190SAndreas Gohruse Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
3480d85403SAndreas Gohruse Rector\Php80\Rector\Identical\StrEndsWithRector;
3580d85403SAndreas Gohruse Rector\Php80\Rector\Identical\StrStartsWithRector;
3680d85403SAndreas Gohruse Rector\Php80\Rector\NotIdentical\StrContainsRector;
37a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
38a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\Name\RenameClassRector;
39077b7fe2SAndreas Gohruse Rector\Set\ValueObject\LevelSetList;
40077b7fe2SAndreas Gohruse Rector\Set\ValueObject\SetList;
41077b7fe2SAndreas Gohruse Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4205fd0e83SAndreas Gohruse Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
43*8b9fc108SAndreas Gohruse Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
44077b7fe2SAndreas Gohr
45077b7fe2SAndreas Gohrreturn static function (RectorConfig $rectorConfig): void {
46f9a94e78SAndreas Gohr    // FIXME we may want to autoload these later
47f9a94e78SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiPtlnRector.php';
4826dfc232SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiRenamePrintToEcho.php';
49f9a94e78SAndreas Gohr
50462a5601SAndreas Gohr    // tune parallel task settings (see rectorphp/rector#8396)
51462a5601SAndreas Gohr    $rectorConfig->parallel(120, 16, 10);
52462a5601SAndreas Gohr
53077b7fe2SAndreas Gohr    $rectorConfig->paths([
54b1f206e1SAndreas Gohr        __DIR__ . '/../inc/',
55b1f206e1SAndreas Gohr        __DIR__ . '/../lib/',
56b1f206e1SAndreas Gohr        __DIR__ . '/../bin/',
57b1f206e1SAndreas Gohr        __DIR__ . '/../*.php',
58077b7fe2SAndreas Gohr    ]);
59077b7fe2SAndreas Gohr
601490c177SAndreas Gohr    $rectorConfig->bootstrapFiles([
6124870174SAndreas Gohr        __DIR__ . '/../inc/init.php',
621490c177SAndreas Gohr    ]);
631490c177SAndreas Gohr
64077b7fe2SAndreas Gohr    $rectorConfig->importNames();
65077b7fe2SAndreas Gohr    $rectorConfig->importShortClasses(false);
66077b7fe2SAndreas Gohr    $rectorConfig->cacheClass(FileCacheStorage::class);
67077b7fe2SAndreas Gohr    $rectorConfig->cacheDirectory(__DIR__ . '/.rector-cache');
68077b7fe2SAndreas Gohr
6926586410SAndreas Gohr    // supported minimum PHP version can be overridden by environment variable
70bac51761SAndreas Gohr    [$major, $minor] = explode('.', $_SERVER['RECTOR_MIN_PHP'] ?? '' ?: '7.4');
7126586410SAndreas Gohr    $phpset = LevelSetList::class . '::UP_TO_PHP_' . $major . $minor;
72108f17cfSAndreas Gohr    fwrite(STDERR, "Using PHP set $phpset\n");
7326586410SAndreas Gohr
74077b7fe2SAndreas Gohr    // define sets of rules
75077b7fe2SAndreas Gohr    $rectorConfig->sets([
7626586410SAndreas Gohr        constant($phpset),
77077b7fe2SAndreas Gohr        SetList::CODE_QUALITY,
78077b7fe2SAndreas Gohr        SetList::DEAD_CODE,
79077b7fe2SAndreas Gohr        SetList::CODING_STYLE,
80077b7fe2SAndreas Gohr    ]);
81077b7fe2SAndreas Gohr
8280d85403SAndreas Gohr    // future rules for which we have polyfills
8380d85403SAndreas Gohr    $rectorConfig->rule(StrContainsRector::class);
8480d85403SAndreas Gohr    $rectorConfig->rule(StrEndsWithRector::class);
8580d85403SAndreas Gohr    $rectorConfig->rule(StrStartsWithRector::class);
8680d85403SAndreas Gohr
87077b7fe2SAndreas Gohr    $rectorConfig->skip([
88077b7fe2SAndreas Gohr        // skip paths
89077b7fe2SAndreas Gohr        __DIR__ . '/../inc/lang/*',
90077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/_test/*',
91077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/_test/*',
92077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/lang/*',
93077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/lang/*',
94467c1427SAndreas Gohr        __DIR__ . '/../lib/plugins/*/conf/*', // maybe later
95467c1427SAndreas Gohr        __DIR__ . '/../lib/tpl/*/conf/*',  // maybe later
96077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/vendor/*',
97077b7fe2SAndreas Gohr        __DIR__ . '/../lib/tpl/*/vendor/*',
98077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/*/skel/*', // dev plugin
9924870174SAndreas Gohr        __DIR__ . '/../inc/deprecated.php',
10024870174SAndreas Gohr        __DIR__ . '/../inc/form.php',
101077b7fe2SAndreas Gohr
102077b7fe2SAndreas Gohr        // third party libs, not yet moved to composer
103077b7fe2SAndreas Gohr        __DIR__ . '/../inc/DifferenceEngine.php',
104077b7fe2SAndreas Gohr        __DIR__ . '/../inc/JpegMeta.php',
105077b7fe2SAndreas Gohr        __DIR__ . '/../lib/plugins/authad/adLDAP',
106077b7fe2SAndreas Gohr
107077b7fe2SAndreas Gohr        // skip rules
1081fc5061dSAndreas Gohr        CompleteMissingIfElseBracketRector::class, // keep one-line guardians
109077b7fe2SAndreas Gohr        SimplifyIfElseToTernaryRector::class,
110077b7fe2SAndreas Gohr        NewlineAfterStatementRector::class,
111077b7fe2SAndreas Gohr        CombineIfRector::class,
112077b7fe2SAndreas Gohr        ExplicitBoolCompareRector::class,
113077b7fe2SAndreas Gohr        IssetOnPropertyObjectToPropertyExistsRector::class, // maybe?
114077b7fe2SAndreas Gohr        SymplifyQuoteEscapeRector::class,
115077b7fe2SAndreas Gohr        CatchExceptionNameMatchingTypeRector::class,
116077b7fe2SAndreas Gohr        EncapsedStringsToSprintfRector::class,
117077b7fe2SAndreas Gohr        SimplifyUselessVariableRector::class, // seems to strip constructor property initializations
118077b7fe2SAndreas Gohr        DisallowedEmptyRuleFixerRector::class,
1196723156fSAndreas Gohr        RemoveParentCallWithoutParentRector::class,
1206723156fSAndreas Gohr        WrapEncapsedVariableInCurlyBracesRector::class,
1211490c177SAndreas Gohr        SimplifyIfReturnBoolRector::class,
122a469bafbSAndreas Gohr        StrictArraySearchRector::class, // we cannot assume strict search is always wanted
123e2d055f5SAndreas Gohr        JoinStringConcatRector::class, // this does not count variables, so it creates overlong lines
1246e695190SAndreas Gohr        RemoveExtraParametersRector::class, // this actually broke code
125ed46af86SAndreas Gohr        RemoveUnusedConstructorParamRector::class, // see rectorphp/rector#8580
126bcaec9f4SAndreas Gohr        RemoveUnusedNonEmptyArrayBeforeForeachRector::class, // seems unreliable when checking on array keys
12724870174SAndreas Gohr        RemoveAlwaysTrueIfConditionRector::class, // fails with if(defined(...)) constructs
128ab9790caSAndreas Gohr        RemoveUnreachableStatementRector::class, // fails GOTO in authpdo -> should be rewritten with exceptions
12905fd0e83SAndreas Gohr        ReturnNeverTypeRector::class,
13005fd0e83SAndreas Gohr        RemoveUselessParamTagRector::class, // keep doc blocks
13105fd0e83SAndreas Gohr        RemoveUselessVarTagRector::class, // keep doc blocks
13205fd0e83SAndreas Gohr        RemoveUselessReturnTagRector::class, // keep doc blocks
1331e467458SAndreas Gohr        ExplicitReturnNullRector::class, // we sometimes return void or string intentionally
1341e467458SAndreas Gohr        UseIdenticalOverEqualWithSameTypeRector::class, // probably a good idea, maybe later
135*8b9fc108SAndreas Gohr        ReduceAlwaysFalseIfOrRector::class, // see rectorphp/rector#8916
1361e467458SAndreas Gohr
137077b7fe2SAndreas Gohr    ]);
138a0aeafaaSAndreas Gohr
139a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
140a0aeafaaSAndreas Gohr        // see inc/deprecated.php
141a0aeafaaSAndreas Gohr        'RemoteAccessDeniedException' => 'dokuwiki\Remote\AccessDeniedException',
142a0aeafaaSAndreas Gohr        'RemoteException' => 'dokuwiki\Remote\RemoteException',
143a0aeafaaSAndreas Gohr        'setting' => 'dokuwiki\plugin\config\core\Setting\Setting',
144a0aeafaaSAndreas Gohr        'setting_authtype' => 'dokuwiki\plugin\config\core\Setting\SettingAuthtype',
145a0aeafaaSAndreas Gohr        'setting_string' => 'dokuwiki\plugin\config\core\Setting\SettingString',
146a0aeafaaSAndreas Gohr        'PageChangelog' => 'dokuwiki\ChangeLog\PageChangeLog',
147a0aeafaaSAndreas Gohr        'MediaChangelog' => 'dokuwiki\ChangeLog\MediaChangeLog',
148a0aeafaaSAndreas Gohr        'Input' => 'dokuwiki\Input\Input',
149a0aeafaaSAndreas Gohr        'PostInput' => 'dokuwiki\Input\Post',
150a0aeafaaSAndreas Gohr        'GetInput' => 'dokuwiki\Input\Get',
151a0aeafaaSAndreas Gohr        'ServerInput' => 'dokuwiki\Input\Server',
152a0aeafaaSAndreas Gohr        'PassHash' => 'dokuwiki\PassHash',
153a0aeafaaSAndreas Gohr        'HTTPClientException' => 'dokuwiki\HTTP\HTTPClientException',
154a0aeafaaSAndreas Gohr        'HTTPClient' => 'dokuwiki\HTTP\HTTPClient',
155a0aeafaaSAndreas Gohr        'DokuHTTPClient' => 'dokuwiki\HTTP\DokuHTTPClient',
156a0aeafaaSAndreas Gohr        'Doku_Plugin_Controller' => 'dokuwiki\Extension\PluginController',
157a0aeafaaSAndreas Gohr        'Doku_Indexer' => 'dokuwiki\Search\Indexer',
158a0aeafaaSAndreas Gohr        'IXR_Client' => 'dokuwiki\Remote\IXR\Client',
159a0aeafaaSAndreas Gohr        'IXR_ClientMulticall' => 'IXR\Client\ClientMulticall',
160a0aeafaaSAndreas Gohr        'IXR_Server' => 'IXR\Server\Server',
161a0aeafaaSAndreas Gohr        'IXR_IntrospectionServer' => 'IXR\Server\IntrospectionServer',
162a0aeafaaSAndreas Gohr        'IXR_Request' => 'IXR\Request\Request',
163a0aeafaaSAndreas Gohr        'IXR_Message' => 'R\Message\Message',
164a0aeafaaSAndreas Gohr        'IXR_Error' => 'XR\Message\Error',
165a0aeafaaSAndreas Gohr        'IXR_Date' => 'IXR\DataType\Date',
166a0aeafaaSAndreas Gohr        'IXR_Base64' => 'IXR\DataType\Base64',
167a0aeafaaSAndreas Gohr        'IXR_Value' => 'IXR\DataType\Value',
168a0aeafaaSAndreas Gohr
169a0aeafaaSAndreas Gohr        // see inc/legacy.php
170a0aeafaaSAndreas Gohr        'Doku_Event_Handler' => 'dokuwiki\Extension\EventHandler',
171a0aeafaaSAndreas Gohr        'Doku_Event' => 'dokuwiki\Extension\Event',
172a0aeafaaSAndreas Gohr        'DokuWiki_Action_Plugin' => 'dokuwiki\Extension\ActionPlugin',
173a0aeafaaSAndreas Gohr        'DokuWiki_Admin_Plugin' => 'dokuwiki\Extension\AdminPlugin',
174a0aeafaaSAndreas Gohr        'DokuWiki_Auth_Plugin' => 'dokuwiki\Extension\AuthPlugin',
175a0aeafaaSAndreas Gohr        'DokuWiki_CLI_Plugin' => 'dokuwiki\Extension\CLIPlugin',
176a0aeafaaSAndreas Gohr        'DokuWiki_Plugin' => 'dokuwiki\Extension\Plugin',
177a0aeafaaSAndreas Gohr        'DokuWiki_Remote_Plugin' => 'dokuwiki\Extension\RemotePlugin',
178a0aeafaaSAndreas Gohr        'DokuWiki_Syntax_Plugin' => 'dokuwiki\Extension\SyntaxPlugin',
179a0aeafaaSAndreas Gohr    ]);
180a0aeafaaSAndreas Gohr
181a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
182a0aeafaaSAndreas Gohr        // see inc/deprecated.php
183a0aeafaaSAndreas Gohr        'Doku_Lexer_Escape' => 'dokuwiki\Parsing\Lexer\Lexer::escape',
184a0aeafaaSAndreas Gohr
185a0aeafaaSAndreas Gohr        // see inc/utf8.php
186a0aeafaaSAndreas Gohr        'utf8_isASCII' => 'dokuwiki\Utf8\Clean::isASCII',
187a0aeafaaSAndreas Gohr        'utf8_strip' => 'dokuwiki\Utf8\Clean::strip',
188a0aeafaaSAndreas Gohr        'utf8_check' => 'dokuwiki\Utf8\Clean::isUtf8',
189a0aeafaaSAndreas Gohr        'utf8_basename' => 'dokuwiki\Utf8\PhpString::basename',
190a0aeafaaSAndreas Gohr        'utf8_strlen' => 'dokuwiki\Utf8\PhpString::strlen',
191a0aeafaaSAndreas Gohr        'utf8_substr' => 'dokuwiki\Utf8\PhpString::substr',
192a0aeafaaSAndreas Gohr        'utf8_substr_replace' => 'dokuwiki\Utf8\PhpString::substr_replace',
193a0aeafaaSAndreas Gohr        'utf8_ltrim' => 'dokuwiki\Utf8\PhpString::ltrim',
194a0aeafaaSAndreas Gohr        'utf8_rtrim' => 'dokuwiki\Utf8\PhpString::rtrim',
195a0aeafaaSAndreas Gohr        'utf8_trim' => 'dokuwiki\Utf8\PhpString::trim',
196a0aeafaaSAndreas Gohr        'utf8_strtolower' => 'dokuwiki\Utf8\PhpString::strtolower',
197a0aeafaaSAndreas Gohr        'utf8_strtoupper' => 'dokuwiki\Utf8\PhpString::strtoupper',
198a0aeafaaSAndreas Gohr        'utf8_ucfirst' => 'dokuwiki\Utf8\PhpString::ucfirst',
199a0aeafaaSAndreas Gohr        'utf8_ucwords' => 'dokuwiki\Utf8\PhpString::ucwords',
200a0aeafaaSAndreas Gohr        'utf8_deaccent' => 'dokuwiki\Utf8\Clean::deaccent',
201a0aeafaaSAndreas Gohr        'utf8_romanize' => 'dokuwiki\Utf8\Clean::romanize',
202a0aeafaaSAndreas Gohr        'utf8_stripspecials' => 'dokuwiki\Utf8\Clean::stripspecials',
203a0aeafaaSAndreas Gohr        'utf8_strpos' => 'dokuwiki\Utf8\PhpString::strpos',
204a0aeafaaSAndreas Gohr        'utf8_tohtml' => 'dokuwiki\Utf8\Conversion::toHtml',
205a0aeafaaSAndreas Gohr        'utf8_unhtml' => 'dokuwiki\Utf8\Conversion::fromHtml',
206a0aeafaaSAndreas Gohr        'utf8_to_unicode' => 'dokuwiki\Utf8\Conversion::fromUtf8',
207a0aeafaaSAndreas Gohr        'unicode_to_utf8' => 'dokuwiki\Utf8\Conversion::toUtf8',
208a0aeafaaSAndreas Gohr        'utf8_to_utf16be' => 'dokuwiki\Utf8\Conversion::toUtf16be',
209a0aeafaaSAndreas Gohr        'utf16be_to_utf8' => 'dokuwiki\Utf8\Conversion::fromUtf16be',
210a0aeafaaSAndreas Gohr        'utf8_bad_replace' => 'dokuwiki\Utf8\Clean::replaceBadBytes',
211a0aeafaaSAndreas Gohr        'utf8_correctIdx' => 'dokuwiki\Utf8\Clean::correctIdx',
212a0aeafaaSAndreas Gohr    ]);
213f9a94e78SAndreas Gohr
214f9a94e78SAndreas Gohr    $rectorConfig->rule(DokuWikiPtlnRector::class);
21526dfc232SAndreas Gohr    $rectorConfig->rule(DokuWikiRenamePrintToEcho::class);
216077b7fe2SAndreas Gohr};
217