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 --- .../serversetup-bwlp-ipxe/inc/bootentry.inc.php | 95 +++++++------- .../inc/bootentryhook.inc.php | 53 +++----- .../serversetup-bwlp-ipxe/inc/execdata.inc.php | 7 +- .../serversetup-bwlp-ipxe/inc/ipxe.inc.php | 146 ++++++++++----------- .../serversetup-bwlp-ipxe/inc/ipxebuilder.inc.php | 16 +-- .../serversetup-bwlp-ipxe/inc/ipxemenu.inc.php | 19 ++- .../serversetup-bwlp-ipxe/inc/localboot.inc.php | 3 + .../serversetup-bwlp-ipxe/inc/menuentry.inc.php | 49 +++---- .../serversetup-bwlp-ipxe/inc/pxelinux.inc.php | 36 ++++- .../inc/scriptbuilderbase.inc.php | 49 +++---- .../inc/scriptbuilderbash.inc.php | 20 +-- .../inc/scriptbuilderipxe.inc.php | 45 +++---- 12 files changed, 257 insertions(+), 281 deletions(-) (limited to 'modules-available/serversetup-bwlp-ipxe/inc') diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php index 2abb5153..919861f0 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php @@ -22,17 +22,13 @@ abstract class BootEntry $this->internalId = $internalId; } - public abstract function supportsMode($mode); + public abstract function supportsMode(string $mode): bool; - /** - * @param ScriptBuilderBase $builder - * @return string - */ - public abstract function toScript($builder); + public abstract function toScript(ScriptBuilderBase $builder): string; - public abstract function toArray(); + public abstract function toArray(): array; - public abstract function addFormFields(&$array); + public abstract function addFormFields(array &$array): void; public function internalId(): string { @@ -48,14 +44,14 @@ abstract class BootEntry * * @param string $module module this entry belongs to, or special values .script/.exec * @param string $data serialized entry data - * @return BootEntry|null instance representing boot entry, null on error + * @return ?BootEntry instance representing boot entry, null on error */ - public static function fromJson($module, $data) + public static function fromJson(string $module, string $data): ?BootEntry { if ($module[0] !== '.') { // Hook from other module $hook = Hook::loadSingle($module, 'ipxe-bootentry'); - if ($hook === false) { + if ($hook === null) { error_log('Module ' . $module . ' doesnt have an ipxe-bootentry hook'); return null; } @@ -64,9 +60,9 @@ abstract class BootEntry return null; return $ret->getBootEntry($data); } - if (is_string($data)) { - $data = json_decode($data, true); - } + $data = json_decode($data, true); + if (!is_array($data)) + return null; if ($module === '.script') { return new CustomBootEntry($data); } @@ -79,12 +75,12 @@ abstract class BootEntry return null; } - public static function forMenu($menuId) + public static function forMenu(int $menuId): MenuBootEntry { return new MenuBootEntry($menuId); } - public static function newStandardBootEntry($initData, $efi = false, $arch = false, string $internalId = '') + public static function newStandardBootEntry($initData, $efi = false, $arch = false, string $internalId = ''): ?StandardBootEntry { $ret = new StandardBootEntry($initData, $efi, $arch, $internalId); $list = []; @@ -104,7 +100,7 @@ abstract class BootEntry return $ret; } - public static function newCustomBootEntry($initData) + public static function newCustomBootEntry($initData): ?CustomBootEntry { if (!is_array($initData) || empty($initData)) return null; @@ -114,15 +110,14 @@ abstract class BootEntry /** * Return a BootEntry instance from database with the given id. * - * @param string $id - * @return BootEntry|null|false false == unknown id, null = unknown entry type, BootEntry instance on success + * @return ?BootEntry null = unknown entry type, BootEntry instance on success */ - public static function fromDatabaseId($id) + public static function fromDatabaseId(string $id): ?BootEntry { $row = Database::queryFirst("SELECT module, data FROM serversetup_bootentry WHERE entryid = :id LIMIT 1", ['id' => $id]); if ($row === false) - return false; + return null; return self::fromJson($row['module'], $row['data']); } @@ -132,7 +127,7 @@ abstract class BootEntry * * @return BootEntry[] all existing BootEntries */ - public static function getAll() + public static function getAll(): array { $res = Database::simpleQuery("SELECT entryid, module, data FROM serversetup_bootentry"); $ret = []; @@ -158,13 +153,13 @@ class StandardBootEntry extends BootEntry */ protected $efi; /** - * @var string BootEntry Constants above + * @var ?string BootEntry Constants above */ protected $arch; const KEYS = ['executable', 'initRd', 'commandLine', 'replace', 'imageFree', 'autoUnload', 'resetConsole', 'dhcpOptions']; - public function __construct($data, $efi = false, $arch = false, string $internalId = '') + public function __construct($data, $efi = false, ?string $arch = null, string $internalId = '') { parent::__construct($internalId); $this->pcbios = new ExecData(); @@ -239,10 +234,7 @@ class StandardBootEntry extends BootEntry } } - /** - * @param PxeSection $data - */ - private function fromPxeMenu($data) + private function fromPxeMenu(PxeSection $data): void { $bios = $this->pcbios; $bios->executable = $data->kernel; @@ -266,12 +258,12 @@ class StandardBootEntry extends BootEntry $bios->commandLine = trim(preg_replace('/\s+/', ' ', $bios->commandLine)); } - public function arch() + public function arch(): ?string { return $this->arch; } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { if ($mode === $this->arch || $this->arch === BootEntry::AGNOSTIC) return true; @@ -282,7 +274,7 @@ class StandardBootEntry extends BootEntry return false; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { if ($this->arch === BootEntry::AGNOSTIC) // Same as below, could construct fall-through but this is more clear return $builder->execDataToScript($this->pcbios, null, null); @@ -291,7 +283,7 @@ class StandardBootEntry extends BootEntry $this->supportsMode(BootEntry::EFI) ? $this->efi : null); } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { $array[$this->arch . '_selected'] = 'selected'; $array['entries'][] = $this->pcbios->toFormFields(BootEntry::BIOS); @@ -299,7 +291,10 @@ class StandardBootEntry extends BootEntry $array['exec_checked'] = 'checked'; } - public function toArray() + /** + * @return array{PCBIOS: array, EFI: array, arch: string} + */ + public function toArray(): array { return [ BootEntry::BIOS => $this->pcbios->toArray(), @@ -314,7 +309,7 @@ class CustomBootEntry extends BootEntry /** * @var string iPXE */ - protected $ipxe; + protected $ipxe = ''; protected $bash; @@ -331,12 +326,12 @@ class CustomBootEntry extends BootEntry } } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { if ($builder instanceof ScriptBuilderIpxe) return $this->ipxe; @@ -345,7 +340,7 @@ class CustomBootEntry extends BootEntry return ''; } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { $array['entry'] = [ 'script' => $this->ipxe, @@ -353,7 +348,10 @@ class CustomBootEntry extends BootEntry $array['script_checked'] = 'checked'; } - public function toArray() + /** + * @return array{script: string} + */ + public function toArray(): array { return ['script' => $this->ipxe]; } @@ -361,31 +359,32 @@ class CustomBootEntry extends BootEntry class MenuBootEntry extends BootEntry { + /** @var int */ protected $menuId; - public function __construct($menuId) + public function __construct(int $menuId) { parent::__construct('menu-' . $menuId); $this->menuId = $menuId; } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { - $menu = IPxeMenu::get($this->menuId); + $menu = IPxeMenu::get($this->menuId, true); return $builder->menuToScript($menu); } - public function toArray() + public function toArray(): array { return []; } - public function addFormFields(&$array) + public function addFormFields(array &$array): void { } } @@ -401,21 +400,21 @@ class SpecialBootEntry extends BootEntry parent::__construct('special-' . $this->type); } - public function supportsMode($mode) + public function supportsMode(string $mode): bool { return true; } - public function toScript($builder) + public function toScript(ScriptBuilderBase $builder): string { return $builder->getSpecial($this->type); } - public function toArray() + public function toArray(): array { return []; } - public function addFormFields(&$array) { } + public function addFormFields(array &$array): void { } } \ No newline at end of file diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php index 060b3903..ab55c888 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php @@ -6,41 +6,37 @@ abstract class BootEntryHook /** * @var string -- set by ipxe, not module implementing hook */ - public $moduleId; + public $moduleId = ''; /** * @var string -- set by ipxe, not module implementing hook */ - public $checked; + public $checked = ''; - private $selectedId; + private $selectedId = ''; private $data = []; /** * @return string */ - public abstract function name(); + public abstract function name(): string; /** * @return HookExtraField[] */ - public abstract function extraFields(); + public abstract function extraFields(): array; - /** - * @param string $id - * @return bool - */ - public abstract function isValidId($id); + public abstract function isValidId(string $id): bool; /** * @return HookEntryGroup[] */ - protected abstract function groupsInternal(); + protected abstract function groupsInternal(): array; /** * @return HookEntryGroup[] */ - public final function groups() + public final function groups(): array { $groups = $this->groupsInternal(); foreach ($groups as $group) { @@ -54,16 +50,13 @@ abstract class BootEntryHook } /** - * @param $id * @return BootEntry|null the actual boot entry instance for given entry, null if invalid id */ - public abstract function getBootEntryInternal($localData); + public abstract function getBootEntryInternal(array $localData): ?BootEntry; - public final function getBootEntry($data) + public final function getBootEntry(string $jsonString): ?BootEntry { - if (!is_array($data)) { - $data = json_decode($data, true); - } + $data = json_decode($jsonString, true); return $this->getBootEntryInternal($data); } @@ -71,7 +64,7 @@ abstract class BootEntryHook * @param string $mixed either the plain ID if the entry to be marked as selected, or the JSON string representing * the entire entry, which must have a key called 'id' that will be used as the ID then. */ - public function setSelected($mixed) + public function setSelected(string $mixed): void { $json = @json_decode($mixed, true); if (is_array($json)) { @@ -86,12 +79,15 @@ abstract class BootEntryHook /** * @return string ID of entry that was marked as selected by setSelected() */ - public function getSelected() + public function getSelected(): string { return $this->selectedId; } - public function renderExtraFields() + /** + * @return HookExtraField[] + */ + public function renderExtraFields(): array { $list = $this->extraFields(); foreach ($list as $entry) { @@ -144,14 +140,7 @@ class HookEntry */ public $selected; - /** - * HookEntry constructor. - * - * @param string $id - * @param string $name - * @param bool $valid - */ - public function __construct($id, $name, $valid) + public function __construct(string $id, string $name, bool $valid) { $this->id = $id; $this->name = $name; @@ -182,7 +171,7 @@ class HookExtraField */ public $hook; - public function __construct($name, $type, $default) + public function __construct(string $name, string $type, $default) { $this->name = $name; $this->type = $type; @@ -203,10 +192,10 @@ class HookExtraField return $val; } - public function html() + public function html(): string { $fieldId = 'extra-' . $this->hook->moduleId . '-' . $this->name; - $fieldText = htmlspecialchars(Dictionary::translateFileModule($this->hook->moduleId, 'module', 'ipxe-' . $this->name, true)); + $fieldText = htmlspecialchars(Dictionary::translateFileModule($this->hook->moduleId, 'module', 'ipxe-' . $this->name)); if (is_array($this->type)) { $out = '