From 06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Nov 2023 14:47:55 +0100 Subject: Add function param/return types, fix a lot more phpstorm complaints --- .../runmode/baseconfig/getconfig.inc.php | 13 +++-- modules-available/runmode/inc/runmode.inc.php | 57 ++++++++++------------ modules-available/runmode/page.inc.php | 28 +++++------ 3 files changed, 47 insertions(+), 51 deletions(-) (limited to 'modules-available/runmode') diff --git a/modules-available/runmode/baseconfig/getconfig.inc.php b/modules-available/runmode/baseconfig/getconfig.inc.php index 9c36cc75..a5de1053 100644 --- a/modules-available/runmode/baseconfig/getconfig.inc.php +++ b/modules-available/runmode/baseconfig/getconfig.inc.php @@ -1,20 +1,23 @@ $machineUuid)); + array('uuid' => $uuid)); if ($res === false) return; $config = RunMode::getModuleConfig($res['module']); - if ($config === false) + if ($config === null) return; if (!Module::isAvailable($res['module'])) return; // Not really possible because getModuleConfig would have failed but we should make sure if ($config->configHook !== false) { - call_user_func($config->configHook, $machineUuid, $res['modeid'], $res['modedata']); + call_user_func($config->configHook, $uuid, $res['modeid'], $res['modedata']); } if ($config->systemdDefaultTarget !== false) { ConfigHolder::add('SLX_SYSTEMD_TARGET', $config->systemdDefaultTarget, 10000); } ConfigHolder::add('SLX_RUNMODE_MODULE', $res['module']); -})($uuid); +} diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index 65b444e7..2d676cae 100644 --- a/modules-available/runmode/inc/runmode.inc.php +++ b/modules-available/runmode/inc/runmode.inc.php @@ -12,36 +12,36 @@ class RunMode * Get runmode config for a specific module * * @param string $module name of module - * @return \RunModeModuleConfig|false config, false if moudles doesn't support run modes + * @return ?RunModeModuleConfig config, null if module doesn't support run modes */ - public static function getModuleConfig($module) + public static function getModuleConfig(string $module): ?RunModeModuleConfig { if (isset(self::$moduleConfigs[$module])) return self::$moduleConfigs[$module]; if (Module::get($module) === false) - return false; + return null; $file = 'modules/' . $module . '/hooks/runmode/config.json'; if (!file_exists($file)) - return false; + return null; return (self::$moduleConfigs[$module] = new RunModeModuleConfig($file)); } /** - * @param string $machineuuid * @param string|\Module $moduleId * @param string|null $modeId an ID specific to the module to further specify the run mode, NULL to delete the run mode entry * @param string|null $modeData optional, additional data for the run mode * @param bool|null $isClient whether to count the machine as a client (in statistics etc.) NULL for looking at module's general runmode config * @return bool whether it was set/deleted */ - public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData = null, $isClient = null) + public static function setRunMode(string $machineuuid, $moduleId, ?string $modeId, + ?string $modeData = null, ?bool $isClient = null): bool { if (is_object($moduleId)) { $moduleId = $moduleId->getIdentifier(); } // - Check if machine exists $machine = Statistics::getMachine($machineuuid, Machine::NO_DATA); - if ($machine === false) + if ($machine === null) return false; // - Delete entry if mode is null if ($modeId === null) { @@ -72,25 +72,21 @@ class RunMode * Change the isClient flag for existing client. * @param string $machineUuid existing machine with some runmode * @param string $moduleId module that assigned the current runmode of that client - * @param bool $isClient + * @param bool $isClient should this machine be considered a normal client? */ - public static function updateClientFlag($machineUuid, $moduleId, $isClient) + public static function updateClientFlag(string $machineUuid, string $moduleId, bool $isClient): void { Database::exec('UPDATE runmode SET isclient = :isclient WHERE machineuuid = :uuid AND module = :module', ['uuid' => $machineUuid, 'module' => $moduleId, 'isclient' => ($isClient ? 1 : 0)]); } /** - * @param string $machineuuid * @param int $returnData bitfield of data to return * @return false|array {'machineuuid', 'isclient', 'module', 'modeid', 'modedata', * ('hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'), ('moduleName', 'modeName')} */ - public static function getRunMode($machineuuid, $returnData = self::DATA_MACHINE_DATA) + public static function getRunMode(string $machineuuid, int $returnData = self::DATA_MACHINE_DATA) { - if ($returnData === true) { - $returnData = self::DATA_MACHINE_DATA | self::DATA_DETAILED; - } if ($returnData & self::DATA_MACHINE_DATA) { if ($returnData & self::DATA_DETAILED) { $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen'; @@ -127,11 +123,11 @@ class RunMode } /** - * @param string|\Module $module - * @param bool true = wrap in array where key is modeid - * @return array key=machineuuid, value={'machineuuid', 'modeid', 'modedata'} + * @param string|Module $module + * @param bool $groupByModeId true = wrap in array where key is modeid + * @return array - format depending on $groupByModeId */ - public static function getForModule($module, $groupByModeId = false) + public static function getForModule($module, bool $groupByModeId = false): array { if (is_object($module)) { $module = $module->getIdentifier(); @@ -153,14 +149,14 @@ class RunMode } /** - * @param string|\Module $module - * @param string $modeId + * @param string|Module $module Module the mode belongs to + * @param string $modeId module-specific runmode identifier * @param bool $detailed whether to return meta data about machine, not just machineuuid * @param bool $assoc use machineuuid as array key * @return array , value={'machineuuid', 'modedata', * <'hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'>} */ - public static function getForMode($module, $modeId, $detailed = false, $assoc = false) + public static function getForMode($module, string $modeId, bool $detailed = false, bool $assoc = false): array { if (is_object($module)) { $module = $module->getIdentifier(); @@ -192,11 +188,12 @@ class RunMode /** * Return assoc array of all configured clients. + * * @param bool $withData also return data field? - * @param bool $isClient true = return clients only, false = return non-clients only, null = return both + * @param bool|null $isClient true = return clients only, false = return non-clients only, null = return both * @return array all the entries from the table */ - public static function getAllClients($withData = false, $isClient = null) + public static function getAllClients(bool $withData = false, bool $isClient = null): array { $xtra = ''; if ($withData) { @@ -218,11 +215,11 @@ class RunMode * that method is passed through. getModeName by contract should return false if * the module doesn't think the given modeId exists. * - * @param string|\Module $module - * @param string $modeId - * @return string|bool mode name if known, modeId as fallback, or false if mode is not known by module + * @param string|Module $module module the runmode belongs to + * @param string $modeId module-specific runmode identifier + * @return string|false mode name if known, modeId as fallback, or false if mode is not known by module */ - public static function getModeName($module, $modeId) + public static function getModeName($module, string $modeId) { if (is_object($module)) { $module = $module->getIdentifier(); @@ -236,10 +233,10 @@ class RunMode /** * Delete given runmode. * - * @param string|\Module $module Module runmode belongs to + * @param string|Module $module Module runmode belongs to * @param string $modeId run mode id */ - public static function deleteMode($module, $modeId) + public static function deleteMode($module, string $modeId): void { if (is_object($module)) { $module = $module->getIdentifier(); @@ -317,7 +314,7 @@ class RunModeModuleConfig { if (!isset($data[$key])) return; - if (is_string($type) && gettype($data[$key]) !== $type) + if (gettype($data[$key]) !== $type) return; $this->{$key} = $data[$key]; } diff --git a/modules-available/runmode/page.inc.php b/modules-available/runmode/page.inc.php index cceefae8..5654456a 100644 --- a/modules-available/runmode/page.inc.php +++ b/modules-available/runmode/page.inc.php @@ -30,7 +30,7 @@ class Page_RunMode extends Page $module = Request::post('module', false, 'string'); $modeId = Request::post('modeid', false, 'string'); $modConfig = RunMode::getModuleConfig($module); - if ($modConfig === false) { + if ($modConfig === null) { Message::addError('runmode.module-hasnt-runmode', $module); return; } @@ -70,7 +70,7 @@ class Page_RunMode extends Page if ($oldMachineMode !== false) { $machineLocation = $oldMachineMode['locationid']; $oldModule = RunMode::getModuleConfig($oldMachineMode['module']); - if ($oldModule !== false) { + if ($oldModule !== null) { if ($oldMachineMode['module'] !== $module || $oldMachineMode['modeid'] !== $modeId) { if (!$oldModule->allowGenericEditor || $oldModule->deleteUrlSnippet !== false) { Message::addError('runmode.machine-still-assigned', $machine, $oldMachineMode['module']); @@ -87,7 +87,7 @@ class Page_RunMode extends Page } else { // Not existing, no old mode - query machine to get location, so we can do a perm-check for new loc $m = Statistics::getMachine($machine, Machine::NO_DATA); - if ($m !== false) { + if ($m !== null) { $machineLocation = $m->locationid; } } @@ -134,7 +134,7 @@ class Page_RunMode extends Page return; } $modConfig = RunMode::getModuleConfig($mode['module']); - if ($modConfig === false) { + if ($modConfig === null) { Message::addError('module-hasnt-runmode', $mode['moduleName']); return; } @@ -172,7 +172,7 @@ class Page_RunMode extends Page } $module->activate(1, false); $config = RunMode::getModuleConfig($moduleId); - if ($config === false) { + if ($config === null) { Message::addError('module-hasnt-runmode', $moduleId); Util::redirect('?do=runmode'); } @@ -187,7 +187,6 @@ class Page_RunMode extends Page if (!$config->userHasPermission(null) && !User::hasPermission('list-all')) { Message::addError('main.no-permission'); Util::redirect('?do=runmode'); - return; } // Show list of machines with assigned mode for this module $this->renderClientList($moduleId); @@ -212,7 +211,10 @@ class Page_RunMode extends Page if (!isset($modules[$row['module']])) { if (!Module::isAvailable($row['module'])) continue; - $modules[$row['module']] = array('config' => RunMode::getModuleConfig($row['module']), 'list' => array()); + $config = RunMode::getModuleConfig($row['module']); + if ($config === null) + continue; + $modules[$row['module']] = array('config' => $config, 'list' => array()); } if (empty($row['hostname'])) { $row['hostname'] = $row['clientip']; @@ -244,7 +246,7 @@ class Page_RunMode extends Page 'list' => $rows['list'], 'modulename' => $module->getDisplayName(), 'module' => $moduleId, - 'canedit' => $config !== false && $config->allowGenericEditor && $config->deleteUrlSnippet === false, + 'canedit' => $config !== null && $config->allowGenericEditor && $config->deleteUrlSnippet === false, 'deleteUrl' => $config->deleteUrlSnippet, 'disabled' => $disabled, )); @@ -254,12 +256,7 @@ class Page_RunMode extends Page } } - /** - * @param \Module $module - * @param string $modeId - * @param \RunModeModuleConfig $config - */ - private function renderModuleMode($module, $modeId, $config) + private function renderModuleMode(Module $module, string $modeId, RunModeModuleConfig $config) { $moduleId = $module->getIdentifier(); $modeName = RunMode::getModeName($moduleId, $modeId); @@ -283,7 +280,6 @@ class Page_RunMode extends Page } else { Message::addError('main.no-permission'); Util::redirect('?do=runmode'); - return; } $machines = RunMode::getForMode($module, $modeId, true); if ($config->permission !== false) { @@ -317,7 +313,7 @@ class Page_RunMode extends Page $config = RunMode::getModuleConfig(Request::any('module', '', 'string')); $returnObject = ['machines' => []]; - if ($config !== false) { + if ($config !== null) { $params = ['query' => "%$query%"]; if ($config->permission === false) { // Global -- cgit v1.2.3-55-g7522