Skip to content

Commit

Permalink
Implement Common::getRequest() to remove global
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela committed Jan 27, 2022
1 parent adc30f6 commit ef6dbe0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

require AUTOLOAD_FILE;

global $route, $containerBuilder, $request;
global $route, $containerBuilder;

Common::run();

$dispatcher = Routing::getDispatcher();
Routing::callControllerForRoute($request, $route, $dispatcher, $containerBuilder);
Routing::callControllerForRoute(Common::getRequest(), $route, $dispatcher, $containerBuilder);
16 changes: 14 additions & 2 deletions libraries/classes/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

final class Common
{
/** @var ServerRequest|null */
private static $request = null;

/**
* Misc stuff and REQUIRED by ALL the scripts.
* MUST be included by every script
Expand Down Expand Up @@ -77,11 +80,11 @@ final class Common
*/
public static function run(): void
{
global $containerBuilder, $errorHandler, $config, $server, $dbi, $request;
global $containerBuilder, $errorHandler, $config, $server, $dbi;
global $lang, $cfg, $isConfigLoading, $auth_plugin, $route, $theme;
global $urlParams, $isMinimumCommon, $sql_query, $token_mismatch;

$request = ServerRequestFactory::createFromGlobals();
$request = self::getRequest();

$route = Routing::getCurrentRoute();

Expand Down Expand Up @@ -617,4 +620,13 @@ private static function connectToDatabaseServer(DatabaseInterface $dbi, Authenti
*/
$dbi->connect(DatabaseInterface::CONNECT_USER, null, DatabaseInterface::CONNECT_CONTROL);
}

public static function getRequest(): ServerRequest
{
if (self::$request === null) {
self::$request = ServerRequestFactory::createFromGlobals();
}

return self::$request;
}
}
5 changes: 2 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
<files psalm-version="4.18.1@dda05fa913f4dc6eb3386f2f7ce5a45d37a71bcb">
<file src="index.php">
<InvalidGlobal occurrences="1">
<code>global $route, $containerBuilder, $request;</code>
<code>global $route, $containerBuilder;</code>
</InvalidGlobal>
<MixedArgument occurrences="2">
<code>$request</code>
<MixedArgument occurrences="1">
<code>$route</code>
</MixedArgument>
</file>
Expand Down

0 comments on commit ef6dbe0

Please sign in to comment.