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 +++++++++++----------- 1 file changed, 47 insertions(+), 48 deletions(-) (limited to 'modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php') 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 -- cgit v1.2.3-55-g7522