Skip to content

Commit

Permalink
PHPStan level 8
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Oct 11, 2021
1 parent 6fddc8d commit e33e20b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
paths:
- src
level: 7
level: 8

includes:
- vendor/phpstan/phpstan-nette/extension.neon
Expand Down
38 changes: 24 additions & 14 deletions src/DI/MysqlSessionHandlerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/MysqlSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e33e20b

Please sign in to comment.