Skip to content

Commit

Permalink
Throw exception when no item is found by getItemByTypeName()
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Feb 10, 2025
1 parent a8e8bdb commit 4f8ab6d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tests/src/autoload/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,17 +774,17 @@ function loadDataset()
/**
* Test helper, search an item from its type and name
*
* @param string $type
* @param string $name
* @param boolean $onlyid
* @return CommonDBTM|false the item, or its id
* @param string $type
* @param string $name
* @param bool $onlyid
* @return CommonDBTM|int the item, or its id
*/
function getItemByTypeName($type, $name, $onlyid = false)
function getItemByTypeName(string $type, string $name, bool $onlyid = false): CommonDBTM|int
{
$item = getItemForItemtype($type);
$nameField = $type::getNameField();
if ($item->getFromDBByCrit([$nameField => $name])) {
return ($onlyid ? $item->getField('id') : $item);
if (!$item->getFromDBByCrit([$nameField => $name])) {
throw new \RuntimeException(sprintf('Unable to load the `%s` item with the name `%s`.', $type, $name));
}
return false;
return ($onlyid ? $item->getID() : $item);
}

0 comments on commit 4f8ab6d

Please sign in to comment.