From ca064b75d275fdd3dc5f93788a37d886d504210a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 22 Dec 2019 10:37:07 +0100 Subject: [PATCH] remove stupid initializations and optimize types --- Autoloader.php | 2 +- DataStorage/Cache/CachePool.php | 2 +- .../Connection/ConnectionAbstract.php | 16 ++++----- .../Database/Connection/MysqlConnection.php | 2 +- .../Connection/PostgresConnection.php | 2 +- .../Database/Connection/SQLiteConnection.php | 2 +- .../Connection/SqlServerConnection.php | 2 +- DataStorage/Database/DataMapperAbstract.php | 1 - DataStorage/Database/GrammarAbstract.php | 6 +++- DataStorage/Session/ConsoleSession.php | 16 ++++----- DataStorage/Session/HttpSession.php | 17 +++++----- DataStorage/Session/SessionInterface.php | 24 ++++++------- Dispatcher/Dispatcher.php | 2 +- Localization/L11nManager.php | 2 +- Log/FileLogger.php | 8 ++--- Message/Console/Request.php | 28 ++++----------- Message/Http/Request.php | 34 +++++++------------ Module/ModuleManager.php | 2 +- 18 files changed, 72 insertions(+), 96 deletions(-) diff --git a/Autoloader.php b/Autoloader.php index b55e9dd88..a54b626f1 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -106,7 +106,7 @@ final class Autoloader $class = \str_replace(['_', '\\'], '/', $class); foreach (self::$paths as $path) { - if (\file_exists($file = $path . $class . '.php')) { + if (\file_exists($path . $class . '.php')) { return true; } } diff --git a/DataStorage/Cache/CachePool.php b/DataStorage/Cache/CachePool.php index f6e2fc67b..399c0e65b 100644 --- a/DataStorage/Cache/CachePool.php +++ b/DataStorage/Cache/CachePool.php @@ -37,7 +37,7 @@ final class CachePool implements DataStoragePoolInterface * @var DataStorageConnectionInterface[] * @since 1.0.0 */ - private ?array $pool = null; + private array $pool = []; /** * Add database. diff --git a/DataStorage/Database/Connection/ConnectionAbstract.php b/DataStorage/Database/Connection/ConnectionAbstract.php index 52dc28565..e501dc5ab 100644 --- a/DataStorage/Database/Connection/ConnectionAbstract.php +++ b/DataStorage/Database/Connection/ConnectionAbstract.php @@ -55,10 +55,10 @@ abstract class ConnectionAbstract implements ConnectionInterface /** * Database data. * - * @var null|string[] + * @var array * @since 1.0.0 */ - protected ?array $dbdata = null; + protected array $dbdata = []; /** * Database type. @@ -79,18 +79,18 @@ abstract class ConnectionAbstract implements ConnectionInterface /** * Database grammar. * - * @var null|Grammar + * @var Grammar * @since 1.0.0 */ - protected ?Grammar $grammar = null; + protected Grammar $grammar; /** * Database grammar. * - * @var null|SchemaGrammar + * @var SchemaGrammar * @since 1.0.0 */ - protected ?SchemaGrammar $schemaGrammar = null; + protected SchemaGrammar $schemaGrammar; /** * {@inheritdoc} @@ -161,7 +161,7 @@ abstract class ConnectionAbstract implements ConnectionInterface */ public function getGrammar() : Grammar { - return $this->grammar ?? new Grammar(); + return $this->grammar; } /** @@ -169,7 +169,7 @@ abstract class ConnectionAbstract implements ConnectionInterface */ public function getSchemaGrammar() : SchemaGrammar { - return $this->schemaGrammar ?? new SchemaGrammar(); + return $this->schemaGrammar; } /** diff --git a/DataStorage/Database/Connection/MysqlConnection.php b/DataStorage/Database/Connection/MysqlConnection.php index 26fda8287..2821283d0 100644 --- a/DataStorage/Database/Connection/MysqlConnection.php +++ b/DataStorage/Database/Connection/MysqlConnection.php @@ -55,7 +55,7 @@ final class MysqlConnection extends ConnectionAbstract */ public function connect(array $dbdata = null) : void { - $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->dbdata = $dbdata ?? $this->dbdata; if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password']) || !DatabaseType::isValidValue($this->dbdata['db']) diff --git a/DataStorage/Database/Connection/PostgresConnection.php b/DataStorage/Database/Connection/PostgresConnection.php index 97c8ef99b..fc16d6827 100644 --- a/DataStorage/Database/Connection/PostgresConnection.php +++ b/DataStorage/Database/Connection/PostgresConnection.php @@ -55,7 +55,7 @@ final class PostgresConnection extends ConnectionAbstract */ public function connect(array $dbdata = null) : void { - $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->dbdata = $dbdata ?? $this->dbdata; if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password']) || !DatabaseType::isValidValue($this->dbdata['db']) diff --git a/DataStorage/Database/Connection/SQLiteConnection.php b/DataStorage/Database/Connection/SQLiteConnection.php index 5414ba3e6..305e0ab3f 100644 --- a/DataStorage/Database/Connection/SQLiteConnection.php +++ b/DataStorage/Database/Connection/SQLiteConnection.php @@ -55,7 +55,7 @@ final class SQLiteConnection extends ConnectionAbstract */ public function connect(array $dbdata = null) : void { - $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->dbdata = $dbdata ?? $this->dbdata; if (!isset($this->dbdata['db'], $this->dbdata['database']) || !DatabaseType::isValidValue($this->dbdata['db']) diff --git a/DataStorage/Database/Connection/SqlServerConnection.php b/DataStorage/Database/Connection/SqlServerConnection.php index 37742452e..ef35f1873 100644 --- a/DataStorage/Database/Connection/SqlServerConnection.php +++ b/DataStorage/Database/Connection/SqlServerConnection.php @@ -57,7 +57,7 @@ final class SqlServerConnection extends ConnectionAbstract { $this->close(); - $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; + $this->dbdata = $dbdata ?? $this->dbdata; $this->prefix = $dbdata['prefix']; if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password']) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 3cf043b5c..719db573f 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -19,7 +19,6 @@ use phpOMS\DataStorage\Database\Exception\InvalidMapperException; use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\Query\QueryType; use phpOMS\DataStorage\DataMapperInterface; -use phpOMS\Message\RequestAbstract; use phpOMS\Utils\ArrayUtils; use Throwable; diff --git a/DataStorage/Database/GrammarAbstract.php b/DataStorage/Database/GrammarAbstract.php index 16ead9d1a..20c9dd010 100644 --- a/DataStorage/Database/GrammarAbstract.php +++ b/DataStorage/Database/GrammarAbstract.php @@ -254,7 +254,11 @@ abstract class GrammarAbstract $fullSystem = ''; foreach ($split as $key => $system) { - $fullSystem .= '.' . ($system !== '*' ? $identifier : '') . ($key === 0 && $system !== '*' ? $prefix : '') . $system . ($system !== '*' ? $identifier : ''); + $fullSystem .= '.' + . ($system !== '*' ? $identifier : '') + . ($key === 0 && $system !== '*' ? $prefix : '') + . $system + . ($system !== '*' ? $identifier : ''); } return \ltrim($fullSystem, '.'); diff --git a/DataStorage/Session/ConsoleSession.php b/DataStorage/Session/ConsoleSession.php index 39aae541e..ecaad2a98 100644 --- a/DataStorage/Session/ConsoleSession.php +++ b/DataStorage/Session/ConsoleSession.php @@ -15,8 +15,6 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Session; use phpOMS\DataStorage\LockException; -use phpOMS\Uri\UriFactory; -use phpOMS\Utils\RnG\StringUtils; /** * Console session class. @@ -49,10 +47,10 @@ class ConsoleSession implements SessionInterface /** * Session ID. * - * @var null|int|string + * @var string * @since 1.0.0 */ - private $sid = null; + private string $sid; /** * Inactivity Interval. @@ -103,7 +101,7 @@ class ConsoleSession implements SessionInterface /** * {@inheritdoc} */ - public function set($key, $value, bool $overwrite = false) : bool + public function set(string $key, $value, bool $overwrite = false) : bool { if (!$this->isLocked && ($overwrite || !isset($this->sessionData[$key]))) { $this->sessionData[$key] = $value; @@ -117,7 +115,7 @@ class ConsoleSession implements SessionInterface /** * {@inheritdoc} */ - public function get($key) + public function get(string $key) { return $this->sessionData[$key] ?? null; } @@ -156,7 +154,7 @@ class ConsoleSession implements SessionInterface /** * {@inheritdoc} */ - public function remove($key) : bool + public function remove(string $key) : bool { if (!$this->isLocked && isset($this->sessionData[$key])) { unset($this->sessionData[$key]); @@ -170,7 +168,7 @@ class ConsoleSession implements SessionInterface /** * {@inheritdoc} */ - public function getSID() + public function getSID() : string { return $this->sid; } @@ -178,7 +176,7 @@ class ConsoleSession implements SessionInterface /** * {@inheritdoc} */ - public function setSID($sid) : void + public function setSID(string $sid) : void { $this->sid = $sid; } diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index 9ea461017..11206b33a 100644 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -16,7 +16,6 @@ namespace phpOMS\DataStorage\Session; use phpOMS\DataStorage\LockException; use phpOMS\Uri\UriFactory; -use phpOMS\Utils\RnG\StringUtils; /** * Http session class. @@ -49,10 +48,10 @@ final class HttpSession implements SessionInterface /** * Session ID. * - * @var null|int|string + * @var string * @since 1.0.0 */ - private $sid = null; + private string $sid; /** * Inactivity Interval. @@ -114,7 +113,7 @@ final class HttpSession implements SessionInterface $this->set('UID', 0, false); if (($csrf = $this->get('CSRF')) === null) { - $csrf = StringUtils::generateString(10, 16); + $csrf = \bin2hex(\random_bytes(32)); $this->set('CSRF', $csrf, false); } @@ -124,7 +123,7 @@ final class HttpSession implements SessionInterface /** * {@inheritdoc} */ - public function set($key, $value, bool $overwrite = false) : bool + public function set(string $key, $value, bool $overwrite = false) : bool { if (!$this->isLocked && ($overwrite || !isset($this->sessionData[$key]))) { $this->sessionData[$key] = $value; @@ -138,7 +137,7 @@ final class HttpSession implements SessionInterface /** * {@inheritdoc} */ - public function get($key) + public function get(string $key) { return $this->sessionData[$key] ?? null; } @@ -177,7 +176,7 @@ final class HttpSession implements SessionInterface /** * {@inheritdoc} */ - public function remove($key) : bool + public function remove(string $key) : bool { if (!$this->isLocked && isset($this->sessionData[$key])) { unset($this->sessionData[$key]); @@ -191,7 +190,7 @@ final class HttpSession implements SessionInterface /** * {@inheritdoc} */ - public function getSID() + public function getSID() : string { return $this->sid; } @@ -199,7 +198,7 @@ final class HttpSession implements SessionInterface /** * {@inheritdoc} */ - public function setSID($sid) : void + public function setSID(string $sid) : void { $this->sid = $sid; } diff --git a/DataStorage/Session/SessionInterface.php b/DataStorage/Session/SessionInterface.php index 01fae4e6e..8559d4e82 100644 --- a/DataStorage/Session/SessionInterface.php +++ b/DataStorage/Session/SessionInterface.php @@ -30,37 +30,37 @@ interface SessionInterface /** * Get session variable by key. * - * @param int|string $key Value key + * @param string $key Value key * * @return mixed * * @since 1.0.0 */ - public function get($key); + public function get(string $key); /** * Store session value by key. * - * @param int|string $key Value key - * @param mixed $value Value to store - * @param bool $overwrite Overwrite existing values + * @param string $key Value key + * @param mixed $value Value to store + * @param bool $overwrite Overwrite existing values * * @return bool * * @since 1.0.0 */ - public function set($key, $value, bool $overwrite = false) : bool; + public function set(string $key, $value, bool $overwrite = false) : bool; /** * Remove value from session by key. * - * @param int|string $key Value key + * @param string $key Value key * * @return bool * * @since 1.0.0 */ - public function remove($key) : bool; + public function remove(string $key) : bool; /** * Save session. @@ -72,20 +72,20 @@ interface SessionInterface public function save() : void; /** - * @return null|int|string + * @return string * * @since 1.0.0 */ - public function getSID(); + public function getSID() : string; /** - * @param null|int|string $sid Session id + * @param string $sid Session id * * @return void * * @since 1.0.0 */ - public function setSID($sid) : void; + public function setSID(string $sid) : void; /** * Lock session from further adjustments. diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index b1d3a1532..140be05e8 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -35,7 +35,7 @@ final class Dispatcher implements DispatcherInterface * @var null|ApplicationAbstract * @since 1.0.0 */ - private ?ApplicationAbstract $app = null; + private ?ApplicationAbstract $app; /** * Controller. diff --git a/Localization/L11nManager.php b/Localization/L11nManager.php index 0aad97a2e..9ac5644aa 100644 --- a/Localization/L11nManager.php +++ b/Localization/L11nManager.php @@ -41,7 +41,7 @@ final class L11nManager * @var string * @since 1.0.0 */ - private string $appName = ''; + private string $appName; /** * Construct. diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 00bfe8333..363a0f05f 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -47,10 +47,10 @@ final class FileLogger implements LoggerInterface /** * Instance. * - * @var null|FileLogger + * @var FileLogger * @since 1.0.0 */ - protected static ?FileLogger $instance = null; + protected static FileLogger $instance; /** * Verbose. @@ -76,7 +76,7 @@ final class FileLogger implements LoggerInterface * @var string * @since 1.0.0 */ - private string $path = ''; + private string $path; /** * Is the logging file created @@ -137,7 +137,7 @@ final class FileLogger implements LoggerInterface */ public static function getInstance(string $path = '', bool $verbose = false) : self { - if (self::$instance === null) { + if (!isset(self::$instance)) { self::$instance = new self($path, $verbose); } diff --git a/Message/Console/Request.php b/Message/Console/Request.php index 915b30cb2..d7fdf472e 100644 --- a/Message/Console/Request.php +++ b/Message/Console/Request.php @@ -44,18 +44,10 @@ final class Request extends RequestAbstract /** * Request method. * - * @var null|string + * @var string * @since 1.0.0 */ - protected ?string $method = null; - - /** - * Request type. - * - * @var null|string - * @since 1.0.0 - */ - protected ?string $type = null; + protected string $method; /** * Request hash. @@ -65,21 +57,13 @@ final class Request extends RequestAbstract */ protected array $hash = []; - /** - * Uploaded files. - * - * @var array - * @since 1.0.0 - */ - protected array $files = []; - /** * OS type. * - * @var null|string + * @var string * @since 1.0.0 */ - private ?string $os = null; + private string $os; /** * Constructor. @@ -173,7 +157,7 @@ final class Request extends RequestAbstract */ public function getOS() : string { - if ($this->os === null) { + if (!isset($this->os)) { $this->os = \strtolower(\PHP_OS); } @@ -207,7 +191,7 @@ final class Request extends RequestAbstract */ public function getMethod() : string { - if ($this->method === null) { + if (!isset($this->method)) { $temp = $this->uri->__toString(); $found = \stripos($temp, ':'); $method = $found !== false && $found > 3 && $found < 8 ? \substr($temp, 0, $found) : RequestMethod::GET; diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 7ccbc0348..d2367fb0b 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -36,42 +36,34 @@ final class Request extends RequestAbstract /** * Request method. * - * @var null|string + * @var string * @since 1.0.0 */ - protected ?string $method = null; - - /** - * Request type. - * - * @var null|string - * @since 1.0.0 - */ - protected ?string $type = null; + protected string $method; /** * Browser type. * - * @var null|string + * @var string * @since 1.0.0 */ - private ?string $browser = null; + private string $browser; /** * OS type. * - * @var null|string + * @var string * @since 1.0.0 */ - private ?string $os = null; + private string $os; /** * Request information. * - * @var null|string[] + * @var string[] * @since 1.0.0 */ - private ?array $info = null; + private array $info; /** * Request hash. @@ -87,7 +79,7 @@ final class Request extends RequestAbstract * @var array * @since 1.0.0 */ - protected array $files = []; + protected array $files; /** * Constructor. @@ -364,7 +356,7 @@ final class Request extends RequestAbstract */ public function getRequestInfo() : array { - if ($this->info === null) { + if (!isset($this->info)) { $this->info['browser'] = $this->getBrowser(); $this->info['os'] = $this->getOS(); } @@ -381,7 +373,7 @@ final class Request extends RequestAbstract */ public function getBrowser() : string { - if ($this->browser === null) { + if (!isset($this->browser)) { $arr = BrowserType::getConstants(); $httpUserAgent = \strtolower($_SERVER['HTTP_USER_AGENT']); @@ -422,7 +414,7 @@ final class Request extends RequestAbstract */ public function getOS() : string { - if ($this->os === null) { + if (!isset($this->os)) { $arr = OSType::getConstants(); $httpUserAgent = \strtolower($_SERVER['HTTP_USER_AGENT']); @@ -528,7 +520,7 @@ final class Request extends RequestAbstract */ public function getMethod() : string { - if ($this->method === null) { + if (!isset($this->method)) { $this->method = $_SERVER['REQUEST_METHOD'] ?? RequestMethod::GET; } diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index a9bb4dc5d..94d4ca5fa 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -82,7 +82,7 @@ final class ModuleManager * @var string * @since 1.0.0 */ - private string $modulePath = __DIR__ . '/../../Modules'; + private string $modulePath; /** * All modules in the module directory.