From e33e20bf2c456ff6b825225affad8a2596d99f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Thu, 7 Oct 2021 21:33:17 +0200 Subject: [PATCH] PHPStan level 8 --- composer.json | 1 + phpstan.neon | 2 +- src/DI/MysqlSessionHandlerExtension.php | 38 ++++++++++++++++--------- src/MysqlSessionHandler.php | 8 +++--- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 077c2c9..80cf9bc 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "nette/utils": "^3.2" }, "require-dev": { + "nette/schema": "^1.2", "php-parallel-lint/php-parallel-lint": "^1.3", "php-parallel-lint/php-console-highlighter": "^0.5.0", "phpstan/phpstan": "^0.12.99", diff --git a/phpstan.neon b/phpstan.neon index 24b4d98..a68aab9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ parameters: paths: - src - level: 7 + level: 8 includes: - vendor/phpstan/phpstan-nette/extension.neon diff --git a/src/DI/MysqlSessionHandlerExtension.php b/src/DI/MysqlSessionHandlerExtension.php index d70cb30..0c37791 100644 --- a/src/DI/MysqlSessionHandlerExtension.php +++ b/src/DI/MysqlSessionHandlerExtension.php @@ -4,35 +4,45 @@ namespace Spaze\Session\DI; use Nette\DI\CompilerExtension; +use Nette\DI\Definitions\ServiceDefinition; use Nette\DI\Statement; - +use Nette\Schema\Expect; +use Nette\Schema\Schema; +use Spaze\Encryption\Symmetric\StaticKey; +use stdClass; + +/** + * @property stdClass $config + */ class MysqlSessionHandlerExtension extends CompilerExtension { - private array $defaults = [ - 'tableName' => 'sessions', - 'lockTimeout' => 5, - 'unchangedUpdateDelay' => 300, - 'encryptionService' => null, - ]; + public function getConfigSchema(): Schema + { + return Expect::structure([ + 'tableName' => Expect::string()->default('sessions'), + 'lockTimeout' => Expect::int()->default(5), + 'unchangedUpdateDelay' => Expect::int()->default(300), + 'encryptionService' => Expect::string(StaticKey::class), + ]); + } public function loadConfiguration(): void { - $this->validateConfig($this->defaults); - $builder = $this->getContainerBuilder(); $definition = $builder->addDefinition($this->prefix('sessionHandler')) ->setClass('Spaze\Session\MysqlSessionHandler') - ->addSetup('setTableName', [$this->config['tableName']]) - ->addSetup('setLockTimeout', [$this->config['lockTimeout']]) - ->addSetup('setUnchangedUpdateDelay', [$this->config['unchangedUpdateDelay']]); + ->addSetup('setTableName', [$this->config->tableName]) + ->addSetup('setLockTimeout', [$this->config->lockTimeout]) + ->addSetup('setUnchangedUpdateDelay', [$this->config->unchangedUpdateDelay]); - if ($this->config['encryptionService']) { - $definition->addSetup('setEncryptionService', [$this->config['encryptionService']]); + if ($this->config->encryptionService) { + $definition->addSetup('setEncryptionService', [$this->config->encryptionService]); } + /** @var ServiceDefinition */ $sessionDefinition = $builder->getDefinition('session'); $sessionSetup = $sessionDefinition->getSetup(); # Prepend setHandler method to other possible setups (setExpiration) which would start session prematurely diff --git a/src/MysqlSessionHandler.php b/src/MysqlSessionHandler.php index 65e3fc0..f52d1b1 100644 --- a/src/MysqlSessionHandler.php +++ b/src/MysqlSessionHandler.php @@ -202,7 +202,7 @@ public function write($sessionId, $sessionData): bool 'data' => $sessionData, ] + $this->additionalData); } - } elseif (($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) && $this->row) { + } elseif ($this->row && ($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay)) { // Optimization: When data has not been changed, only update // the timestamp after a configured delay, if any. $this->row->update([ @@ -230,9 +230,9 @@ public function gc($maxLifeTime): bool // In a typical master-master replication setup, the server IDs are 1 and 2. // There is no subtraction on server 1 and one day (or one tenth of $maxLifeTime) // subtraction on server 2. - $serverId = $this->context->query('SELECT @@server_id as `server_id`')->fetch()->server_id; - if ($serverId > 1 && $serverId < 10) { - $maxTimestamp -= ($serverId - 1) * \max(86400, $maxLifeTime / 10); + $row = $this->context->query('SELECT @@server_id as `serverId`')->fetch(); + if ($row && $row->serverId > 1 && $row->serverId < 10) { + $maxTimestamp -= ($row->serverId - 1) * \max(86400, $maxLifeTime / 10); } $this->context->table($this->tableName)