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 --- modules-available/runmode/inc/runmode.inc.php | 57 +++++++++++++-------------- 1 file changed, 27 insertions(+), 30 deletions(-) (limited to 'modules-available/runmode/inc/runmode.inc.php') 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]; } -- cgit v1.2.3-55-g7522