Skip to content

Commit

Permalink
Fix handling of GLPI dynamic constants in PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Dec 10, 2024
1 parent 98176e5 commit 4a9ea24
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 103 deletions.
100 changes: 2 additions & 98 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,6 @@
'count' => 2,
'path' => __DIR__ . '/front/report.infocom.php',
];
$ignoreErrors[] = [
'message' => '#^Negated boolean expression is always true\\.$#',
'identifier' => 'booleanNot.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/front/user.form.php',
];
$ignoreErrors[] = [
'message' => '#^Call to function is_array\\(\\) with string will always evaluate to false\\.$#',
'identifier' => 'function.impossibleType',
Expand Down Expand Up @@ -217,12 +211,6 @@
'count' => 1,
'path' => __DIR__ . '/inc/db.function.php',
];
$ignoreErrors[] = [
'message' => '#^Left side of && is always true\\.$#',
'identifier' => 'booleanAnd.leftAlwaysTrue',
'count' => 1,
'path' => __DIR__ . '/inc/includes.php',
];
$ignoreErrors[] = [
'message' => '#^If condition is always false\\.$#',
'identifier' => 'if.alwaysFalse',
Expand Down Expand Up @@ -751,12 +739,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Auth.php',
];
$ignoreErrors[] = [
'message' => '#^If condition is always false\\.$#',
'identifier' => 'if.alwaysFalse',
'count' => 2,
'path' => __DIR__ . '/src/Auth.php',
];
$ignoreErrors[] = [
'message' => '#^Loose comparison using \\=\\= between \'external\' and \'external\' will always evaluate to true\\.$#',
'identifier' => 'equal.alwaysTrue',
Expand Down Expand Up @@ -1123,12 +1105,6 @@
'count' => 1,
'path' => __DIR__ . '/src/CartridgeItem.php',
];
$ignoreErrors[] = [
'message' => '#^If condition is always true\\.$#',
'identifier' => 'if.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Central.php',
];
$ignoreErrors[] = [
'message' => '#^Method Central\\:\\:getTabNameForItem\\(\\) should return string but returns array\\<int, string\\>\\.$#',
'identifier' => 'return.type',
Expand Down Expand Up @@ -1783,18 +1759,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Config.php',
];
$ignoreErrors[] = [
'message' => '#^Negated boolean expression is always true\\.$#',
'identifier' => 'booleanNot.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Config.php',
];
$ignoreErrors[] = [
'message' => '#^Result of \\|\\| is always true\\.$#',
'identifier' => 'booleanOr.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Config.php',
];
$ignoreErrors[] = [
'message' => '#^Call to function property_exists\\(\\) with \\$this\\(Glpi\\\\Console\\\\AbstractCommand\\) and \'db\' will always evaluate to true\\.$#',
'identifier' => 'function.alreadyNarrowedType',
Expand Down Expand Up @@ -1975,12 +1939,6 @@
'count' => 5,
'path' => __DIR__ . '/src/DBConnection.php',
];
$ignoreErrors[] = [
'message' => '#^Path in include_once\\(\\) "/var/www/glpi/config/config_db_slave\\.php" is not a file or it does not exist\\.$#',
'identifier' => 'includeOnce.fileNotFound',
'count' => 4,
'path' => __DIR__ . '/src/DBConnection.php',
];
$ignoreErrors[] = [
'message' => '#^Unreachable statement \\- code above always terminates\\.$#',
'identifier' => 'deadCode.unreachable',
Expand Down Expand Up @@ -2623,18 +2581,6 @@
'count' => 1,
'path' => __DIR__ . '/src/GLPIKey.php',
];
$ignoreErrors[] = [
'message' => '#^Cannot cast list\\<string\\>\\|null to string\\.$#',
'identifier' => 'cast.string',
'count' => 1,
'path' => __DIR__ . '/src/GLPINetwork.php',
];
$ignoreErrors[] = [
'message' => '#^Expression in empty\\(\\) is always falsy\\.$#',
'identifier' => 'empty.expr',
'count' => 1,
'path' => __DIR__ . '/src/GLPINetwork.php',
];
$ignoreErrors[] = [
'message' => '#^Expression on left side of \\?\\? is not nullable\\.$#',
'identifier' => 'nullCoalesce.expr',
Expand Down Expand Up @@ -2725,12 +2671,6 @@
'count' => 3,
'path' => __DIR__ . '/src/Html.php',
];
$ignoreErrors[] = [
'message' => '#^If condition is always true\\.$#',
'identifier' => 'if.alwaysTrue',
'count' => 2,
'path' => __DIR__ . '/src/Html.php',
];
$ignoreErrors[] = [
'message' => '#^Instanceof between Glpi\\\\Console\\\\Application and Glpi\\\\Console\\\\Application will always evaluate to true\\.$#',
'identifier' => 'instanceof.alwaysTrue',
Expand Down Expand Up @@ -2782,7 +2722,7 @@
$ignoreErrors[] = [
'message' => '#^Negated boolean expression is always true\\.$#',
'identifier' => 'booleanNot.alwaysTrue',
'count' => 2,
'count' => 1,
'path' => __DIR__ . '/src/Html.php',
];
$ignoreErrors[] = [
Expand Down Expand Up @@ -3745,12 +3685,6 @@
'count' => 1,
'path' => __DIR__ . '/src/ManualLink.php',
];
$ignoreErrors[] = [
'message' => '#^Left side of && is always true\\.$#',
'identifier' => 'booleanAnd.leftAlwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Marketplace/Controller.php',
];
$ignoreErrors[] = [
'message' => '#^Strict comparison using \\!\\=\\= between int and false will always evaluate to true\\.$#',
'identifier' => 'notIdentical.alwaysTrue',
Expand All @@ -3763,18 +3697,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Marketplace/View.php',
];
$ignoreErrors[] = [
'message' => '#^Right side of && is always true\\.$#',
'identifier' => 'booleanAnd.rightAlwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Marketplace/View.php',
];
$ignoreErrors[] = [
'message' => '#^Strict comparison using \\!\\=\\= between \'GIT\' and \'CLOUD\' will always evaluate to true\\.$#',
'identifier' => 'notIdentical.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Marketplace/View.php',
];
$ignoreErrors[] = [
'message' => '#^Call to function is_array\\(\\) with array\\<int\\|string, mixed\\> will always evaluate to true\\.$#',
'identifier' => 'function.alreadyNarrowedType',
Expand Down Expand Up @@ -5083,12 +5005,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Session.php',
];
$ignoreErrors[] = [
'message' => '#^Left side of && is always true\\.$#',
'identifier' => 'booleanAnd.leftAlwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Session.php',
];
$ignoreErrors[] = [
'message' => '#^Method Session\\:\\:loadLanguage\\(\\) with return type void returns mixed but should not return anything\\.$#',
'identifier' => 'return.void',
Expand Down Expand Up @@ -5395,12 +5311,6 @@
'count' => 2,
'path' => __DIR__ . '/src/Toolbox.php',
];
$ignoreErrors[] = [
'message' => '#^Negated boolean expression is always true\\.$#',
'identifier' => 'booleanNot.alwaysTrue',
'count' => 1,
'path' => __DIR__ . '/src/Toolbox.php',
];
$ignoreErrors[] = [
'message' => '#^Offset \'function\' on array\\{function\\: string, line\\?\\: int, file\\?\\: string, class\\?\\: class\\-string, type\\?\\: \'\\-\\>\'\\|\'\\:\\:\', args\\?\\: array\\<mixed\\>, object\\?\\: object\\} in isset\\(\\) always exists and is not nullable\\.$#',
'identifier' => 'isset.offset',
Expand Down Expand Up @@ -5512,7 +5422,7 @@
$ignoreErrors[] = [
'message' => '#^Negated boolean expression is always true\\.$#',
'identifier' => 'booleanNot.alwaysTrue',
'count' => 5,
'count' => 1,
'path' => __DIR__ . '/src/User.php',
];
$ignoreErrors[] = [
Expand All @@ -5527,12 +5437,6 @@
'count' => 1,
'path' => __DIR__ . '/src/User.php',
];
$ignoreErrors[] = [
'message' => '#^Right side of && is always false\\.$#',
'identifier' => 'booleanAnd.rightAlwaysFalse',
'count' => 1,
'path' => __DIR__ . '/src/User.php',
];
$ignoreErrors[] = [
'message' => '#^Unreachable statement \\- code above always terminates\\.$#',
'identifier' => 'deadCode.unreachable',
Expand Down
51 changes: 49 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,55 @@ parameters:
- src
universalObjectCratesClasses:
- Sabre\VObject\Node
stubFiles:
- stubs/glpi_constants.php
dynamicConstantNames:
- GLPI_AJAX_DASHBOARD
- GLPI_ALLOW_IFRAME_IN_RICH_TEXT
- GLPI_CACHE_DIR
- GLPI_CALDAV_IMPORT_STATE
- GLPI_CENTRAL_WARNINGS
- GLPI_CONFIG_DIR
- GLPI_CRON_DIR
- GLPI_CSRF_EXPIRES
- GLPI_CSRF_MAX_TOKENS
- GLPI_DEMO_MODE
- GLPI_DISABLE_ONLY_FULL_GROUP_BY_SQL_MODE
- GLPI_DOC_DIR
- GLPI_DUMP_DIR
- GLPI_FORCE_MAIL
- GLPI_GRAPH_DIR
- GLPI_IDOR_EXPIRES
- GLPI_INSTALL_MODE
- GLPI_INVENTORY_DIR
- GLPI_LOCAL_I18N_DIR
- GLPI_LOCK_DIR
- GLPI_LOG_DIR
- GLPI_LOG_LVL
- GLPI_MARKETPLACE_ALLOW_OVERRIDE
- GLPI_MARKETPLACE_DIR
- GLPI_MARKETPLACE_ENABLE
- GLPI_MARKETPLACE_MANUAL_DOWNLOADS
- GLPI_MARKETPLACE_PLUGINS_API_URI
- GLPI_MARKETPLACE_PRERELEASES
- GLPI_NETWORK_REGISTRATION_API_URL
- GLPI_NETWORK_MAIL
- GLPI_NETWORK_SERVICES
- GLPI_PICTURE_DIR
- GLPI_PLUGIN_DOC_DIR
- GLPI_RSS_DIR
- GLPI_SERVERSIDE_URL_ALLOWLIST
- GLPI_SESSION_DIR
- GLPI_SQL_DEBUG
- GLPI_STRICT_DEPRECATED
- GLPI_TELEMETRY_URI
- GLPI_TEXT_MAXSIZE
- GLPI_TMP_DIR
- GLPI_UPLOAD_DIR
- GLPI_USE_CSRF_CHECK
- GLPI_USE_IDOR_CHECK
- GLPI_USER_AGENT_EXTRA_COMMENTS
- GLPI_VAR_DIR
- PLUGINS_DIRECTORIES
- TU_USER
ignoreErrors:
- '/Call to static method \w+\(\) on an unknown class phpCAS/'
- '/Class phpCAS not found/'
Expand Down
6 changes: 3 additions & 3 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ public static function getNewCSRFToken(bool $standalone = false)
if (!isset($_SESSION['glpicsrftokens'])) {
$_SESSION['glpicsrftokens'] = [];
}
$_SESSION['glpicsrftokens'][$token] = time() + GLPI_CSRF_EXPIRES;
$_SESSION['glpicsrftokens'][$token] = time() + (int) GLPI_CSRF_EXPIRES;

if (!$standalone) {
$CURRENTCSRFTOKEN = $token;
Expand Down Expand Up @@ -1601,7 +1601,7 @@ public static function cleanCSRFTokens()
unset($_SESSION['glpicsrftokens'][$token]);
}
}
$overflow = count($_SESSION['glpicsrftokens']) - GLPI_CSRF_MAX_TOKENS;
$overflow = count($_SESSION['glpicsrftokens']) - (int) GLPI_CSRF_MAX_TOKENS;
if ($overflow > 0) {
$_SESSION['glpicsrftokens'] = array_slice(
$_SESSION['glpicsrftokens'],
Expand Down Expand Up @@ -1718,7 +1718,7 @@ public static function getNewIDORToken(string $itemtype = "", array $add_params
}

$_SESSION['glpiidortokens'][$token] = [
'expires' => time() + GLPI_IDOR_EXPIRES
'expires' => time() + (int) GLPI_IDOR_EXPIRES
] + ($itemtype !== "" ? ['itemtype' => $itemtype] : [])
+ $add_params;

Expand Down

0 comments on commit 4a9ea24

Please sign in to comment.