From d84040b08f2974c3c31dcfb69d490c7ce8a2183b Mon Sep 17 00:00:00 2001 From: Curtis Conard Date: Sat, 27 Apr 2024 15:40:04 -0400 Subject: [PATCH 1/6] Clean Device Code --- CHANGELOG.md | 1 + src/CommonDBRelation.php | 2 +- src/CommonDevice.php | 228 +++++++++++------------------- src/DeviceBattery.php | 23 ++- src/DeviceBatteryModel.php | 1 - src/DeviceCamera.php | 29 ++-- src/DeviceCase.php | 29 ++-- src/DeviceControl.php | 54 ++++--- src/DeviceDrive.php | 39 ++--- src/DeviceFirmware.php | 31 ++-- src/DeviceGeneric.php | 34 ++--- src/DeviceGraphicCard.php | 53 +++---- src/DeviceHardDrive.php | 55 +++---- src/DeviceMemory.php | 46 +++--- src/DeviceMotherboard.php | 30 ++-- src/DeviceNetworkCard.php | 69 ++++----- src/DevicePci.php | 36 ++--- src/DevicePowerSupply.php | 25 ++-- src/DeviceProcessor.php | 54 +++---- src/DeviceSensor.php | 40 +++--- src/DeviceSimcard.php | 25 ++-- src/DeviceSoundCard.php | 54 ++++--- src/PCIVendor.php | 60 ++++---- src/RegisteredID.php | 42 +++--- src/USBVendor.php | 53 ++++--- templates/dropdown_form.html.twig | 6 + 26 files changed, 466 insertions(+), 653 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 441b4e5621c..d7adf15062f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,6 +136,7 @@ The present file will list all changes made to the project; according to the - `Lock::getLocksQueryInfosByItemType()` has been made private. - `DBmysql::request()`, `DBmysqlIterator::buildQuery()` and `DBmysqlIterator::execute()` methods signatures changed. - Some values for the `$type` parameters of several `Stat` methods have changed to match English spelling (technicien -> technician). +- Multiple methods in `CommonDevice` and sub-classes now have return types defined (classes that extends these must match the new method signatures). #### Deprecated - Usage of `MAIL_SMTPSSL` and `MAIL_SMTPTLS` constants. diff --git a/src/CommonDBRelation.php b/src/CommonDBRelation.php index fffa2d2c1f7..5a0a1edf6ec 100644 --- a/src/CommonDBRelation.php +++ b/src/CommonDBRelation.php @@ -256,7 +256,7 @@ public static function getOppositeItemtype(?string $itemtype): ?string * * @param $number * - * @return boolean + * @return CommonDBTM|false **/ public function getOnePeer($number) { diff --git a/src/CommonDevice.php b/src/CommonDevice.php index 4af840a422c..9128bc6dc6d 100644 --- a/src/CommonDevice.php +++ b/src/CommonDevice.php @@ -52,20 +52,19 @@ abstract class CommonDevice extends CommonDropdown public $second_level_menu = "commondevice"; public $third_level_menu = ""; - public static function getTypeName($nb = 0) + public static function getTypeName($nb = 0): string { return _n('Component', 'Components', $nb); } - /** * Get all the kind of devices available inside the system. * * @since 0.85 * - * @return array of the types of CommonDevice available + * @return array Array of the types of CommonDevice available **/ - public static function getDeviceTypes() + public static function getDeviceTypes(): array { /** @var array $CFG_GLPI */ global $CFG_GLPI; @@ -73,8 +72,6 @@ public static function getDeviceTypes() return $CFG_GLPI['device_types']; } - - /** * Get the associated item_device associated with this device * This method can be overridden, for instance by the plugin @@ -82,15 +79,14 @@ public static function getDeviceTypes() * @since 0.85 * @since 9.3 added the $devicetype parameter * - * @param string $devicetype class name of device type, defaults to called class name + * @param class-string|null $devicetype class name of device type, defaults to called class name * - * @return string + * @return class-string **/ - public static function getItem_DeviceType($devicetype = null) + public static function getItem_DeviceType($devicetype = null): string { - if (null === $devicetype) { - $devicetype = get_called_class(); + $devicetype = static::class; } if ($plug = isPluginItemType($devicetype)) { return 'Plugin' . $plug['plugin'] . 'Item_' . $plug['class']; @@ -98,15 +94,8 @@ public static function getItem_DeviceType($devicetype = null) return "Item_$devicetype"; } - - /** - * @see CommonGLPI::getMenuContent() - * - * @since 0.85 - **/ - public static function getMenuContent() + public static function getMenuContent(): false|array { - $menu = []; if (self::canView()) { $menu['title'] = static::getTypeName(Session::getPluralNumber()); @@ -116,17 +105,18 @@ public static function getMenuContent() $dps = Dropdown::getDeviceItemTypes(); foreach ($dps as $tab) { + /** @var class-string $key */ foreach ($tab as $key => $val) { if ($tmp = getItemForItemtype($key)) { $menu['options'][$key] = [ 'title' => $val, - 'page' => $tmp->getSearchURL(false), + 'page' => $tmp::getSearchURL(false), 'links' => [ - 'search' => $tmp->getSearchURL(false), + 'search' => $tmp::getSearchURL(false), ] ]; - if ($tmp->canCreate()) { - $menu['options'][$key]['links']['add'] = $tmp->getFormURL(false); + if ($tmp::canCreate()) { + $menu['options'][$key]['links']['add'] = $tmp::getFormURL(false); } if ($itemClass = getItemForItemtype(self::getItem_DeviceType($key))) { @@ -137,11 +127,11 @@ public static function getMenuContent() $listLabel = '' . '' . $itemTypeName . ''; - $menu['options'][$key]['links'][$listLabel] = $itemClass->getSearchURL(false); + $menu['options'][$key]['links'][$listLabel] = $itemClass::getSearchURL(false); // item device self links - $item_device_key = $itemClass->getType(); - $item_device_search_url = $itemClass->getSearchURL(false); + $item_device_key = $itemClass::class; + $item_device_search_url = $itemClass::getSearchURL(false); $menu['options'][$item_device_key] = [ 'title' => $itemTypeName, 'page' => $item_device_search_url, @@ -149,8 +139,8 @@ public static function getMenuContent() 'search' => $item_device_search_url, ], ]; - if ($itemClass->canCreate()) { - $menu['options'][$item_device_key]['links']['add'] = $itemClass->getFormURL(false); + if ($itemClass::canCreate()) { + $menu['options'][$item_device_key]['links']['add'] = $itemClass::getFormURL(false); } } } @@ -163,9 +153,8 @@ public static function getMenuContent() return false; } - public function displaySpecificTypeField($ID, $field = [], array $options = []) + public function displaySpecificTypeField($ID, $field = [], array $options = []): void { - switch ($field['type']) { case 'registeredIDChooser': RegisteredID::showChildsForItemForm($this, '_registeredID'); @@ -173,28 +162,18 @@ public function displaySpecificTypeField($ID, $field = [], array $options = []) } } - - public function getAdditionalFields() + public function getAdditionalFields(): array { - - return [['name' => 'manufacturers_id', - 'label' => Manufacturer::getTypeName(1), - 'type' => 'dropdownValue' - ] + return [ + [ + 'name' => 'manufacturers_id', + 'label' => Manufacturer::getTypeName(1), + 'type' => 'dropdownValue' + ] ]; } - /** - * Can I change recursive flag to false - * check if there is "linked" object in another entity - * - * Overloaded from CommonDBTM - * - * @since 0.85 - * - * @return boolean - **/ - public function canUnrecurs() + public function canUnrecurs(): bool { /** @var \DBmysql $DB */ global $DB; @@ -256,8 +235,7 @@ public function canUnrecurs() return true; } - - public function rawSearchOptions() + public function rawSearchOptions(): array { $tab = []; @@ -268,7 +246,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '1', - 'table' => $this->getTable(), + 'table' => static::getTable(), 'field' => 'designation', 'name' => __('Name'), 'datatype' => 'itemlink', @@ -277,7 +255,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '2', - 'table' => $this->getTable(), + 'table' => static::getTable(), 'field' => 'id', 'name' => __('ID'), 'datatype' => 'number', @@ -294,7 +272,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '16', - 'table' => $this->getTable(), + 'table' => static::getTable(), 'field' => 'comment', 'name' => __('Comments'), 'datatype' => 'text' @@ -302,7 +280,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '19', - 'table' => $this->getTable(), + 'table' => static::getTable(), 'field' => 'date_mod', 'name' => __('Last update'), 'datatype' => 'datetime', @@ -311,7 +289,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '121', - 'table' => $this->getTable(), + 'table' => static::getTable(), 'field' => 'date_creation', 'name' => __('Creation date'), 'datatype' => 'datetime', @@ -329,46 +307,35 @@ public function rawSearchOptions() return $tab; } - - public function title() + public function title(): void { - Dropdown::showItemTypeMenu( _n('Component', 'Components', Session::getPluralNumber()), Dropdown::getDeviceItemTypes(), - $this->getSearchURL() + self::getSearchURL() ); } - /** - * @since 0.84 - * - * @see CommonDBTM::getNameField - * - * @return string - **/ - public static function getNameField() + public static function getNameField(): string { return 'designation'; } - /** - * @since 0.84 - * get the HTMLTable Header for the current device according to the type of the item that - * is requesting - * - * @param $itemtype string the type of the item - * @param $base HTMLTableBase object:the element on which adding the header + * get the HTMLTable Header for the current device according to the type of the item that is requesting + * @param string $itemtype The type of the item + * @param HTMLTableBase $base The element on which adding the header * (ie.: HTMLTableMain or HTMLTableGroup) - * @param $super HTMLTableSuperHeader object: the super header + * @param HTMLTableSuperHeader|null $super The super header * (in case of adding to HTMLTableGroup) (default NULL) - * @param $father HTMLTableHeader object: the father of the current headers + * @param HTMLTableHeader|null $father The father of the current headers * (default NULL) - * @param $options array parameter such as restriction + * @param array $options parameter such as restriction * * @return HTMLTableHeader|void - **/ + * @throws Exception + * @since 0.84 + */ public static function getHTMLTableHeader( $itemtype, HTMLTableBase $base, @@ -376,10 +343,7 @@ public static function getHTMLTableHeader( HTMLTableHeader $father = null, array $options = [] ) { - - $this_type = get_called_class(); - - if (isset($options['dont_display'][$this_type])) { + if (isset($options['dont_display'][static::class])) { return $father; } @@ -393,7 +357,7 @@ public static function getHTMLTableHeader( if (in_array($itemtype, $linktype::itemAffinity()) || in_array('*', $linktype::itemAffinity())) { $column = $base->addHeader('device', $content, $super, $father); $column->setItemType( - $this_type, + static::class, isset($options['itemtype_title']) ? $options['itemtype_title'] : '' ); } else { @@ -403,47 +367,45 @@ public static function getHTMLTableHeader( return $column; } - /** - * @since 0.84 - * + * @param HTMLTableRow|null $row object + * @param CommonDBTM|null $item object (default NULL) + * @param HTMLTableCell|null $father object (default NULL) + * @param array $options + * @return HTMLTableCell|null + * @throws Exception * @warning note the difference between getHTMLTableCellForItem and getHTMLTableCellsForItem - * - * @param $row HTMLTableRow object - * @param $item CommonDBTM object (default NULL) - * @param $father HTMLTableCell object (default NULL) - * @param $options array - **/ + * @since 0.84 + */ public function getHTMLTableCellForItem( HTMLTableRow $row = null, CommonDBTM $item = null, HTMLTableCell $father = null, array $options = [] - ) { - - $this_type = $this->getType(); + ): ?HTMLTableCell + { - if (isset($options['dont_display'][$this_type])) { + if (isset($options['dont_display'][static::class])) { return $father; } if (static::canView()) { $content = $this->getLink([ - 'icon' => $this->getIcon() + 'icon' => self::getIcon() ]); } else { $content = $this->getName([ - 'icon' => $this->getIcon() + 'icon' => self::getIcon() ]); } if ($options['canedit']) { - $field_name = 'quantity_' . $this->getType() . '_' . $this->getID(); + $field_name = 'quantity_' . static::class . '_' . $this->getID(); $content .= " " . __s('Add') . ""; $content .= "