Skip to content

Commit e33e20b

Browse files
committed
PHPStan level 8
1 parent 6fddc8d commit e33e20b

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"nette/utils": "^3.2"
2121
},
2222
"require-dev": {
23+
"nette/schema": "^1.2",
2324
"php-parallel-lint/php-parallel-lint": "^1.3",
2425
"php-parallel-lint/php-console-highlighter": "^0.5.0",
2526
"phpstan/phpstan": "^0.12.99",

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parameters:
22
paths:
33
- src
4-
level: 7
4+
level: 8
55

66
includes:
77
- vendor/phpstan/phpstan-nette/extension.neon

src/DI/MysqlSessionHandlerExtension.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,45 @@
44
namespace Spaze\Session\DI;
55

66
use Nette\DI\CompilerExtension;
7+
use Nette\DI\Definitions\ServiceDefinition;
78
use Nette\DI\Statement;
8-
9+
use Nette\Schema\Expect;
10+
use Nette\Schema\Schema;
11+
use Spaze\Encryption\Symmetric\StaticKey;
12+
use stdClass;
13+
14+
/**
15+
* @property stdClass $config
16+
*/
917
class MysqlSessionHandlerExtension extends CompilerExtension
1018
{
1119

12-
private array $defaults = [
13-
'tableName' => 'sessions',
14-
'lockTimeout' => 5,
15-
'unchangedUpdateDelay' => 300,
16-
'encryptionService' => null,
17-
];
20+
public function getConfigSchema(): Schema
21+
{
22+
return Expect::structure([
23+
'tableName' => Expect::string()->default('sessions'),
24+
'lockTimeout' => Expect::int()->default(5),
25+
'unchangedUpdateDelay' => Expect::int()->default(300),
26+
'encryptionService' => Expect::string(StaticKey::class),
27+
]);
28+
}
1829

1930

2031
public function loadConfiguration(): void
2132
{
22-
$this->validateConfig($this->defaults);
23-
2433
$builder = $this->getContainerBuilder();
2534

2635
$definition = $builder->addDefinition($this->prefix('sessionHandler'))
2736
->setClass('Spaze\Session\MysqlSessionHandler')
28-
->addSetup('setTableName', [$this->config['tableName']])
29-
->addSetup('setLockTimeout', [$this->config['lockTimeout']])
30-
->addSetup('setUnchangedUpdateDelay', [$this->config['unchangedUpdateDelay']]);
37+
->addSetup('setTableName', [$this->config->tableName])
38+
->addSetup('setLockTimeout', [$this->config->lockTimeout])
39+
->addSetup('setUnchangedUpdateDelay', [$this->config->unchangedUpdateDelay]);
3140

32-
if ($this->config['encryptionService']) {
33-
$definition->addSetup('setEncryptionService', [$this->config['encryptionService']]);
41+
if ($this->config->encryptionService) {
42+
$definition->addSetup('setEncryptionService', [$this->config->encryptionService]);
3443
}
3544

45+
/** @var ServiceDefinition */
3646
$sessionDefinition = $builder->getDefinition('session');
3747
$sessionSetup = $sessionDefinition->getSetup();
3848
# Prepend setHandler method to other possible setups (setExpiration) which would start session prematurely

src/MysqlSessionHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function write($sessionId, $sessionData): bool
202202
'data' => $sessionData,
203203
] + $this->additionalData);
204204
}
205-
} elseif (($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) && $this->row) {
205+
} elseif ($this->row && ($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay)) {
206206
// Optimization: When data has not been changed, only update
207207
// the timestamp after a configured delay, if any.
208208
$this->row->update([
@@ -230,9 +230,9 @@ public function gc($maxLifeTime): bool
230230
// In a typical master-master replication setup, the server IDs are 1 and 2.
231231
// There is no subtraction on server 1 and one day (or one tenth of $maxLifeTime)
232232
// subtraction on server 2.
233-
$serverId = $this->context->query('SELECT @@server_id as `server_id`')->fetch()->server_id;
234-
if ($serverId > 1 && $serverId < 10) {
235-
$maxTimestamp -= ($serverId - 1) * \max(86400, $maxLifeTime / 10);
233+
$row = $this->context->query('SELECT @@server_id as `serverId`')->fetch();
234+
if ($row && $row->serverId > 1 && $row->serverId < 10) {
235+
$maxTimestamp -= ($row->serverId - 1) * \max(86400, $maxLifeTime / 10);
236236
}
237237

238238
$this->context->table($this->tableName)

0 commit comments

Comments
 (0)