Skip to content

Commit 39b73cf

Browse files
LainowRom1-B
andauthored
Phpstan level 5 (#911)
* Phpstan level 5 * Fix PGP-CS-Fixer error * Update phpstan config (lvl 5) * Update inc/container.class.php Co-authored-by: Romain B. <[email protected]> * implement suggestions * update composer * implement suggestions * Update inc/inventory.class.php Co-authored-by: Romain B. <[email protected]> * Fix phpstan error --------- Co-authored-by: Romain B. <[email protected]>
1 parent 7772165 commit 39b73cf

16 files changed

+150
-122
lines changed

composer.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

front/container.form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
Html::redirect(PLUGINFIELDS_WEB_DIR . '/front/container.php');
4747
} elseif (isset($_REQUEST['purge'])) {
4848
$container->check($_REQUEST['id'], PURGE);
49-
$container->delete($_REQUEST, 1);
49+
$container->delete($_REQUEST, true);
5050
Html::redirect(PLUGINFIELDS_WEB_DIR . '/front/container.php');
5151
} elseif (isset($_POST['update'])) {
5252
$container->check($_POST['id'], UPDATE);

front/field.form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
Html::back();
4949
} elseif (isset($_REQUEST['purge'])) {
5050
$field->check($_REQUEST['id'], PURGE);
51-
$field->delete($_REQUEST, 1);
51+
$field->delete($_REQUEST, true);
5252
$field->redirectToList();
5353
} elseif (isset($_POST['update'])) {
5454
$field->check($_POST['id'], UPDATE);

hook.php

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -254,49 +254,46 @@ function plugin_fields_rule_matched($params = [])
254254

255255
$container = new PluginFieldsContainer();
256256

257-
switch ($params['sub_type']) {
258-
case 'PluginFusioninventoryTaskpostactionRule':
259-
/** @phpstan-ignore-next-line */
260-
$agent = new PluginFusioninventoryAgent();
261-
262-
if (isset($params['input']['plugin_fusioninventory_agents_id'])) {
263-
foreach ($params['output'] as $field => $value) {
264-
// check if current field is in a tab container
265-
$iterator = $DB->request([
266-
'SELECT' => 'glpi_plugin_fields_containers.id',
267-
'FROM' => 'glpi_plugin_fields_containers',
268-
'LEFT JOIN' => [
269-
'glpi_plugin_fields_fields' => [
270-
'FKEY' => [
271-
'glpi_plugin_fields_containers' => 'id',
272-
'glpi_plugin_fields_fields' => 'plugin_fields_containers_id',
273-
],
257+
if (class_exists('PluginFusioninventoryAgent') && $params['sub_type'] == 'PluginFusioninventoryTaskpostactionRule') {
258+
$agent = new PluginFusioninventoryAgent();
259+
260+
if (isset($params['input']['plugin_fusioninventory_agents_id'])) {
261+
foreach ($params['output'] as $field => $value) {
262+
// check if current field is in a tab container
263+
$iterator = $DB->request([
264+
'SELECT' => 'glpi_plugin_fields_containers.id',
265+
'FROM' => 'glpi_plugin_fields_containers',
266+
'LEFT JOIN' => [
267+
'glpi_plugin_fields_fields' => [
268+
'FKEY' => [
269+
'glpi_plugin_fields_containers' => 'id',
270+
'glpi_plugin_fields_fields' => 'plugin_fields_containers_id',
274271
],
275272
],
276-
'WHERE' => [
277-
'glpi_plugin_fields_fields.name' => $field,
273+
],
274+
'WHERE' => [
275+
'glpi_plugin_fields_fields.name' => $field,
276+
],
277+
]);
278+
if (count($iterator) > 0) {
279+
$data = $iterator->current();
280+
281+
//retrieve computer
282+
$agents_id = $params['input']['plugin_fusioninventory_agents_id'];
283+
$agent->getFromDB($agents_id);
284+
285+
// update current field
286+
$container->updateFieldsValues(
287+
[
288+
'plugin_fields_containers_id' => $data['id'],
289+
$field => $value,
290+
'items_id' => $agent->fields['computers_id'],
278291
],
279-
]);
280-
if (count($iterator) > 0) {
281-
$data = $iterator->current();
282-
283-
//retrieve computer
284-
$agents_id = $params['input']['plugin_fusioninventory_agents_id'];
285-
$agent->getFromDB($agents_id);
286-
287-
// update current field
288-
$container->updateFieldsValues(
289-
[
290-
'plugin_fields_containers_id' => $data['id'],
291-
$field => $value,
292-
'items_id' => $agent->fields['computers_id'],
293-
],
294-
Computer::getType(),
295-
);
296-
}
292+
Computer::getType(),
293+
);
297294
}
298295
}
299-
break;
296+
}
300297
}
301298
}
302299

inc/container.class.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public static function installUserData(Migration $migration, $version)
253253
$compcontainer->getFromDB($comptab);
254254

255255
$fields = new PluginFieldsField();
256-
$fields = $fields->find(['plugin_fields_containers_id' => $ostab]);
256+
$fieldsdata = $fields->find(['plugin_fields_containers_id' => $ostab]);
257257

258258
$classname = self::getClassname(Computer::getType(), $oscontainer->fields['name']);
259259
$osdata = new $classname();
@@ -262,7 +262,7 @@ public static function installUserData(Migration $migration, $version)
262262

263263
$fieldnames = [];
264264
//add fields to compcontainer
265-
foreach ($fields as $field) {
265+
foreach ($fieldsdata as $field) {
266266
$newname = $field['name'];
267267
$compfields = $fields->find(['plugin_fields_containers_id' => $comptab, 'name' => $newname]);
268268
if ($compfields) {
@@ -492,11 +492,14 @@ public static function getSpecificValueToDisplay($field, $values, array $options
492492
continue;
493493
}
494494
$name_type = getItemForItemtype($type);
495-
$obj .= $name_type->getTypeName(2);
496-
if ($count > $i) {
497-
$obj .= ', ';
495+
496+
if ($name_type !== false) {
497+
$obj .= $name_type->getTypeName(2);
498+
if ($count > $i) {
499+
$obj .= ', ';
500+
}
501+
$i++;
498502
}
499-
$i++;
500503
}
501504

502505
return $obj;
@@ -617,9 +620,7 @@ public function prepareInputForAdd($input)
617620
}
618621
}
619622

620-
$input['itemtypes'] = isset($input['itemtypes'])
621-
? Sanitizer::dbEscape(json_encode($input['itemtypes']))
622-
: null;
623+
$input['itemtypes'] = Sanitizer::dbEscape(json_encode($input['itemtypes']));
623624

624625
return $input;
625626
}
@@ -856,11 +857,13 @@ public function showForm($ID, $options = [])
856857
}
857858

858859
$name_type = getItemForItemtype($type);
859-
$obj .= $name_type->getTypeName(2);
860-
if ($count > $i) {
861-
$obj .= ', ';
860+
if ($name_type !== false) {
861+
$obj .= $name_type->getTypeName(2);
862+
if ($count > $i) {
863+
$obj .= ', ';
864+
}
865+
$i++;
862866
}
863-
$i++;
864867
}
865868
echo $obj;
866869
} else {
@@ -1136,7 +1139,7 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false)
11361139
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
11371140
{
11381141
$itemtypes = self::getEntries('tab', true);
1139-
if (isset($itemtypes[$item->getType()])) {
1142+
if (isset($itemtypes[$item->getType()]) && $item instanceof CommonDBTM) {
11401143
$tabs_entries = [];
11411144
$container = new self();
11421145
foreach ($itemtypes[$item->getType()] as $tab_name => $tab_label) {
@@ -1169,7 +1172,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
11691172
{
11701173
if ($withtemplate) {
11711174
//Do not display tab from template or from item created from template
1172-
return [];
1175+
return false;
11731176
}
11741177

11751178
//retrieve container for current tab
@@ -1293,7 +1296,7 @@ private function addRichTextFiles(CommonDBTM $object): void
12931296
* @param int $items_id item id
12941297
* @param string $itemtype item type
12951298
* @param array $data values send by update form
1296-
* @param array $old_values old values, if empty -> values add
1299+
* @param object $field_obj field object
12971300
* @return void
12981301
*/
12991302
public static function constructHistory(
@@ -1513,10 +1516,9 @@ public static function validateValues($data, $itemtype, $massiveaction)
15131516
if (
15141517
$field['mandatory'] == 1
15151518
&& (
1516-
$value === null
1519+
$value === null
15171520
|| $value === ''
15181521
|| (($field['type'] === 'dropdown' || preg_match('/^dropdown-.+/i', $field['type'])) && $value == 0)
1519-
|| ($field['type'] === 'glpi_item' && $value === null)
15201522
|| (in_array($field['type'], ['date', 'datetime']) && $value == 'NULL')
15211523
)
15221524
) {
@@ -1617,7 +1619,8 @@ public static function postItemAdd(CommonDBTM $item)
16171619
return true;
16181620
}
16191621

1620-
return $item->input = [];
1622+
$item->input = [];
1623+
return $item;
16211624
}
16221625

16231626
return true;
@@ -1647,7 +1650,7 @@ public static function preItemUpdate(CommonDBTM $item)
16471650
return true;
16481651
}
16491652

1650-
return $item->input = [];
1653+
return false;
16511654
}
16521655

16531656
return true;
@@ -1693,10 +1696,10 @@ public static function preItem(CommonDBTM $item)
16931696
if (isset($_SESSION['glpiactiveprofile']['id']) && $_SESSION['glpiactiveprofile']['id'] != null && $c_id > 0) {
16941697
$right = PluginFieldsProfile::getRightOnContainer($_SESSION['glpiactiveprofile']['id'], $c_id);
16951698
if (($right > READ) === false) {
1696-
return;
1699+
return false;
16971700
}
16981701
} else {
1699-
return;
1702+
return false;
17001703
}
17011704

17021705

@@ -1708,7 +1711,7 @@ public static function preItem(CommonDBTM $item)
17081711

17091712
//workaround: when a ticket is created from readdonly profile,
17101713
//it is not initialized; see https://github.com/glpi-project/glpi/issues/1438
1711-
if (!isset($item->fields) || count($item->fields) == 0) {
1714+
if (!empty($item->fields)) {
17121715
$item->fields = $item->input;
17131716
}
17141717

@@ -1720,14 +1723,14 @@ public static function preItem(CommonDBTM $item)
17201723
if (self::validateValues($data, $item::getType(), isset($_REQUEST['massiveaction'])) === false) {
17211724
$item->input = [];
17221725

1723-
return [];
1726+
return false;
17241727
}
17251728
$item->input['_plugin_fields_data'] = $data;
17261729

1727-
return $data;
1730+
return true;
17281731
}
17291732

1730-
return;
1733+
return false;
17311734
}
17321735

17331736
/**

inc/containerdisplaycondition.class.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
use Glpi\Application\View\TemplateRenderer;
3232
use Glpi\Toolbox\Sanitizer;
33+
use GlpiPlugin\Scim\Controller\Common;
3334

3435
class PluginFieldsContainerDisplayCondition extends CommonDBChild
3536
{
@@ -156,6 +157,9 @@ public static function getTypeName($nb = 0)
156157

157158
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
158159
{
160+
if (!($item instanceof CommonDBTM)) {
161+
return '';
162+
}
159163
return self::createTabEntry(
160164
self::getTypeName(Session::getPluralNumber()),
161165
countElementsInTable(self::getTable(), ['plugin_fields_containers_id' => $item->getID()]),
@@ -479,8 +483,10 @@ public function checkCondition($item)
479483
public static function checkRegex($regex)
480484
{
481485
// Avoid php notice when validating the regular expression
482-
set_error_handler(function ($errno, $errstr, $errfile, $errline) {});
483-
$isValid = !(preg_match($regex, null) === false);
486+
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
487+
return true;
488+
});
489+
$isValid = !(preg_match($regex, '') === false);
484490
restore_error_handler();
485491

486492
return $isValid;
@@ -520,6 +526,9 @@ public function prepareInputForUpdate($input)
520526

521527
public static function showForTabContainer(CommonGLPI $item, $options = [])
522528
{
529+
if (!$item instanceof CommonDBTM) {
530+
return;
531+
}
523532
$displayCondition_id = $options['displaycondition_id'] ?? 0;
524533
$display_condition = null;
525534

0 commit comments

Comments
 (0)