1<?php 2 3declare(strict_types=1); 4 5define('DOKU_INC', __DIR__ . '/../'); 6 7use dokuwiki\test\rector\DokuWikiPtlnRector; 8use dokuwiki\test\rector\DokuWikiRenamePrintToEcho; 9use Rector\Caching\ValueObject\Storage\FileCacheStorage; 10use Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector; 11use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector; 12use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector; 13use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; 14use Rector\CodeQuality\Rector\If_\CombineIfRector; 15use Rector\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector; 16use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector; 17use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector; 18use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector; 19use Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector; 20use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector; 21use Rector\CodingStyle\Rector\ClassLike\NewlineBetweenClassLikeStmtsRector; 22use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector; 23use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector; 24use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector; 25use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector; 26use Rector\CodingStyle\Rector\String_\SimplifyQuoteEscapeRector; 27use Rector\Config\RectorConfig; 28use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector; 29use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector; 30use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector; 31use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector; 32use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector; 33use Rector\DeadCode\Rector\If_\RemoveDeadIfBlockRector; 34use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector; 35use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector; 36use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector; 37use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector; 38use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector; 39use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; 40use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector; 41use Rector\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector; 42use Rector\Renaming\Rector\FuncCall\RenameFunctionRector; 43use Rector\Renaming\Rector\Name\RenameClassRector; 44use Rector\Set\ValueObject\LevelSetList; 45use Rector\Set\ValueObject\SetList; 46use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; 47use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector; 48use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector; 49use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector; 50 51return static function (RectorConfig $rectorConfig): void { 52 // FIXME we may want to autoload these later 53 require_once __DIR__ . '/rector/DokuWikiPtlnRector.php'; 54 require_once __DIR__ . '/rector/DokuWikiRenamePrintToEcho.php'; 55 56 // tune parallel task settings (see rectorphp/rector#8396) 57 $rectorConfig->parallel(120, 16, 10); 58 59 $rectorConfig->paths([ 60 __DIR__ . '/../inc/', 61 __DIR__ . '/../lib/', 62 __DIR__ . '/../bin/', 63 __DIR__ . '/../*.php', 64 ]); 65 66 $rectorConfig->bootstrapFiles([ 67 __DIR__ . '/../inc/init.php', 68 ]); 69 70 $rectorConfig->importNames(); 71 $rectorConfig->importShortClasses(false); 72 $rectorConfig->cacheClass(FileCacheStorage::class); 73 $rectorConfig->cacheDirectory(__DIR__ . '/.rector-cache'); 74 75 // supported minimum PHP version can be overridden by environment variable 76 [$major, $minor] = explode('.', $_SERVER['RECTOR_MIN_PHP'] ?? '' ?: '8.2'); 77 $phpset = LevelSetList::class . '::UP_TO_PHP_' . $major . $minor; 78 fwrite(STDERR, "Using PHP set $phpset\n"); 79 80 // define sets of rules 81 $rectorConfig->sets([ 82 constant($phpset), 83 SetList::CODE_QUALITY, 84 SetList::DEAD_CODE, 85 SetList::CODING_STYLE, 86 ]); 87 88 $rectorConfig->skip([ 89 // skip paths 90 __DIR__ . '/../inc/lang/*', 91 __DIR__ . '/../lib/plugins/*/_test/*', 92 __DIR__ . '/../lib/tpl/*/_test/*', 93 __DIR__ . '/../lib/plugins/*/lang/*', 94 __DIR__ . '/../lib/tpl/*/lang/*', 95 __DIR__ . '/../lib/plugins/*/conf/*', // maybe later 96 __DIR__ . '/../lib/tpl/*/conf/*', // maybe later 97 __DIR__ . '/../lib/plugins/*/vendor/*', 98 __DIR__ . '/../lib/tpl/*/vendor/*', 99 __DIR__ . '/../lib/plugins/*/skel/*', // dev plugin 100 __DIR__ . '/../inc/deprecated.php', 101 __DIR__ . '/../inc/form.php', 102 103 // third party libs, not yet moved to composer 104 __DIR__ . '/../inc/DifferenceEngine.php', 105 __DIR__ . '/../inc/JpegMeta.php', 106 __DIR__ . '/../lib/plugins/authad/adLDAP', 107 108 // skip rules 109 CompleteMissingIfElseBracketRector::class, // keep one-line guardians 110 SimplifyIfElseToTernaryRector::class, 111 NewlineAfterStatementRector::class, 112 CombineIfRector::class, 113 ExplicitBoolCompareRector::class, 114 IssetOnPropertyObjectToPropertyExistsRector::class, // maybe? 115 SimplifyQuoteEscapeRector::class, 116 CatchExceptionNameMatchingTypeRector::class, 117 EncapsedStringsToSprintfRector::class, 118 SimplifyUselessVariableRector::class, // seems to strip constructor property initializations 119 DisallowedEmptyRuleFixerRector::class, 120 RemoveParentCallWithoutParentRector::class, 121 WrapEncapsedVariableInCurlyBracesRector::class, 122 SimplifyIfReturnBoolRector::class, 123 StrictArraySearchRector::class, // we cannot assume strict search is always wanted 124 JoinStringConcatRector::class, // this does not count variables, so it creates overlong lines 125 RemoveExtraParametersRector::class, // this actually broke code 126 RemoveUnusedConstructorParamRector::class, // see rectorphp/rector#8580 127 RemoveUnusedNonEmptyArrayBeforeForeachRector::class, // seems unreliable when checking on array keys 128 RemoveAlwaysTrueIfConditionRector::class, // fails with if(defined(...)) constructs 129 RemoveUnreachableStatementRector::class, // fails GOTO in authpdo -> should be rewritten with exceptions 130 ReturnNeverTypeRector::class, 131 RemoveUselessParamTagRector::class, // keep doc blocks 132 RemoveUselessVarTagRector::class, // keep doc blocks 133 RemoveUselessReturnTagRector::class, // keep doc blocks 134 ExplicitReturnNullRector::class, // we sometimes return void or string intentionally 135 UseIdenticalOverEqualWithSameTypeRector::class, // probably a good idea, maybe later 136 ReduceAlwaysFalseIfOrRector::class, // see rectorphp/rector#8916 137 NewlineBetweenClassLikeStmtsRector::class, // looks ugly 138 NullToStrictStringFuncCallArgRector::class, // might hide warnings we want to see explicitly 139 ClassPropertyAssignToConstructorPromotionRector::class, // not sure I like the syntax, maybe later 140 ReplaceBlockToItsStmtsRector::class, // blocks sometimes help readability 141 Utf8DecodeEncodeToMbConvertEncodingRector::class, // we probably want our own mapping to the UTF8/* functions 142 RemoveDeadIfBlockRector::class, // creates harder to read statements 143 144 // we're not ready for full type safety. though this rule is probably safe I am not comfortable to add it yet 145 // https://getrector.com/blog/introducing-safe-and-progressive-strict-type-adoption-rule 146 SafeDeclareStrictTypesRector::class, 147 ]); 148 149 $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [ 150 // see inc/deprecated.php 151 'RemoteAccessDeniedException' => 'dokuwiki\Remote\AccessDeniedException', 152 'RemoteException' => 'dokuwiki\Remote\RemoteException', 153 'setting' => 'dokuwiki\plugin\config\core\Setting\Setting', 154 'setting_authtype' => 'dokuwiki\plugin\config\core\Setting\SettingAuthtype', 155 'setting_string' => 'dokuwiki\plugin\config\core\Setting\SettingString', 156 'PageChangelog' => 'dokuwiki\ChangeLog\PageChangeLog', 157 'MediaChangelog' => 'dokuwiki\ChangeLog\MediaChangeLog', 158 'Input' => 'dokuwiki\Input\Input', 159 'PostInput' => 'dokuwiki\Input\Post', 160 'GetInput' => 'dokuwiki\Input\Get', 161 'ServerInput' => 'dokuwiki\Input\Server', 162 'PassHash' => 'dokuwiki\PassHash', 163 'HTTPClientException' => 'dokuwiki\HTTP\HTTPClientException', 164 'HTTPClient' => 'dokuwiki\HTTP\HTTPClient', 165 'DokuHTTPClient' => 'dokuwiki\HTTP\DokuHTTPClient', 166 'Doku_Plugin_Controller' => 'dokuwiki\Extension\PluginController', 167 'Doku_Indexer' => 'dokuwiki\Search\Indexer', 168 'IXR_Client' => 'dokuwiki\Remote\IXR\Client', 169 'IXR_ClientMulticall' => 'IXR\Client\ClientMulticall', 170 'IXR_Server' => 'IXR\Server\Server', 171 'IXR_IntrospectionServer' => 'IXR\Server\IntrospectionServer', 172 'IXR_Request' => 'IXR\Request\Request', 173 'IXR_Message' => 'R\Message\Message', 174 'IXR_Error' => 'XR\Message\Error', 175 'IXR_Date' => 'IXR\DataType\Date', 176 'IXR_Base64' => 'IXR\DataType\Base64', 177 'IXR_Value' => 'IXR\DataType\Value', 178 179 // see inc/legacy.php 180 'Doku_Event_Handler' => 'dokuwiki\Extension\EventHandler', 181 'Doku_Event' => 'dokuwiki\Extension\Event', 182 'DokuWiki_Action_Plugin' => 'dokuwiki\Extension\ActionPlugin', 183 'DokuWiki_Admin_Plugin' => 'dokuwiki\Extension\AdminPlugin', 184 'DokuWiki_Auth_Plugin' => 'dokuwiki\Extension\AuthPlugin', 185 'DokuWiki_CLI_Plugin' => 'dokuwiki\Extension\CLIPlugin', 186 'DokuWiki_Plugin' => 'dokuwiki\Extension\Plugin', 187 'DokuWiki_Remote_Plugin' => 'dokuwiki\Extension\RemotePlugin', 188 'DokuWiki_Syntax_Plugin' => 'dokuwiki\Extension\SyntaxPlugin', 189 ]); 190 191 $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [ 192 // see inc/deprecated.php 193 'Doku_Lexer_Escape' => 'dokuwiki\Parsing\Lexer\Lexer::escape', 194 195 // see inc/utf8.php 196 'utf8_isASCII' => 'dokuwiki\Utf8\Clean::isASCII', 197 'utf8_strip' => 'dokuwiki\Utf8\Clean::strip', 198 'utf8_check' => 'dokuwiki\Utf8\Clean::isUtf8', 199 'utf8_basename' => 'dokuwiki\Utf8\PhpString::basename', 200 'utf8_strlen' => 'dokuwiki\Utf8\PhpString::strlen', 201 'utf8_substr' => 'dokuwiki\Utf8\PhpString::substr', 202 'utf8_substr_replace' => 'dokuwiki\Utf8\PhpString::substr_replace', 203 'utf8_ltrim' => 'dokuwiki\Utf8\PhpString::ltrim', 204 'utf8_rtrim' => 'dokuwiki\Utf8\PhpString::rtrim', 205 'utf8_trim' => 'dokuwiki\Utf8\PhpString::trim', 206 'utf8_strtolower' => 'dokuwiki\Utf8\PhpString::strtolower', 207 'utf8_strtoupper' => 'dokuwiki\Utf8\PhpString::strtoupper', 208 'utf8_ucfirst' => 'dokuwiki\Utf8\PhpString::ucfirst', 209 'utf8_ucwords' => 'dokuwiki\Utf8\PhpString::ucwords', 210 'utf8_deaccent' => 'dokuwiki\Utf8\Clean::deaccent', 211 'utf8_romanize' => 'dokuwiki\Utf8\Clean::romanize', 212 'utf8_stripspecials' => 'dokuwiki\Utf8\Clean::stripspecials', 213 'utf8_strpos' => 'dokuwiki\Utf8\PhpString::strpos', 214 'utf8_tohtml' => 'dokuwiki\Utf8\Conversion::toHtml', 215 'utf8_unhtml' => 'dokuwiki\Utf8\Conversion::fromHtml', 216 'utf8_to_unicode' => 'dokuwiki\Utf8\Conversion::fromUtf8', 217 'unicode_to_utf8' => 'dokuwiki\Utf8\Conversion::toUtf8', 218 'utf8_to_utf16be' => 'dokuwiki\Utf8\Conversion::toUtf16be', 219 'utf16be_to_utf8' => 'dokuwiki\Utf8\Conversion::fromUtf16be', 220 'utf8_bad_replace' => 'dokuwiki\Utf8\Clean::replaceBadBytes', 221 'utf8_correctIdx' => 'dokuwiki\Utf8\Clean::correctIdx', 222 ]); 223 224 $rectorConfig->rule(DokuWikiPtlnRector::class); 225 $rectorConfig->rule(DokuWikiRenamePrintToEcho::class); 226}; 227