xref: /dokuwiki/_test/rector.php (revision 2e0b7f745bd19f7e65c90e63b75064a835dbb73d)
1077b7fe2SAndreas Gohr<?php
2077b7fe2SAndreas Gohr
3077b7fe2SAndreas Gohrdeclare(strict_types=1);
4077b7fe2SAndreas Gohr
5*2e0b7f74SAndreas Gohrdefine('DOKU_INC', __DIR__ . '/../');
6*2e0b7f74SAndreas 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;
21077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
226723156fSAndreas Gohruse Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
23a469bafbSAndreas Gohruse Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
24077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
25077b7fe2SAndreas Gohruse Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
26077b7fe2SAndreas Gohruse Rector\Config\RectorConfig;
27ed46af86SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
28077b7fe2SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
2905fd0e83SAndreas Gohruse Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
3024870174SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
31bcaec9f4SAndreas Gohruse Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
32104a3b7cSAndreas Gohruse Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
336723156fSAndreas Gohruse Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
3424870174SAndreas Gohruse Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
356e695190SAndreas Gohruse Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
3680d85403SAndreas Gohruse Rector\Php80\Rector\Identical\StrEndsWithRector;
3780d85403SAndreas Gohruse Rector\Php80\Rector\Identical\StrStartsWithRector;
3880d85403SAndreas Gohruse Rector\Php80\Rector\NotIdentical\StrContainsRector;
39a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
40a0aeafaaSAndreas Gohruse Rector\Renaming\Rector\Name\RenameClassRector;
41077b7fe2SAndreas Gohruse Rector\Set\ValueObject\LevelSetList;
42077b7fe2SAndreas Gohruse Rector\Set\ValueObject\SetList;
43077b7fe2SAndreas Gohruse Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4405fd0e83SAndreas Gohruse Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
458b9fc108SAndreas Gohruse Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
46077b7fe2SAndreas Gohr
47077b7fe2SAndreas Gohrreturn static function (RectorConfig $rectorConfig): void {
48f9a94e78SAndreas Gohr    // FIXME we may want to autoload these later
49f9a94e78SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiPtlnRector.php';
5026dfc232SAndreas Gohr    require_once __DIR__ . '/rector/DokuWikiRenamePrintToEcho.php';
51f9a94e78SAndreas Gohr
52462a5601SAndreas Gohr    // tune parallel task settings (see rectorphp/rector#8396)
53462a5601SAndreas Gohr    $rectorConfig->parallel(120, 16, 10);
54462a5601SAndreas Gohr
55077b7fe2SAndreas Gohr    $rectorConfig->paths([
56b1f206e1SAndreas Gohr        __DIR__ . '/../inc/',
57b1f206e1SAndreas Gohr        __DIR__ . '/../lib/',
58b1f206e1SAndreas Gohr        __DIR__ . '/../bin/',
59b1f206e1SAndreas Gohr        __DIR__ . '/../*.php',
60077b7fe2SAndreas Gohr    ]);
61077b7fe2SAndreas Gohr
621490c177SAndreas Gohr    $rectorConfig->bootstrapFiles([
6324870174SAndreas Gohr        __DIR__ . '/../inc/init.php',
641490c177SAndreas Gohr    ]);
651490c177SAndreas Gohr
66077b7fe2SAndreas Gohr    $rectorConfig->importNames();
67077b7fe2SAndreas Gohr    $rectorConfig->importShortClasses(false);
68077b7fe2SAndreas Gohr    $rectorConfig->cacheClass(FileCacheStorage::class);
69077b7fe2SAndreas Gohr    $rectorConfig->cacheDirectory(__DIR__ . '/.rector-cache');
70077b7fe2SAndreas Gohr
7126586410SAndreas Gohr    // supported minimum PHP version can be overridden by environment variable
72bac51761SAndreas Gohr    [$major, $minor] = explode('.', $_SERVER['RECTOR_MIN_PHP'] ?? '' ?: '7.4');
7326586410SAndreas Gohr    $phpset = LevelSetList::class . '::UP_TO_PHP_' . $major . $minor;
74108f17cfSAndreas Gohr    fwrite(STDERR, "Using PHP set $phpset\n");
7526586410SAndreas Gohr
76077b7fe2SAndreas Gohr    // define sets of rules
77077b7fe2SAndreas Gohr    $rectorConfig->sets([
7826586410SAndreas Gohr        constant($phpset),
79077b7fe2SAndreas Gohr        SetList::CODE_QUALITY,
80077b7fe2SAndreas Gohr        SetList::DEAD_CODE,
81077b7fe2SAndreas Gohr        SetList::CODING_STYLE,
82077b7fe2SAndreas Gohr    ]);
83077b7fe2SAndreas Gohr
8480d85403SAndreas Gohr    // future rules for which we have polyfills
8580d85403SAndreas Gohr    $rectorConfig->rule(StrContainsRector::class);
8680d85403SAndreas Gohr    $rectorConfig->rule(StrEndsWithRector::class);
8780d85403SAndreas Gohr    $rectorConfig->rule(StrStartsWithRector::class);
8880d85403SAndreas 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?
116077b7fe2SAndreas Gohr        SymplifyQuoteEscapeRector::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
1381e467458SAndreas Gohr
139077b7fe2SAndreas Gohr    ]);
140a0aeafaaSAndreas Gohr
141a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
142a0aeafaaSAndreas Gohr        // see inc/deprecated.php
143a0aeafaaSAndreas Gohr        'RemoteAccessDeniedException' => 'dokuwiki\Remote\AccessDeniedException',
144a0aeafaaSAndreas Gohr        'RemoteException' => 'dokuwiki\Remote\RemoteException',
145a0aeafaaSAndreas Gohr        'setting' => 'dokuwiki\plugin\config\core\Setting\Setting',
146a0aeafaaSAndreas Gohr        'setting_authtype' => 'dokuwiki\plugin\config\core\Setting\SettingAuthtype',
147a0aeafaaSAndreas Gohr        'setting_string' => 'dokuwiki\plugin\config\core\Setting\SettingString',
148a0aeafaaSAndreas Gohr        'PageChangelog' => 'dokuwiki\ChangeLog\PageChangeLog',
149a0aeafaaSAndreas Gohr        'MediaChangelog' => 'dokuwiki\ChangeLog\MediaChangeLog',
150a0aeafaaSAndreas Gohr        'Input' => 'dokuwiki\Input\Input',
151a0aeafaaSAndreas Gohr        'PostInput' => 'dokuwiki\Input\Post',
152a0aeafaaSAndreas Gohr        'GetInput' => 'dokuwiki\Input\Get',
153a0aeafaaSAndreas Gohr        'ServerInput' => 'dokuwiki\Input\Server',
154a0aeafaaSAndreas Gohr        'PassHash' => 'dokuwiki\PassHash',
155a0aeafaaSAndreas Gohr        'HTTPClientException' => 'dokuwiki\HTTP\HTTPClientException',
156a0aeafaaSAndreas Gohr        'HTTPClient' => 'dokuwiki\HTTP\HTTPClient',
157a0aeafaaSAndreas Gohr        'DokuHTTPClient' => 'dokuwiki\HTTP\DokuHTTPClient',
158a0aeafaaSAndreas Gohr        'Doku_Plugin_Controller' => 'dokuwiki\Extension\PluginController',
159a0aeafaaSAndreas Gohr        'Doku_Indexer' => 'dokuwiki\Search\Indexer',
160a0aeafaaSAndreas Gohr        'IXR_Client' => 'dokuwiki\Remote\IXR\Client',
161a0aeafaaSAndreas Gohr        'IXR_ClientMulticall' => 'IXR\Client\ClientMulticall',
162a0aeafaaSAndreas Gohr        'IXR_Server' => 'IXR\Server\Server',
163a0aeafaaSAndreas Gohr        'IXR_IntrospectionServer' => 'IXR\Server\IntrospectionServer',
164a0aeafaaSAndreas Gohr        'IXR_Request' => 'IXR\Request\Request',
165a0aeafaaSAndreas Gohr        'IXR_Message' => 'R\Message\Message',
166a0aeafaaSAndreas Gohr        'IXR_Error' => 'XR\Message\Error',
167a0aeafaaSAndreas Gohr        'IXR_Date' => 'IXR\DataType\Date',
168a0aeafaaSAndreas Gohr        'IXR_Base64' => 'IXR\DataType\Base64',
169a0aeafaaSAndreas Gohr        'IXR_Value' => 'IXR\DataType\Value',
170a0aeafaaSAndreas Gohr
171a0aeafaaSAndreas Gohr        // see inc/legacy.php
172a0aeafaaSAndreas Gohr        'Doku_Event_Handler' => 'dokuwiki\Extension\EventHandler',
173a0aeafaaSAndreas Gohr        'Doku_Event' => 'dokuwiki\Extension\Event',
174a0aeafaaSAndreas Gohr        'DokuWiki_Action_Plugin' => 'dokuwiki\Extension\ActionPlugin',
175a0aeafaaSAndreas Gohr        'DokuWiki_Admin_Plugin' => 'dokuwiki\Extension\AdminPlugin',
176a0aeafaaSAndreas Gohr        'DokuWiki_Auth_Plugin' => 'dokuwiki\Extension\AuthPlugin',
177a0aeafaaSAndreas Gohr        'DokuWiki_CLI_Plugin' => 'dokuwiki\Extension\CLIPlugin',
178a0aeafaaSAndreas Gohr        'DokuWiki_Plugin' => 'dokuwiki\Extension\Plugin',
179a0aeafaaSAndreas Gohr        'DokuWiki_Remote_Plugin' => 'dokuwiki\Extension\RemotePlugin',
180a0aeafaaSAndreas Gohr        'DokuWiki_Syntax_Plugin' => 'dokuwiki\Extension\SyntaxPlugin',
181a0aeafaaSAndreas Gohr    ]);
182a0aeafaaSAndreas Gohr
183a0aeafaaSAndreas Gohr    $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
184a0aeafaaSAndreas Gohr        // see inc/deprecated.php
185a0aeafaaSAndreas Gohr        'Doku_Lexer_Escape' => 'dokuwiki\Parsing\Lexer\Lexer::escape',
186a0aeafaaSAndreas Gohr
187a0aeafaaSAndreas Gohr        // see inc/utf8.php
188a0aeafaaSAndreas Gohr        'utf8_isASCII' => 'dokuwiki\Utf8\Clean::isASCII',
189a0aeafaaSAndreas Gohr        'utf8_strip' => 'dokuwiki\Utf8\Clean::strip',
190a0aeafaaSAndreas Gohr        'utf8_check' => 'dokuwiki\Utf8\Clean::isUtf8',
191a0aeafaaSAndreas Gohr        'utf8_basename' => 'dokuwiki\Utf8\PhpString::basename',
192a0aeafaaSAndreas Gohr        'utf8_strlen' => 'dokuwiki\Utf8\PhpString::strlen',
193a0aeafaaSAndreas Gohr        'utf8_substr' => 'dokuwiki\Utf8\PhpString::substr',
194a0aeafaaSAndreas Gohr        'utf8_substr_replace' => 'dokuwiki\Utf8\PhpString::substr_replace',
195a0aeafaaSAndreas Gohr        'utf8_ltrim' => 'dokuwiki\Utf8\PhpString::ltrim',
196a0aeafaaSAndreas Gohr        'utf8_rtrim' => 'dokuwiki\Utf8\PhpString::rtrim',
197a0aeafaaSAndreas Gohr        'utf8_trim' => 'dokuwiki\Utf8\PhpString::trim',
198a0aeafaaSAndreas Gohr        'utf8_strtolower' => 'dokuwiki\Utf8\PhpString::strtolower',
199a0aeafaaSAndreas Gohr        'utf8_strtoupper' => 'dokuwiki\Utf8\PhpString::strtoupper',
200a0aeafaaSAndreas Gohr        'utf8_ucfirst' => 'dokuwiki\Utf8\PhpString::ucfirst',
201a0aeafaaSAndreas Gohr        'utf8_ucwords' => 'dokuwiki\Utf8\PhpString::ucwords',
202a0aeafaaSAndreas Gohr        'utf8_deaccent' => 'dokuwiki\Utf8\Clean::deaccent',
203a0aeafaaSAndreas Gohr        'utf8_romanize' => 'dokuwiki\Utf8\Clean::romanize',
204a0aeafaaSAndreas Gohr        'utf8_stripspecials' => 'dokuwiki\Utf8\Clean::stripspecials',
205a0aeafaaSAndreas Gohr        'utf8_strpos' => 'dokuwiki\Utf8\PhpString::strpos',
206a0aeafaaSAndreas Gohr        'utf8_tohtml' => 'dokuwiki\Utf8\Conversion::toHtml',
207a0aeafaaSAndreas Gohr        'utf8_unhtml' => 'dokuwiki\Utf8\Conversion::fromHtml',
208a0aeafaaSAndreas Gohr        'utf8_to_unicode' => 'dokuwiki\Utf8\Conversion::fromUtf8',
209a0aeafaaSAndreas Gohr        'unicode_to_utf8' => 'dokuwiki\Utf8\Conversion::toUtf8',
210a0aeafaaSAndreas Gohr        'utf8_to_utf16be' => 'dokuwiki\Utf8\Conversion::toUtf16be',
211a0aeafaaSAndreas Gohr        'utf16be_to_utf8' => 'dokuwiki\Utf8\Conversion::fromUtf16be',
212a0aeafaaSAndreas Gohr        'utf8_bad_replace' => 'dokuwiki\Utf8\Clean::replaceBadBytes',
213a0aeafaaSAndreas Gohr        'utf8_correctIdx' => 'dokuwiki\Utf8\Clean::correctIdx',
214a0aeafaaSAndreas Gohr    ]);
215f9a94e78SAndreas Gohr
216f9a94e78SAndreas Gohr    $rectorConfig->rule(DokuWikiPtlnRector::class);
21726dfc232SAndreas Gohr    $rectorConfig->rule(DokuWikiRenamePrintToEcho::class);
218077b7fe2SAndreas Gohr};
219