Skip to content

Commit

Permalink
PHPStan: fix baseline issues for ajax/* files
Browse files Browse the repository at this point in the history
* One supported item is a CommonGLPI (Planning)
* Remove nested variable; do not output empty content
* Dropdown::getDropdownName() can also return an array
* Remove forced types that are already validated by instanceOf
* Avoid false positive by declaring all possible keys
* Use try/catch; Add never return type; Remove exit
* Add missing parenthesis
* Remove always true condition
  • Loading branch information
AdrienClairembault authored Sep 4, 2024
1 parent 0922c24 commit c74bad8
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 193 deletions.
162 changes: 0 additions & 162 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,6 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
$ignoreErrors[] = [
// identifier: identical.alwaysFalse
'message' => '#^Strict comparison using \\=\\=\\= between class\\-string\\<CommonDBTM\\> and \'Planning\' will always evaluate to false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/central.php',
];
$ignoreErrors[] = [
// identifier: function.impossibleType
'message' => '#^Call to function is_array\\(\\) with string will always evaluate to false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/comments.php',
];
$ignoreErrors[] = [
// identifier: isset.offset
'message' => '#^Offset \'comment\' on \\*NEVER\\* in isset\\(\\) always exists and is not nullable\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/comments.php',
];
$ignoreErrors[] = [
// identifier: nullCoalesce.offset
'message' => '#^Offset \'comment\' on array\\{comment\\: string\\} on left side of \\?\\? always exists and is not nullable\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/comments.php',
];
$ignoreErrors[] = [
// identifier: booleanAnd.alwaysFalse
'message' => '#^Result of && is always false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/comments.php',
];
$ignoreErrors[] = [
// identifier: booleanOr.alwaysFalse
'message' => '#^Result of \\|\\| is always false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/commonitilobject_item.php',
];
$ignoreErrors[] = [
// identifier: booleanAnd.rightAlwaysTrue
'message' => '#^Right side of && is always true\\.$#',
'count' => 2,
'path' => __DIR__ . '/ajax/dropdownItilActors.php',
];
$ignoreErrors[] = [
// identifier: booleanNot.alwaysFalse
'message' => '#^Negated boolean expression is always false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/form/answer.php',
];
$ignoreErrors[] = [
// identifier: greater.alwaysFalse
'message' => '#^Comparison operation "\\>" between 0 and 0 is always false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/itilfollowup.php',
];
$ignoreErrors[] = [
// identifier: booleanNot.alwaysTrue
'message' => '#^Negated boolean expression is always true\\.$#',
'count' => 2,
'path' => __DIR__ . '/ajax/searchoptionvalue.php',
];
$ignoreErrors[] = [
// identifier: greater.alwaysFalse
'message' => '#^Comparison operation "\\>" between 0 and 0 is always false\\.$#',
'count' => 1,
'path' => __DIR__ . '/ajax/task.php',
];
$ignoreErrors[] = [
// identifier: classConstant.nonObject
'message' => '#^Cannot access constant class on object\\|false\\.$#',
Expand Down Expand Up @@ -835,18 +769,6 @@
'count' => 4,
'path' => __DIR__ . '/src/CommonDBTM.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/CommonDBTM.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/CommonDBTM.php',
];
$ignoreErrors[] = [
// identifier: assign.propertyType
'message' => '#^Property CommonDBTM\\:\\:\\$right \\(int\\) does not accept null\\.$#',
Expand Down Expand Up @@ -913,18 +835,6 @@
'count' => 1,
'path' => __DIR__ . '/src/CommonDBVisible.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 3,
'path' => __DIR__ . '/src/CommonDBVisible.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 3,
'path' => __DIR__ . '/src/CommonDBVisible.php',
];
$ignoreErrors[] = [
// identifier: instanceof.alwaysFalse
'message' => '#^Instanceof between \\$this\\(CommonDropdown\\) and IPAddress will always evaluate to false\\.$#',
Expand Down Expand Up @@ -1315,18 +1225,6 @@
'count' => 1,
'path' => __DIR__ . '/src/CommonItilObject_Item.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/CommonItilObject_Item.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/CommonItilObject_Item.php',
];
$ignoreErrors[] = [
// identifier: phpDoc.parseError
'message' => '#^PHPDoc tag @param has invalid value \\(integer type \\$obj_id ITIL object on which the used item are attached\\)\\: Unexpected token "type", expected variable at offset 76$#',
Expand Down Expand Up @@ -1435,18 +1333,6 @@
'count' => 1,
'path' => __DIR__ . '/src/ContractCost.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Contract_Item.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Contract_Item.php',
];
$ignoreErrors[] = [
// identifier: parameter.defaultValue
'message' => '#^Default value of the parameter \\#4 \\$options \\(array\\{\\}\\) of method CronTask\\:\\:register\\(\\) is incompatible with type array\\{state\\: 0\\|1\\|2, mode\\: 1\\|2, allowmode\\: int, hourmin\\: int, hourmax\\: int, logs_lifetime\\: int, param\\: int, comment\\: string\\}\\.$#',
Expand Down Expand Up @@ -1903,12 +1789,6 @@
'count' => 3,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: return.type
'message' => '#^Method Dropdown\\:\\:getDropdownName\\(\\) should return string but returns array\\<string, mixed\\>\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: return.empty
'message' => '#^Method Dropdown\\:\\:getDropdownUsers\\(\\) should return array\\|string but empty return statement found\\.$#',
Expand All @@ -1933,12 +1813,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: isset.offset
'message' => '#^Offset \'max\' on array in isset\\(\\) always exists and is not nullable\\.$#',
Expand All @@ -1951,12 +1825,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 2,
'path' => __DIR__ . '/src/Dropdown.php',
];
$ignoreErrors[] = [
// identifier: booleanOr.rightAlwaysFalse
'message' => '#^Right side of \\|\\| is always false\\.$#',
Expand Down Expand Up @@ -2455,12 +2323,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Glpi/Application/ErrorHandler.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Glpi/Application/View/Extension/ItemtypeExtension.php',
];
$ignoreErrors[] = [
// identifier: parameter.defaultValue
'message' => '#^Default value of the parameter \\#2 \\$cachedir \\(null\\) of method Glpi\\\\Application\\\\View\\\\TemplateRenderer\\:\\:__construct\\(\\) is incompatible with type string\\.$#',
Expand Down Expand Up @@ -3961,18 +3823,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Item_Devices.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Item_Devices.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Item_Devices.php',
];
$ignoreErrors[] = [
// identifier: return.void
'message' => '#^Method Item_Disk\\:\\:showForItem\\(\\) with return type void returns false but should not return anything\\.$#',
Expand Down Expand Up @@ -4603,18 +4453,6 @@
'count' => 1,
'path' => __DIR__ . '/src/NotificationTargetCommonITILObject.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'comment\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/NotificationTargetCommonITILObject.php',
];
$ignoreErrors[] = [
// identifier: offsetAccess.notFound
'message' => '#^Offset \'name\' does not exist on string\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/NotificationTargetCommonITILObject.php',
];
$ignoreErrors[] = [
// identifier: return.type
'message' => '#^Method NotificationTemplate\\:\\:getTemplateByLanguage\\(\\) should return int\\|false but returns non\\-falsy\\-string\\.$#',
Expand Down
1 change: 0 additions & 1 deletion ajax/agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
$agent = new Agent();
if (!$agent->getFromDB($_POST['id']) || !$agent->canView()) {
Response::sendError(404, 'Unable to load agent #' . $_POST['id']);
return;
}
$answer = [];

Expand Down
2 changes: 1 addition & 1 deletion ajax/central.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
die();
}

/** @var class-string<CommonDBTM> $itemtype */
/** @var class-string<CommonGLPI> $itemtype */
$itemtype = $_REQUEST['itemtype'];
$params = $_REQUEST['params'];

Expand Down
9 changes: 1 addition & 8 deletions ajax/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@
break;

case Group::getType():
$tmpname = [
'comment' => "",
];
if ($_POST['value'] != 0) {
$group = new \Group();
if (!is_array($_POST["value"]) && $group->getFromDB($_POST['value']) && $group->canView()) {
Expand All @@ -106,15 +103,11 @@
'group_name' => $group->fields['completename'],
'comment' => $group->fields['comment'],
];
$comment = TemplateRenderer::getInstance()->render('components/group/info_card.html.twig', [
TemplateRenderer::getInstance()->display('components/group/info_card.html.twig', [
'group' => $group_params,
]);
$tmpname = [
'comment' => $comment,
];
}
}
echo($tmpname["comment"] ?? '');
break;

default:
Expand Down
9 changes: 4 additions & 5 deletions ajax/commonitilobject_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@
* ---------------------------------------------------------------------
*/

/**
* @var CommonDBTM $obj
* @var CommonItilObject_Item $item_obj
*/

use Glpi\Http\Response;

header("Content-Type: text/html; charset=UTF-8");
Html::header_nocache();

Session::checkLoginUser();

// Should be defined by other files that include this file.
// See: change_item.php, item_problem.php, item_ticket.php and item_ticketrecurrent.php
$obj = $obj ?? null;
$item_obj = $item_obj ?? null;
if (!($obj instanceof CommonDBTM) || !($item_obj instanceof CommonItilObject_Item)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}
Expand Down
13 changes: 8 additions & 5 deletions ajax/dropdownItilActors.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@
}
}

$options = ['name' => '_itil_' . $_POST["actortype"] . '[users_id]',
$options = [
'name' => '_itil_' . $_POST["actortype"] . '[users_id]',
'entity' => Session::getMatchingActiveEntities($_POST['entity_restrict']),
'right' => $right,
'rand' => $rand,
'ldap_import' => true
'ldap_import' => true,
'toupdate' => null,
];

if ($CFG_GLPI["notifications_mailing"]) {
Expand All @@ -93,7 +95,7 @@
&& ($_POST["actortype"] == 'assign')
) {
$toupdate = [];
if (isset($options['toupdate']) && is_array($options['toupdate'])) {
if (is_array($options['toupdate'])) {
$toupdate[] = $options['toupdate'];
}
$toupdate[] = ['value_fieldname' => 'value',
Expand Down Expand Up @@ -179,7 +181,8 @@
case "supplier":
$options = ['name' => '_itil_' . $_POST["actortype"] . '[suppliers_id]',
'entity' => Session::getMatchingActiveEntities($_POST['entity_restrict']),
'rand' => $rand
'rand' => $rand,
'to_update' => null,
];
if ($CFG_GLPI["notifications_mailing"]) {
$paramscomment = ['value' => '__VALUE__',
Expand All @@ -201,7 +204,7 @@
}
if ($_POST["itemtype"] == 'Ticket') {
$toupdate = [];
if (isset($options['toupdate']) && is_array($options['toupdate'])) {
if (is_array($options['toupdate'])) {
$toupdate[] = $options['toupdate'];
}
$toupdate[] = ['value_fieldname' => 'value',
Expand Down
1 change: 0 additions & 1 deletion ajax/dropdownTrackingDeviceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
// Check for required params
if (empty($itemtype)) {
Response::sendError(400, "Bad request: itemtype cannot be empty", Response::CONTENT_TYPE_TEXT_HTML);
die;
}

// Check if itemtype is valid in the given context
Expand Down
5 changes: 3 additions & 2 deletions ajax/form/answer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@

// Try to save answers
$handler = AnswersHandler::getInstance();
$answers_set = $handler->saveAnswers($form, $answers, Session::getLoginUserID());
if (!$answers_set) {
try {
$answers_set = $handler->saveAnswers($form, $answers, Session::getLoginUserID());
} catch (\Throwable $e) {
Response::sendError(500, __('Failed to save answers'));
}

Expand Down
1 change: 0 additions & 1 deletion ajax/impact.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@

default:
Response::sendError(400, "Missing or invalid 'action' parameter");
break;
}
break;

Expand Down
2 changes: 1 addition & 1 deletion ajax/itilfollowup.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
}
}

if ($template->fields['pendingreasons_id'] ?? 0 > 0) {
if (($template->fields['pendingreasons_id'] ?? 0) > 0) {
$pendingReason = new PendingReason();
if ($pendingReason->getFromDB($template->fields['pendingreasons_id'])) {
$template->fields = array_merge($template->fields, [
Expand Down
Loading

0 comments on commit c74bad8

Please sign in to comment.