From 1490c177a0ca4ece26d483758a02b2f93a1441e0 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 29 Aug 2023 14:15:12 +0200 Subject: [PATCH] Apply rector fixes to inc/Extension and inc/Debug --- _test/rector.php | 8 +++++ inc/Debug/DebugHelper.php | 10 +++--- inc/Debug/PropertyDeprecationHelper.php | 4 +-- inc/Extension/AdminPlugin.php | 2 +- inc/Extension/AuthPlugin.php | 57 ++++++++++++++++++--------------- inc/Extension/CLIPlugin.php | 3 +- inc/Extension/Event.php | 4 +-- inc/Extension/EventHandler.php | 8 ++--- inc/Extension/PluginController.php | 31 ++++++++---------- inc/Extension/PluginTrait.php | 24 +++++++------- inc/Extension/RemotePlugin.php | 14 ++++---- inc/Extension/SyntaxPlugin.php | 7 ++-- 12 files changed, 92 insertions(+), 80 deletions(-) diff --git a/_test/rector.php b/_test/rector.php index 9a82a256f..85a9220ba 100644 --- a/_test/rector.php +++ b/_test/rector.php @@ -9,6 +9,7 @@ use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; use Rector\CodeQuality\Rector\If_\CombineIfRector; use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector; use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector; +use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector; use Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector; use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector; use Rector\CodingStyle\Rector\Closure\StaticClosureRector; @@ -26,11 +27,17 @@ use Rector\Set\ValueObject\SetList; use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; return static function (RectorConfig $rectorConfig): void { + define('DOKU_INC', __DIR__ . '/../'); + $rectorConfig->paths([ __DIR__ . '/../inc', __DIR__ . '/../lib', ]); + $rectorConfig->bootstrapFiles([ + __DIR__ . '/../inc/load.php', + ]); + $rectorConfig->importNames(); $rectorConfig->importShortClasses(false); $rectorConfig->cacheClass(FileCacheStorage::class); @@ -79,5 +86,6 @@ return static function (RectorConfig $rectorConfig): void { ForRepeatedCountToOwnVariableRector::class, // adds unwanted is_countable checks? RemoveParentCallWithoutParentRector::class, WrapEncapsedVariableInCurlyBracesRector::class, + SimplifyIfReturnBoolRector::class, ]); }; diff --git a/inc/Debug/DebugHelper.php b/inc/Debug/DebugHelper.php index f78b88193..c645e576a 100644 --- a/inc/Debug/DebugHelper.php +++ b/inc/Debug/DebugHelper.php @@ -24,7 +24,7 @@ class DebugHelper global $EVENT_HANDLER; if ( !Logger::getInstance(Logger::LOG_DEPRECATED)->isLogging() && - ($EVENT_HANDLER === null || !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) + (!$EVENT_HANDLER instanceof EventHandler || !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) ) { // avoid any work if no one cares return false; @@ -45,15 +45,15 @@ class DebugHelper if (!self::isEnabled()) return; $backtrace = debug_backtrace(); - for ($i = 0; $i < $callerOffset; $i += 1) { + for ($i = 0; $i < $callerOffset; ++$i) { if(count($backtrace) > 1) array_shift($backtrace); } - list($self, $call) = $backtrace; + [$self, $call] = $backtrace; if (!$thing) { $thing = trim( - (!empty($self['class']) ? ($self['class'] . '::') : '') . + (empty($self['class']) ? ('') : $self['class'] . '::') . $self['function'] . '()', ':'); } @@ -62,7 +62,7 @@ class DebugHelper $alternative, $thing, trim( - (!empty($call['class']) ? ($call['class'] . '::') : '') . + (empty($call['class']) ? ('') : $call['class'] . '::') . $call['function'] . '()', ':'), $self['file'] ?? $call['file'] ?? '', $self['line'] ?? $call['line'] ?? 0 diff --git a/inc/Debug/PropertyDeprecationHelper.php b/inc/Debug/PropertyDeprecationHelper.php index 6289d5ba8..aeca78d86 100644 --- a/inc/Debug/PropertyDeprecationHelper.php +++ b/inc/Debug/PropertyDeprecationHelper.php @@ -108,7 +108,7 @@ trait PropertyDeprecationHelper // The class name is not necessarily correct here but getting the correct class // name would be expensive, this will work most of the time and getting it // wrong is not a big deal. - return __CLASS__; + return self::class; } // property_exists() returns false when the property does exist but is private (and not // defined by the current class, for some value of "current" that differs slightly @@ -124,7 +124,7 @@ trait PropertyDeprecationHelper $classname = substr($obfuscatedProp, 1, -strlen($obfuscatedPropTail)); if ($classname === '*') { // sanity; this shouldn't be possible as protected properties were handled earlier - $classname = __CLASS__; + $classname = self::class; } return $classname; } diff --git a/inc/Extension/AdminPlugin.php b/inc/Extension/AdminPlugin.php index 7900a1ec4..86ed56c62 100644 --- a/inc/Extension/AdminPlugin.php +++ b/inc/Extension/AdminPlugin.php @@ -116,7 +116,7 @@ abstract class AdminPlugin extends Plugin */ public function getTOC() { - return array(); + return []; } } diff --git a/inc/Extension/AuthPlugin.php b/inc/Extension/AuthPlugin.php index 4b75fba95..2d61dfd8e 100644 --- a/inc/Extension/AuthPlugin.php +++ b/inc/Extension/AuthPlugin.php @@ -20,20 +20,31 @@ abstract class AuthPlugin extends Plugin * do. The things a backend can do need to be set to true * in the constructor. */ - protected $cando = array( - 'addUser' => false, // can Users be created? - 'delUser' => false, // can Users be deleted? - 'modLogin' => false, // can login names be changed? - 'modPass' => false, // can passwords be changed? - 'modName' => false, // can real names be changed? - 'modMail' => false, // can emails be changed? - 'modGroups' => false, // can groups be changed? - 'getUsers' => false, // can a (filtered) list of users be retrieved? - 'getUserCount' => false, // can the number of users be retrieved? - 'getGroups' => false, // can a list of available groups be retrieved? - 'external' => false, // does the module do external auth checking? - 'logout' => true, // can the user logout again? (eg. not possible with HTTP auth) - ); + protected $cando = [ + 'addUser' => false, + // can Users be created? + 'delUser' => false, + // can Users be deleted? + 'modLogin' => false, + // can login names be changed? + 'modPass' => false, + // can passwords be changed? + 'modName' => false, + // can real names be changed? + 'modMail' => false, + // can emails be changed? + 'modGroups' => false, + // can groups be changed? + 'getUsers' => false, + // can a (filtered) list of users be retrieved? + 'getUserCount' => false, + // can the number of users be retrieved? + 'getGroups' => false, + // can a list of available groups be retrieved? + 'external' => false, + // does the module do external auth checking? + 'logout' => true, + ]; /** * Constructor. @@ -90,7 +101,6 @@ abstract class AuthPlugin extends Plugin return ($this->cando['modPass'] || $this->cando['modName'] || $this->cando['modMail']); - break; case 'UserMod': // can at least anything be changed? return ($this->cando['modPass'] || @@ -99,7 +109,6 @@ abstract class AuthPlugin extends Plugin $this->cando['modLogin'] || $this->cando['modGroups'] || $this->cando['modMail']); - break; default: // print a helping message for developers if (!isset($this->cando[$cap])) { @@ -124,20 +133,16 @@ abstract class AuthPlugin extends Plugin */ public function triggerUserMod($type, $params) { - $validTypes = array( - 'create' => 'createUser', - 'modify' => 'modifyUser', - 'delete' => 'deleteUsers', - ); + $validTypes = ['create' => 'createUser', 'modify' => 'modifyUser', 'delete' => 'deleteUsers']; if (empty($validTypes[$type])) { return false; } $result = false; - $eventdata = array('type' => $type, 'params' => $params, 'modification_result' => null); + $eventdata = ['type' => $type, 'params' => $params, 'modification_result' => null]; $evt = new Event('AUTH_USER_CHANGE', $eventdata); if ($evt->advise_before(true)) { - $result = call_user_func_array(array($this, $validTypes[$type]), $evt->data['params']); + $result = call_user_func_array([$this, $validTypes[$type]], $evt->data['params']); $evt->data['modification_result'] = $result; } $evt->advise_after(); @@ -323,7 +328,7 @@ abstract class AuthPlugin extends Plugin * @param array $filter array of field/pattern pairs, empty array for no filter * @return int */ - public function getUserCount($filter = array()) + public function getUserCount($filter = []) { msg("authorisation method does not provide user counts", -1); return 0; @@ -343,7 +348,7 @@ abstract class AuthPlugin extends Plugin public function retrieveUsers($start = 0, $limit = 0, $filter = null) { msg("authorisation method does not support mass retrieval of user data", -1); - return array(); + return []; } /** @@ -374,7 +379,7 @@ abstract class AuthPlugin extends Plugin public function retrieveGroups($start = 0, $limit = 0) { msg("authorisation method does not support group list retrieval", -1); - return array(); + return []; } /** diff --git a/inc/Extension/CLIPlugin.php b/inc/Extension/CLIPlugin.php index 8637ccf8c..22d935754 100644 --- a/inc/Extension/CLIPlugin.php +++ b/inc/Extension/CLIPlugin.php @@ -2,12 +2,13 @@ namespace dokuwiki\Extension; +use splitbrain\phpcli\CLI; /** * CLI plugin prototype * * Provides DokuWiki plugin functionality on top of php-cli */ -abstract class CLIPlugin extends \splitbrain\phpcli\CLI implements PluginInterface +abstract class CLIPlugin extends CLI implements PluginInterface { use PluginTrait; } diff --git a/inc/Extension/Event.php b/inc/Extension/Event.php index cc38f0f36..64d768442 100644 --- a/inc/Extension/Event.php +++ b/inc/Extension/Event.php @@ -13,13 +13,13 @@ class Event /** @var string READONLY event name, objects must register against this name to see the event */ public $name = ''; /** @var mixed|null READWRITE data relevant to the event, no standardised format, refer to event docs */ - public $data = null; + public $data; /** * @var mixed|null READWRITE the results of the event action, only relevant in "_AFTER" advise * event handlers may modify this if they are preventing the default action * to provide the after event handlers with event results */ - public $result = null; + public $result; /** @var bool READONLY if true, event handlers can prevent the events default action */ public $canPreventDefault = true; diff --git a/inc/Extension/EventHandler.php b/inc/Extension/EventHandler.php index 33ae5e123..8d0a1a361 100644 --- a/inc/Extension/EventHandler.php +++ b/inc/Extension/EventHandler.php @@ -12,7 +12,7 @@ class EventHandler // public properties: none // private properties - protected $hooks = array(); // array of events and their registered handlers + protected $hooks = []; // array of events and their registered handlers /** * event_handler @@ -31,7 +31,7 @@ class EventHandler foreach ($pluginlist as $plugin_name) { $plugin = plugin_load('action', $plugin_name); - if ($plugin !== null) $plugin->register($this); + if ($plugin instanceof PluginInterface) $plugin->register($this); } } @@ -51,7 +51,7 @@ class EventHandler { $seq = (int)$seq; $doSort = !isset($this->hooks[$event . '_' . $advise][$seq]); - $this->hooks[$event . '_' . $advise][$seq][] = array($obj, $method, $param); + $this->hooks[$event . '_' . $advise][$seq][] = [$obj, $method, $param]; if ($doSort) { ksort($this->hooks[$event . '_' . $advise]); @@ -72,7 +72,7 @@ class EventHandler if (!empty($this->hooks[$evt_name])) { foreach ($this->hooks[$evt_name] as $sequenced_hooks) { foreach ($sequenced_hooks as $hook) { - list($obj, $method, $param) = $hook; + [$obj, $method, $param] = $hook; if ($obj === null) { $method($event, $param); diff --git a/inc/Extension/PluginController.php b/inc/Extension/PluginController.php index d53ea853e..4db61b8fc 100644 --- a/inc/Extension/PluginController.php +++ b/inc/Extension/PluginController.php @@ -83,7 +83,7 @@ class PluginController //we keep all loaded plugins available in global scope for reuse global $DOKU_PLUGINS; - list($plugin, /* $component */) = $this->splitName($name); + [$plugin, ] = $this->splitName($name); // check if disabled if (!$disabled && !$this->isEnabled($plugin)) { @@ -120,8 +120,7 @@ class PluginController } elseif (preg_match('/^' . DOKU_PLUGIN_NAME_REGEX . '$/', $plugin) !== 1) { msg( sprintf( - "Plugin name '%s' is not a valid plugin name, only the characters a-z ". - "and 0-9 are allowed. " . + 'Plugin name \'%s\' is not a valid plugin name, only the characters a-z and 0-9 are allowed. ' . 'Maybe the plugin has been installed in the wrong directory?', hsc($plugin) ), -1 ); @@ -204,7 +203,7 @@ class PluginController protected function populateMasterList() { if ($dh = @opendir(DOKU_PLUGIN)) { - $all_plugins = array(); + $all_plugins = []; while (false !== ($plugin = readdir($dh))) { if ($plugin[0] === '.') continue; // skip hidden entries if (is_file(DOKU_PLUGIN . $plugin)) continue; // skip files, we're only interested in directories @@ -234,7 +233,7 @@ class PluginController */ protected function checkRequire($files) { - $plugins = array(); + $plugins = []; foreach ($files as $file) { if (file_exists($file)) { include_once($file); @@ -297,7 +296,7 @@ class PluginController //gives us the ones we need to check and save $diffed_ones = array_diff_key($local_default, $this->pluginCascade['default']); //The ones which we are sure of (list of 0s not in default) - $sure_plugins = array_filter($diffed_ones, array($this, 'negate')); + $sure_plugins = array_filter($diffed_ones, [$this, 'negate']); //the ones in need of diff $conflicts = array_diff_key($local_default, $diffed_ones); //The final list @@ -311,20 +310,18 @@ class PluginController protected function loadConfig() { global $config_cascade; - foreach (array('default', 'protected') as $type) { + foreach (['default', 'protected'] as $type) { if (array_key_exists($type, $config_cascade['plugins'])) { $this->pluginCascade[$type] = $this->checkRequire($config_cascade['plugins'][$type]); } } $local = $config_cascade['plugins']['local']; $this->lastLocalConfigFile = array_pop($local); - $this->pluginCascade['local'] = $this->checkRequire(array($this->lastLocalConfigFile)); - if (is_array($local)) { - $this->pluginCascade['default'] = array_merge( - $this->pluginCascade['default'], - $this->checkRequire($local) - ); - } + $this->pluginCascade['local'] = $this->checkRequire([$this->lastLocalConfigFile]); + $this->pluginCascade['default'] = array_merge( + $this->pluginCascade['default'], + $this->checkRequire($local) + ); $this->masterList = array_merge( $this->pluginCascade['default'], $this->pluginCascade['local'], @@ -344,8 +341,8 @@ class PluginController { $master_list = $enabled ? array_keys(array_filter($this->masterList)) - : array_keys(array_filter($this->masterList, array($this, 'negate'))); - $plugins = array(); + : array_keys(array_filter($this->masterList, [$this, 'negate'])); + $plugins = []; foreach ($master_list as $plugin) { @@ -386,7 +383,7 @@ class PluginController return sexplode('_', $name, 2, ''); } - return array($name, ''); + return [$name, '']; } /** diff --git a/inc/Extension/PluginTrait.php b/inc/Extension/PluginTrait.php index 7f399df1a..4f43c5ff2 100644 --- a/inc/Extension/PluginTrait.php +++ b/inc/Extension/PluginTrait.php @@ -9,9 +9,9 @@ trait PluginTrait { protected $localised = false; // set to true by setupLocale() after loading language dependent strings - protected $lang = array(); // array to hold language dependent strings, best accessed via ->getLang() + protected $lang = []; // array to hold language dependent strings, best accessed via ->getLang() protected $configloaded = false; // set to true by loadConfig() after loading plugin configuration variables - protected $conf = array(); // array to hold plugin settings, best accessed via ->getConf() + protected $conf = []; // array to hold plugin settings, best accessed via ->getConf() /** * @see PluginInterface::getInfo() @@ -27,10 +27,10 @@ trait PluginTrait 'Verify you\'re running the latest version of the plugin. If the problem persists, send a ' . 'bug report to the author of the ' . $parts[2] . ' plugin.', -1 ); - return array( + return [ 'date' => '0000-00-00', - 'name' => $parts[2] . ' plugin', - ); + 'name' => $parts[2] . ' plugin' + ]; } /** @@ -58,7 +58,7 @@ trait PluginTrait */ public function getPluginType() { - list($t) = explode('_', get_class($this), 2); + [$t] = explode('_', get_class($this), 2); return $t; } @@ -67,7 +67,7 @@ trait PluginTrait */ public function getPluginName() { - list(/* $t */, /* $p */, $n) = sexplode('_', get_class($this), 4, ''); + [, , $n] = sexplode('_', get_class($this), 4, ''); return $n; } @@ -76,7 +76,7 @@ trait PluginTrait */ public function getPluginComponent() { - list(/* $t */, /* $p */, /* $n */, $c) = sexplode('_', get_class($this), 4, ''); + [, , , $c] = sexplode('_', get_class($this), 4, ''); return $c; } @@ -90,7 +90,7 @@ trait PluginTrait { if (!$this->localised) $this->setupLocale(); - return (isset($this->lang[$id]) ? $this->lang[$id] : ''); + return ($this->lang[$id] ?? ''); } /** @@ -129,7 +129,7 @@ trait PluginTrait global $conf, $config_cascade; // definitely don't invoke "global $lang" $path = DOKU_PLUGIN . $this->getPluginName() . '/lang/'; - $lang = array(); + $lang = []; // don't include once, in case several plugin components require the same language file @include($path . 'en/lang.php'); @@ -201,7 +201,7 @@ trait PluginTrait { $path = DOKU_PLUGIN . $this->getPluginName() . '/conf/'; - $conf = array(); + $conf = []; if (file_exists($path . 'default.php')) { include($path . 'default.php'); @@ -221,7 +221,7 @@ trait PluginTrait if (!$email) return $name; $email = obfuscate($email); if (!$name) $name = $email; - $class = "class='" . ($class ? $class : 'mail') . "'"; + $class = "class='" . ($class ?: 'mail') . "'"; return "$name"; } diff --git a/inc/Extension/RemotePlugin.php b/inc/Extension/RemotePlugin.php index 33bca980a..fd6711044 100644 --- a/inc/Extension/RemotePlugin.php +++ b/inc/Extension/RemotePlugin.php @@ -14,7 +14,7 @@ use ReflectionMethod; abstract class RemotePlugin extends Plugin { - private $api; + private Api $api; /** * Constructor @@ -35,7 +35,7 @@ abstract class RemotePlugin extends Plugin */ public function _getMethods() { - $result = array(); + $result = []; $reflection = new \ReflectionClass($this); foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { @@ -52,17 +52,17 @@ abstract class RemotePlugin extends Plugin // strip asterisks $doc = $method->getDocComment(); $doc = preg_replace( - array('/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'), - array('', '', '', ''), + ['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'], + ['', '', '', ''], $doc ); // prepare data - $data = array(); + $data = []; $data['name'] = $method_name; $data['public'] = 0; $data['doc'] = $doc; - $data['args'] = array(); + $data['args'] = []; // get parameter type from doc block type hint foreach ($method->getParameters() as $parameter) { @@ -104,7 +104,7 @@ abstract class RemotePlugin extends Plugin if ($t === 'boolean') { return 'bool'; } - if (in_array($t, array('array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'))) { + if (in_array($t, ['array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'])) { return $t; } } diff --git a/inc/Extension/SyntaxPlugin.php b/inc/Extension/SyntaxPlugin.php index ea8f51b4d..d590b04e4 100644 --- a/inc/Extension/SyntaxPlugin.php +++ b/inc/Extension/SyntaxPlugin.php @@ -2,6 +2,7 @@ namespace dokuwiki\Extension; +use dokuwiki\Parsing\ParserMode\Plugin; use Doku_Handler; use Doku_Renderer; @@ -14,7 +15,7 @@ use Doku_Renderer; * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Andreas Gohr */ -abstract class SyntaxPlugin extends \dokuwiki\Parsing\ParserMode\Plugin +abstract class SyntaxPlugin extends Plugin { use PluginTrait; @@ -40,7 +41,7 @@ abstract class SyntaxPlugin extends \dokuwiki\Parsing\ParserMode\Plugin */ public function getAllowedTypes() { - return array(); + return []; } /** @@ -120,7 +121,7 @@ abstract class SyntaxPlugin extends \dokuwiki\Parsing\ParserMode\Plugin $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]); } - $idx = array_search(substr(get_class($this), 7), (array)$this->allowedModes); + $idx = array_search(substr(get_class($this), 7), (array)$this->allowedModes, true); if ($idx !== false) { unset($this->allowedModes[$idx]); } -- 2.11.4.GIT