Skip to content

Commit 6a52507

Browse files
RomainLvrstonebuzz
authored andcommitted
Fix phpstan
1 parent 459f50d commit 6a52507

12 files changed

+93
-64
lines changed

ajax/container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
$subtype = $_GET['subtype'];
4848
$input = $_GET['input'];
4949

50-
$item = new $itemtype();
50+
$dbu = new DbUtils();
51+
$item = $dbu->getItemForItemtype($itemtype);
5152
if ($items_id > 0 && !$item->getFromDB($items_id)) {
5253
throw new HttpException(404, 'Not Found');
5354
}

ajax/container_display_condition.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,5 @@
5555
}
5656
}
5757
} else {
58-
http_response_code(400);
59-
die();
58+
throw new \RuntimeException('Invalid request', 400);
6059
}

ajax/reorder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
|| !array_key_exists('new_order', $_POST)
3838
) {
3939
// Missing input
40-
exit();
40+
throw new \RuntimeException('Missing input', 400);
4141
}
4242

4343
$table = PluginFieldsField::getTable();
@@ -62,7 +62,7 @@
6262

6363
if (0 === $field_iterator->count()) {
6464
// Unknown field
65-
exit();
65+
throw new \RuntimeException('Unknown field', 404);
6666
}
6767

6868
$field_id = $field_iterator->current()['id'];

ajax/status_override.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,5 @@
4343
$status_override->showForm($_GET['id'], $_GET);
4444
}
4545
} else {
46-
http_response_code(400);
47-
die();
46+
throw new \RuntimeException('Invalid request', 400);
4847
}

ajax/viewtranslations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
Session::checkLoginUser();
4040

4141
if (!isset($_POST['itemtype']) || !isset($_POST['items_id']) || !isset($_POST['id'])) {
42-
exit();
42+
throw new \RuntimeException('Missing required parameters', 400);
4343
}
4444

4545
$translation = new PluginFieldsLabelTranslation();

inc/container.class.php

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public static function installBaseData(Migration $migration, $version)
9696
PRIMARY KEY (`id`),
9797
KEY `entities_id` (`entities_id`)
9898
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
99-
$DB->doQuery($query) or die($DB->error());
99+
if (!$DB->doQuery($query)) {
100+
throw new \RuntimeException('Error creating plugin_fields_containers table: ' . $DB->error());
101+
}
100102
}
101103

102104
// multiple itemtype for one container
@@ -256,9 +258,10 @@ public static function installUserData(Migration $migration, $version)
256258
$fieldsdata = $fields->find(['plugin_fields_containers_id' => $ostab]);
257259

258260
$classname = self::getClassname(Computer::getType(), $oscontainer->fields['name']);
259-
$osdata = new $classname();
261+
$dbu = new DbUtils();
262+
$osdata = $dbu->getItemForItemtype($classname);
260263
$classname = self::getClassname(Computer::getType(), $compcontainer->fields['name']);
261-
$compdata = new $classname();
264+
$compdata = $dbu->getItemForItemtype($classname);
262265

263266
$fieldnames = [];
264267
//add fields to compcontainer
@@ -278,7 +281,7 @@ public static function installUserData(Migration $migration, $version)
278281
],
279282
);
280283
}
281-
$compdata::addField($newname, $field['type']);
284+
$compdata->addField($newname, $field['type']);
282285
$fieldnames[$field['name']] = $newname;
283286
}
284287

@@ -768,7 +771,8 @@ public static function preItemPurge($item)
768771
$itemtypes = json_decode($container['itemtypes']);
769772
if (in_array($itemtype, $itemtypes)) {
770773
$classname = 'PluginFields' . $itemtype . getSingular($container['name']);
771-
$fields = new $classname();
774+
$dbu = new DbUtils();
775+
$fields = $dbu->getItemForItemtype($classname);
772776
$fields->deleteByCriteria(['items_id' => $item->fields['id']], true);
773777
}
774778
}
@@ -892,7 +896,8 @@ public function showForm($ID, $options = [])
892896
if ($ID > 0 && !empty($this->fields['subtype'])) {
893897
$itemtypes = json_decode($this->fields['itemtypes'], true);
894898
$itemtype = array_shift($itemtypes);
895-
$item = new $itemtype();
899+
$dbu = new DbUtils();
900+
$item = $dbu->getItemForItemtype($itemtype);
896901
$item->getEmpty();
897902
$tabs = self::getSubtypes($item);
898903
echo $tabs[$this->fields['subtype']];
@@ -962,41 +967,44 @@ public static function showFormSubtype($params, $display = false)
962967
$out = "<script type='text/javascript'>jQuery('#tab_tr').hide();</script>";
963968
if (isset($params['type']) && $params['type'] == 'domtab') {
964969
if (class_exists($params['itemtype'])) {
965-
$item = new $params['itemtype']();
966-
$item->getEmpty();
967-
968-
$tabs = self::getSubtypes($item);
970+
$dbu = new DbUtils();
971+
$item = $dbu->getItemForItemtype($params['itemtype']);
972+
if ($item !== false) {
973+
$item->getEmpty();
974+
975+
$tabs = self::getSubtypes($item);
976+
977+
if (count($tabs)) {
978+
// delete Log of array (don't work with this tab)
979+
$tabs_to_remove = ['Log$1', 'Document_Item$1'];
980+
foreach ($tabs_to_remove as $tab_to_remove) {
981+
if (isset($tabs[$tab_to_remove])) {
982+
unset($tabs[$tab_to_remove]);
983+
}
984+
}
969985

970-
if (count($tabs)) {
971-
// delete Log of array (don't work with this tab)
972-
$tabs_to_remove = ['Log$1', 'Document_Item$1'];
973-
foreach ($tabs_to_remove as $tab_to_remove) {
974-
if (isset($tabs[$tab_to_remove])) {
975-
unset($tabs[$tab_to_remove]);
986+
// For delete <sup class='tab_nb'>number</sup> :
987+
foreach ($tabs as &$value) {
988+
$results = [];
989+
if (preg_match_all('#<sup.*>(.+)</sup>#', $value, $results)) {
990+
$value = str_replace($results[0][0], '', $value);
991+
}
976992
}
977-
}
978993

979-
// For delete <sup class='tab_nb'>number</sup> :
980-
foreach ($tabs as &$value) {
981-
$results = [];
982-
if (preg_match_all('#<sup.*>(.+)</sup>#', $value, $results)) {
983-
$value = str_replace($results[0][0], '', $value);
994+
if (!isset($params['subtype'])) {
995+
$params['subtype'] = null;
984996
}
985-
}
986997

987-
if (!isset($params['subtype'])) {
988-
$params['subtype'] = null;
998+
$out .= Dropdown::showFromArray(
999+
'subtype',
1000+
$tabs,
1001+
['value' => $params['subtype'],
1002+
'width' => '100%',
1003+
'display' => false,
1004+
],
1005+
);
1006+
$out .= "<script type='text/javascript'>jQuery('#tab_tr').show();</script>";
9891007
}
990-
991-
$out .= Dropdown::showFromArray(
992-
'subtype',
993-
$tabs,
994-
['value' => $params['subtype'],
995-
'width' => '100%',
996-
'display' => false,
997-
],
998-
);
999-
$out .= "<script type='text/javascript'>jQuery('#tab_tr').show();</script>";
10001008
}
10011009
}
10021010
}
@@ -1241,7 +1249,8 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
12411249
$items_id = $data['items_id'];
12421250
$classname = self::getClassname($itemtype, $container_obj->fields['name']);
12431251

1244-
$obj = new $classname();
1252+
$dbu = new DbUtils();
1253+
$obj = $dbu->getItemForItemtype($classname);
12451254
if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) {
12461255
// add fields data
12471256
$obj->add($data);
@@ -1315,7 +1324,8 @@ public static function constructHistory(
13151324
$field_obj
13161325
) {
13171326
// Don't log few itemtypes
1318-
$obj = new $itemtype();
1327+
$dbu = new DbUtils();
1328+
$obj = $dbu->getItemForItemtype($itemtype);
13191329
if ($obj->dohistory == false) {
13201330
return;
13211331
}
@@ -1467,7 +1477,8 @@ public static function validateValues($data, $itemtype, $massiveaction)
14671477
if ($container->fields['type'] === 'dom') {
14681478
$status_value = $data[$status_field_name] ?? null;
14691479
} else {
1470-
$relatedItem = new $itemtype();
1480+
$dbu = new DbUtils();
1481+
$relatedItem = $dbu->getItemForItemtype($itemtype);
14711482
$status_value = $relatedItem->fields[$status_field_name] ?? null;
14721483
}
14731484
// Apply status overrides

inc/containerdisplaycondition.class.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public static function installBaseData(Migration $migration, $version)
7777
PRIMARY KEY (`id`),
7878
KEY `plugin_fields_containers_id_itemtype` (`plugin_fields_containers_id`, `itemtype`)
7979
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
80-
$DB->doQuery($query) or die($DB->error());
80+
if (!$DB->doQuery($query)) {
81+
throw new \RuntimeException('Error creating plugin_fields_containers_displayconditions table: ' . $DB->error());
82+
}
8183
}
8284

8385
return true;
@@ -297,7 +299,8 @@ public static function showSearchOptionCondition($searchoption_id, $itemtype, ?s
297299
case 'impact':
298300
case 'urgency':
299301
case 'priority':
300-
$twig_params['item'] = new $itemtype();
302+
$dbu = new DbUtils();
303+
$twig_params['item'] = $dbu->getItemForItemtype($itemtype);
301304
$twig_params['itemtype_field'] = $so['field'];
302305
break;
303306
case 'global_validation':
@@ -321,7 +324,8 @@ public static function getRawValue($searchoption_id, $itemtype, $value)
321324

322325
if ($so['datatype'] == 'dropdown' || ($so['datatype'] == 'itemlink' && $so['table'] !== $itemtypetable)) {
323326
$dropdown_itemtype = getItemTypeForTable($so['table']);
324-
$dropdown = new $dropdown_itemtype();
327+
$dbu = new DbUtils();
328+
$dropdown = $dbu->getItemForItemtype($dropdown_itemtype);
325329
if ($dropdown->getFromDB($value)) {
326330
$raw_value = $dropdown->fields['name'];
327331
}
@@ -355,7 +359,8 @@ public static function getRawValue($searchoption_id, $itemtype, $value)
355359

356360
public static function removeBlackListedOption($array, $itemtype_class)
357361
{
358-
$itemtype_object = new $itemtype_class();
362+
$dbu = new DbUtils();
363+
$itemtype_object = $dbu->getItemForItemtype($itemtype_class);
359364
$allowed_so = [];
360365

361366
//remove "Common"

inc/field.class.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public static function installBaseData(Migration $migration, $version)
9292
KEY `is_active` (`is_active`),
9393
KEY `is_readonly` (`is_readonly`)
9494
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
95-
$DB->doQuery($query) or die($DB->error());
95+
if (!$DB->doQuery($query)) {
96+
throw new \RuntimeException('Error creating plugin_fields_fields table: ' . $DB->error());
97+
}
9698
}
9799

98100
$migration->displayMessage("Updating $table");
@@ -1121,7 +1123,8 @@ public static function prepareHtmlFields(
11211123
if (!$item->isNewItem()) {
11221124
//find row for this object with the items_id
11231125
$classname = PluginFieldsContainer::getClassname($item->getType(), $container_obj->fields['name']);
1124-
$obj = new $classname();
1126+
$dbu = new DbUtils();
1127+
$obj = $dbu->getItemForItemtype($classname);
11251128
$found_values = $obj->find(
11261129
[
11271130
'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'],
@@ -1164,12 +1167,15 @@ public static function prepareHtmlFields(
11641167
$field['dropdown_class'] = $dropdown_class;
11651168
$field['dropdown_condition'] = [];
11661169

1167-
$object = new $dropdown_class();
1168-
if ($object->maybeDeleted()) {
1169-
$field['dropdown_condition']['is_deleted'] = false;
1170-
}
1171-
if ($object->maybeActive()) {
1172-
$field['dropdown_condition']['is_active'] = true;
1170+
$dbu = new DbUtils();
1171+
$object = $dbu->getItemForItemtype($dropdown_class);
1172+
if ($object !== false) {
1173+
if ($object->maybeDeleted()) {
1174+
$field['dropdown_condition']['is_deleted'] = false;
1175+
}
1176+
if ($object->maybeActive()) {
1177+
$field['dropdown_condition']['is_active'] = true;
1178+
}
11731179
}
11741180
}
11751181

@@ -1322,7 +1328,8 @@ public static function showSingle($itemtype, $searchOption, $massiveaction = fal
13221328
];
13231329

13241330
//show field
1325-
$item = new $itemtype();
1331+
$dbu = new DbUtils();
1332+
$item = $dbu->getItemForItemtype($itemtype);
13261333
$item->getEmpty();
13271334

13281335
echo self::prepareHtmlFields($fields, $item, true, false, $massiveaction);

inc/labeltranslation.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public static function installBaseData(Migration $migration, $version)
6969
KEY `language` (`language`),
7070
UNIQUE KEY `unicity` (`itemtype`, `items_id`, `language`)
7171
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
72-
$DB->doQuery($query) or die($DB->error());
72+
if (!$DB->doQuery($query)) {
73+
throw new \RuntimeException('Error creating plugin_fields_labeltranslations table: ' . $DB->error());
74+
}
7375
}
7476

7577
if ($DB->fieldExists($table, 'plugin_fields_itemtype')) {

inc/profile.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public static function installBaseData(Migration $migration, $version)
6868
KEY `profiles_id` (`profiles_id`),
6969
KEY `plugin_fields_containers_id` (`plugin_fields_containers_id`)
7070
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
71-
$DB->doQuery($query) or die($DB->error());
71+
if (!$DB->doQuery($query)) {
72+
throw new \RuntimeException('Error creating plugin_fields_profiles table: ' . $DB->error());
73+
}
7274
}
7375

7476
return true;

inc/statusoverride.class.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public static function installBaseData(Migration $migration, $version)
6969
PRIMARY KEY (`id`),
7070
KEY `plugin_fields_fields_id` (`plugin_fields_fields_id`)
7171
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
72-
$DB->doQuery($query) or die($DB->error());
72+
if (!$DB->doQuery($query)) {
73+
throw new \RuntimeException('Error creating plugin_fields_statusoverrides table: ' . $DB->error());
74+
}
7375
}
7476

7577
return true;

setup.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,14 @@ function plugin_fields_exportBlockAsYaml($container_id = null)
350350

351351
switch ($field['type']) {
352352
case 'dropdown':
353-
$obj = new $itemtype();
353+
$dbu = new DbUtils();
354+
$obj = $dbu->getItemForItemtype($itemtype);
354355
$obj->getEmpty();
355356

356357
$dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']);
357358
$tmp_field['xml_node'] = strtoupper(getForeignKeyFieldForItemType($dropdown_itemtype));
358359

359-
$dropdown_obj = new $dropdown_itemtype();
360+
$dropdown_obj = $dbu->getItemForItemtype($dropdown_itemtype);
360361
$dropdown_datas = $dropdown_obj->find();
361362
$datas = [];
362363
foreach ($dropdown_datas as $value) {

0 commit comments

Comments
 (0)