diff options
| author | Simon Rettberg | 2025-07-24 11:40:48 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2025-07-24 11:40:48 +0200 |
| commit | c253bbb41356f09c274e97cb7144e6a78ab5a088 (patch) | |
| tree | 7e88f44b648999b157be656ca55198893ff82f7f | |
| parent | [minilinux/rebootcontrol/statistics] Refactor to better suit audit logging (diff) | |
| download | slx-admin-c253bbb41356f09c274e97cb7144e6a78ab5a088.tar.gz slx-admin-c253bbb41356f09c274e97cb7144e6a78ab5a088.tar.xz slx-admin-c253bbb41356f09c274e97cb7144e6a78ab5a088.zip | |
[roomplanner] Add type annotations, change falsable -> nullable
| -rw-r--r-- | modules-available/roomplanner/api.inc.php | 6 | ||||
| -rw-r--r-- | modules-available/roomplanner/inc/composedroom.inc.php | 30 | ||||
| -rw-r--r-- | modules-available/roomplanner/inc/pvsgenerator.inc.php | 34 | ||||
| -rw-r--r-- | modules-available/roomplanner/inc/room.inc.php | 17 | ||||
| -rw-r--r-- | modules-available/roomplanner/inc/simpleroom.inc.php | 21 | ||||
| -rw-r--r-- | modules-available/roomplanner/page.inc.php | 27 | ||||
| -rw-r--r-- | modules-available/statistics/pages/list.inc.php | 4 | ||||
| -rw-r--r-- | modules-available/statistics/pages/machine.inc.php | 2 |
8 files changed, 67 insertions, 74 deletions
diff --git a/modules-available/roomplanner/api.inc.php b/modules-available/roomplanner/api.inc.php index c66640a0..ef376814 100644 --- a/modules-available/roomplanner/api.inc.php +++ b/modules-available/roomplanner/api.inc.php @@ -2,12 +2,12 @@ // SVG if (Request::any('show') === 'svg') { - $ret = PvsGenerator::generateSvg(Request::any('locationid', false, 'int'), - Request::any('machineuuid', false, 'string'), + $ret = PvsGenerator::generateSvg(Request::any('locationid', null, 'int'), + Request::any('machineuuid', null, 'string'), Request::any('rotate', 0, 'int'), Request::any('scale', 1, 'float'), Request::any('url', false, 'bool')); - if ($ret === false) { + if ($ret === null) { if (Request::any('fallback', 0, 'int') === 0) { http_response_code(404); exit; diff --git a/modules-available/roomplanner/inc/composedroom.inc.php b/modules-available/roomplanner/inc/composedroom.inc.php index 3ee892db..fff158ba 100644 --- a/modules-available/roomplanner/inc/composedroom.inc.php +++ b/modules-available/roomplanner/inc/composedroom.inc.php @@ -13,7 +13,7 @@ class ComposedRoom extends Room private $list; /** - * @var bool Whether composed room is active, ie. visible in PVS. + * @var bool Whether the composed room is active, i.e., visible in PVS. */ private $enabled; @@ -25,11 +25,11 @@ class ComposedRoom extends Room /** * ComposedRoom constructor. * - * @param array|true $row DB row to instantiate from, or true to read from $_POST + * @param ?array $row DB row to instantiate from, or null to read from $_POST */ - public function __construct($row, $sanitize = true) + public function __construct(?array $row, bool $sanitize = true) { - if ($row === true) { + if ($row === null) { $this->orientation = Request::post('orientation', 'horizontal', 'string'); $this->enabled = (bool)Request::post('enabled', 0, 'int'); $this->controlRoom = Request::post('controlroom', 0, 'int'); @@ -38,7 +38,7 @@ class ComposedRoom extends Room $this->list = array_keys($vals); } else { parent::__construct($row); - if (is_array($row) && isset($row['roomplan'])) { + if (!empty($row['roomplan'])) { // From DB $row = json_decode($row['roomplan'], true); } @@ -58,7 +58,7 @@ class ComposedRoom extends Room /** * Make sure all member vars have the proper type */ - protected function sanitize() + protected function sanitize(): void { $this->orientation = ($this->orientation === 'horizontal' ? 'horizontal' : 'vertical'); settype($this->enabled, 'bool'); @@ -117,7 +117,7 @@ class ComposedRoom extends Room return $sum; } - public function getSize(?int &$width, ?int &$height) + public function getSize(?int &$width, ?int &$height): void { $horz = ($this->orientation == 'horizontal'); foreach ($this->list as $locId) { @@ -127,10 +127,10 @@ class ComposedRoom extends Room } } - public function getIniClientSection(int &$i, int $offX = 0, int $offY = 0) + public function getIniClientSection(int &$i, int $offX = 0, int $offY = 0): ?string { if (!$this->enabled) - return false; + return null; if ($this->orientation == 'horizontal') { $x = 1; $y = 0; @@ -141,7 +141,7 @@ class ComposedRoom extends Room $out = ''; foreach ($this->list as $locId) { $ret = self::$rooms[$locId]->getIniClientSection($i, $offX, $offY); - if ($ret !== false) { + if ($ret !== null) { $out .= $ret; self::$rooms[$locId]->getSize($w, $h); $offX += $w * $x; @@ -149,7 +149,7 @@ class ComposedRoom extends Room } } if (empty($out)) - return false; + return null; return $out; } @@ -179,18 +179,18 @@ class ComposedRoom extends Room return $ret; } - public function getManagerIp() + public function getManagerIp(): ?string { if (isset(self::$rooms[$this->controlRoom])) return self::$rooms[$this->controlRoom]->getManagerIp(); - return false; + return null; } - public function getTutorIp() + public function getTutorIp(): ?string { if (isset(self::$rooms[$this->controlRoom])) return self::$rooms[$this->controlRoom]->getTutorIp(); - return false; + return null; } public function isLeaf(): bool diff --git a/modules-available/roomplanner/inc/pvsgenerator.inc.php b/modules-available/roomplanner/inc/pvsgenerator.inc.php index 3a699a60..7860f893 100644 --- a/modules-available/roomplanner/inc/pvsgenerator.inc.php +++ b/modules-available/roomplanner/inc/pvsgenerator.inc.php @@ -15,7 +15,7 @@ class PvsGenerator if ($room->getManagerIp() === false) // No .ini entry for rooms without manager (do we want this?) continue; $roomBlock = PvsGenerator::generateRoomBlock($room); - if ($roomBlock === false) + if ($roomBlock === null) continue; // Room nonexistent or empty $section = substr(md5($room->locationId() . '-' . $room->locationName()), 0, 10); $roomNames[] = $section; @@ -34,17 +34,17 @@ class PvsGenerator * Generate .ini section for specific room. * * @param Room $room room/location data as fetched from db - * @return string|false .ini section for room, or false if room is empty + * @return ?string .ini section for room, or false if room is empty */ - private static function generateRoomBlock(Room $room) + private static function generateRoomBlock(Room $room): ?string { $room->getSize($sizeX, $sizeY); if ($sizeX === 0 || $sizeY === 0) - return false; + return null; $count = 0; $section = $room->getIniClientSection($count); - if ($section === false) - return false; + if ($section === null) + return null; $cs = SimpleRoom::CLIENT_SIZE; $out = "name=" . $room->locationName() . "\n"; @@ -72,39 +72,41 @@ class PvsGenerator * the given machine in the plan. If only machineUuid is * given, determine locationId from machine. * - * @param int|false $locationId - * @param string|false $highlightUuid * @param int $rotate rotate plan (0-3 for N E S W up, -1 for "auto" if highlightUuid is given) * @param float $scale scaling factor for output * @return string SVG */ - public static function generateSvg($locationId = false, $highlightUuid = false, int $rotate = 0, $scale = 1, $links = false, array $present = null) + public static function generateSvg(?int $locationId = null, ?string $highlightUuid = null, + int $rotate = 0, float $scale = 1, + bool $links = false, array $present = null): ?string { - if ($locationId === false) { + if ($locationId === null) { + if ($highlightUuid === null) + return null; $locationId = Database::queryFirst('SELECT fixedlocationid FROM machine WHERE machineuuid = :uuid AND Length(position) > 5', ['uuid' => $highlightUuid]); // Not found or not placed in room plan -- bail out if ($locationId === false || $locationId['fixedlocationid'] === null) - return false; - $locationId = $locationId['fixedlocationid']; + return null; + $locationId = (int)$locationId['fixedlocationid']; } // Load room $room = Room::get($locationId); if ($room === null) - return false; + return null; $room->getSize($sizeX, $sizeY); if ($sizeX === 0 || $sizeY === 0) - return false; // Empty + return null; // Empty $machines = $room->getShiftedArray() ?? []; $ORIENTATION = ['north' => 2, 'east' => 3, 'south' => 0, 'west' => 1]; - if (is_string($highlightUuid)) { + if ($highlightUuid !== null) { $highlightUuid = strtoupper($highlightUuid); } // Figure out autorotate $auto = ($rotate < 0); - if ($auto && $highlightUuid !== false) { + if ($auto && $highlightUuid !== null) { foreach ($machines as $machine) { if ($machine['machineuuid'] === $highlightUuid) { $rotate = 4 - $ORIENTATION[$machine['rotation']]; // Reverse rotation diff --git a/modules-available/roomplanner/inc/room.inc.php b/modules-available/roomplanner/inc/room.inc.php index c37dc24a..f4a81a90 100644 --- a/modules-available/roomplanner/inc/room.inc.php +++ b/modules-available/roomplanner/inc/room.inc.php @@ -18,7 +18,7 @@ abstract class Room */ private $locationName; - protected static function init() + protected static function init(): void { if (self::$rooms !== null) return; @@ -94,7 +94,7 @@ abstract class Room return null; } - public function __construct($row) + public function __construct(array $row) { $this->locationId = (int)$row['locationid']; $this->locationName = Location::getName($this->locationId); @@ -111,7 +111,7 @@ abstract class Room * @param int|null $width OUT width of room * @param int|null $height OUT height of room */ - abstract public function getSize(?int &$width, ?int &$height); + abstract public function getSize(?int &$width, ?int &$height): void; /** * Get clients in this room in .ini format for PVS. @@ -121,9 +121,8 @@ abstract class Room * @param int $i offset for indexing clients * @param int $offX positional X offset for clients * @param int $offY positional Y offset for clients - * @return string|false */ - abstract public function getIniClientSection(int &$i, int $offX = 0, int $offY = 0); + abstract public function getIniClientSection(int &$i, int $offX = 0, int $offY = 0): ?string; /** * Get clients in this room as array. @@ -135,12 +134,12 @@ abstract class Room /** * @return string|false IP address of manager. */ - abstract public function getManagerIp(); + abstract public function getManagerIp(): ?string; /** - * @return string|false IP address of tutor client. + * @return string|false IP address of tutor's client. */ - abstract public function getTutorIp(); + abstract public function getTutorIp(): ?string; /** * @return bool true if this is a simple/leaf room, false for composed rooms. @@ -155,7 +154,7 @@ abstract class Room /** * Sanitize this room's data. */ - abstract protected function sanitize(); + abstract protected function sanitize(): void; /** * @return string get room's name. diff --git a/modules-available/roomplanner/inc/simpleroom.inc.php b/modules-available/roomplanner/inc/simpleroom.inc.php index b4d3e744..074c5ef2 100644 --- a/modules-available/roomplanner/inc/simpleroom.inc.php +++ b/modules-available/roomplanner/inc/simpleroom.inc.php @@ -9,12 +9,13 @@ class SimpleRoom extends Room private $bb = false; - private $tutorIp = false; + /** @var ?string */ + private $tutorIp = null; /** @var ?string */ - private $managerIp = false; + private $managerIp = null; - public function __construct($row) + public function __construct(array $row) { parent::__construct($row); $locationId = (int)$row['locationid']; @@ -42,17 +43,17 @@ class SimpleRoom extends Room } // Runmode info overrides IP given if (Module::isAvailable('runmode')) { - $pc = RunMode::getForMode('roomplanner', $locationId, true); + $pc = RunMode::getForMode('roomplanner', (string)$locationId, true); if (!empty($pc)) { $pc = array_pop($pc); $row['managerip'] = $pc['clientip']; } } if (!empty($row['managerip'])) { - $this->managerIp = $row['managerip']; + $this->managerIp = (string)$row['managerip']; } if (!empty($row['tutorip'])) { - $this->tutorIp = $row['tutorip']; + $this->tutorIp = (string)$row['tutorip']; } } @@ -61,7 +62,7 @@ class SimpleRoom extends Room return count($this->machines); } - public function getSize(?int &$width, ?int &$height) + public function getSize(?int &$width, ?int &$height): void { if (empty($this->machines)) { $width = $height = 0; @@ -124,12 +125,12 @@ class SimpleRoom extends Room } } - public function getManagerIp() + public function getManagerIp(): ?string { return $this->managerIp; } - public function getTutorIp() + public function getTutorIp(): ?string { return $this->tutorIp; } @@ -144,7 +145,7 @@ class SimpleRoom extends Room return empty($this->machines); } - protected function sanitize() + protected function sanitize(): void { // Nothing } diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php index 9b114528..3286b71a 100644 --- a/modules-available/roomplanner/page.inc.php +++ b/modules-available/roomplanner/page.inc.php @@ -23,7 +23,7 @@ class Page_Roomplanner extends Page */ private $isLeaf; - private function loadRequestedLocation() + private function loadRequestedLocation(): void { $this->locationid = Request::get('locationid', Request::REQUIRED, 'int'); $locs = Location::getLocationsAssoc(); @@ -74,7 +74,7 @@ class Page_Roomplanner extends Page } } - private function showLeafEditor() + private function showLeafEditor(): void { $config = Database::queryFirst('SELECT roomplan, managerip, tutoruuid FROM location_roomplan @@ -117,7 +117,7 @@ class Page_Roomplanner extends Page Render::addTemplate('footer', $params); } - private function showComposedEditor() + private function showComposedEditor(): void { // Load settings $row = Database::queryFirst("SELECT locationid, roomplan @@ -213,7 +213,7 @@ class Page_Roomplanner extends Page } } - private function handleSaveRequest($isAjax) + private function handleSaveRequest(bool $isAjax): void { User::assertPermission('edit', $this->locationid); $leaf = (bool)Request::post('isleaf', 1, 'int'); @@ -232,7 +232,7 @@ class Page_Roomplanner extends Page } } - private function saveLeafRoom($isAjax) + private function saveLeafRoom(bool $isAjax): void { $machinesOnPlan = $this->getMachinesOnPlan('invalid'); $config = Request::post('serializedRoom', null, 'string'); @@ -262,9 +262,9 @@ class Page_Roomplanner extends Page $this->saveComputerConfig($config['computers'], $machinesOnPlan); } - private function saveComposedRoom($isAjax) + private function saveComposedRoom(bool $isAjax): void { - $room = new ComposedRoom(true); + $room = new ComposedRoom(null); $res = Database::exec('INSERT INTO location_roomplan (locationid, roomplan) VALUES (:lid, :plan) ON DUPLICATE KEY UPDATE roomplan = VALUES(roomplan)', ['lid' => $this->locationid, 'plan' => $room->serialize()]); @@ -278,20 +278,11 @@ class Page_Roomplanner extends Page } } - private function sanitizeNumber(&$number, $lower, $upper) - { - if (!is_numeric($number) || $number < $lower) { - $number = $lower; - } elseif ($number > $upper) { - $number = $upper; - } - } - /** * @param array $computers Deserialized json from browser with all the computers * @param array $oldComputers Deserialized old roomplan from database, used to find removed computers */ - protected function saveComputerConfig(array $computers, array $oldComputers) + protected function saveComputerConfig(array $computers, array $oldComputers): void { $oldUuids = []; @@ -336,7 +327,7 @@ class Page_Roomplanner extends Page } } - protected function saveRoomConfig(?array $furniture, ?string $tutorUuid) + protected function saveRoomConfig(?array $furniture, ?string $tutorUuid): void { $obj = json_encode(['furniture' => $furniture]); $managerIp = Request::post('managerip', '', 'string'); diff --git a/modules-available/statistics/pages/list.inc.php b/modules-available/statistics/pages/list.inc.php index f9cf413c..0cb4a141 100644 --- a/modules-available/statistics/pages/list.inc.php +++ b/modules-available/statistics/pages/list.inc.php @@ -159,9 +159,9 @@ class SubPage $side = []; if (!empty($rows) && !empty($colValCount)) { if (count($colValCount['locationid']) === 1 - && ($lid = array_key_first($colValCount['locationid'])) > 0 + && ($lid = (int)array_key_first($colValCount['locationid'])) > 0 && Module::isAvailable('roomplanner')) { - $roomsvg = PvsGenerator::generateSvg($lid, false, 0, 1, true, $colValCount['locationid'][$lid]); + $roomsvg = PvsGenerator::generateSvg($lid, null, 0, 1, true, $colValCount['locationid'][$lid]); } // Handle our selected attributes foreach (['locationid', 'cpumodel', 'nic-speed_s', 'gbram', 'gbtmp'] as $key) { diff --git a/modules-available/statistics/pages/machine.inc.php b/modules-available/statistics/pages/machine.inc.php index 1fc9efd8..f7ebd472 100644 --- a/modules-available/statistics/pages/machine.inc.php +++ b/modules-available/statistics/pages/machine.inc.php @@ -72,7 +72,7 @@ class SubPage $locations = Location::getLocationRootChain($client['locationid']); } if ($client['locationid'] && $client['hasroomplan'] && Module::isAvailable('roomplanner')) { - $client['roomsvg'] = PvsGenerator::generateSvg($client['locationid'], $client['machineuuid'], + $client['roomsvg'] = PvsGenerator::generateSvg((int)$client['locationid'], $client['machineuuid'], 0, 1, true); } User::assertPermission('machine.view-details', (int)$client['locationid']); |
