Skip to content

Commit 6fddc8d

Browse files
committed
Add linter, CodeSniffer, PHPStan tests
1 parent 0a29b98 commit 6fddc8d

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.gitattributes export-ignore
22
.gitignore export-ignore
3+
phpcs.xml export-ignore
4+
phpstan.neon export-ignore

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919
"nette/di": "^3.0",
2020
"nette/utils": "^3.2"
2121
},
22+
"require-dev": {
23+
"php-parallel-lint/php-parallel-lint": "^1.3",
24+
"php-parallel-lint/php-console-highlighter": "^0.5.0",
25+
"phpstan/phpstan": "^0.12.99",
26+
"phpstan/phpstan-nette": "^0.12.21",
27+
"spaze/coding-standard": "^0.0",
28+
"spaze/encryption": "^0.4.0"
29+
},
30+
"scripts": {
31+
"lint": "vendor/bin/parallel-lint --colors src/",
32+
"phpcs": "vendor/bin/phpcs src/",
33+
"phpstan": "vendor/phpstan/phpstan/phpstan --ansi analyse --configuration phpstan.neon",
34+
"test": [
35+
"@lint",
36+
"@phpcs",
37+
"@phpstan"
38+
]
39+
},
2240
"autoload": {
2341
"classmap": ["src/"]
2442
},

phpcs.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="MysqlSessionHandler">
3+
<arg name="extensions" value="php"/>
4+
<arg name="cache"/>
5+
<arg name="colors"/>
6+
<arg value="s"/>
7+
<arg value="p"/>
8+
<rule ref="vendor/spaze/coding-standard/src/ruleset.xml"/>
9+
</ruleset>

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
paths:
3+
- src
4+
level: 7
5+
6+
includes:
7+
- vendor/phpstan/phpstan-nette/extension.neon
8+
- vendor/phpstan/phpstan-nette/rules.neon

src/DI/MysqlSessionHandlerExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class MysqlSessionHandlerExtension extends CompilerExtension
1010
{
1111

12-
private $defaults = [
12+
private array $defaults = [
1313
'tableName' => 'sessions',
1414
'lockTimeout' => 5,
1515
'unchangedUpdateDelay' => 300,

src/MysqlSessionHandler.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,30 @@ class MysqlSessionHandler implements SessionHandlerInterface
1818

1919
use SmartObject;
2020

21-
/** @var string */
22-
private $tableName;
2321

24-
/** @var integer */
25-
private $lockTimeout = 5;
22+
private Context $context;
2623

27-
/** @var integer */
28-
private $unchangedUpdateDelay = 300;
24+
private ?StaticKeyEncryption $encryptionService = null;
2925

30-
/** @var Context */
31-
private $context;
26+
private string $tableName;
3227

33-
/** @var string */
34-
private $lockId;
28+
private int $lockTimeout = 5;
29+
30+
private int $unchangedUpdateDelay = 300;
31+
32+
private ?string $lockId = null;
3533

3634
/** @var string[] */
37-
private $idHashes = [];
35+
private array $idHashes = [];
3836

39-
/** @var ActiveRow */
40-
private $row;
37+
/** @var ActiveRow{data:string, timestamp:int}|null */
38+
private ?ActiveRow $row;
4139

4240
/** @var string[] */
43-
private $data = [];
41+
private array $data = [];
4442

4543
/** @var mixed[] */
46-
private $additionalData = [];
47-
48-
/** @var StaticKeyEncryption */
49-
private $encryptionService;
44+
private array $additionalData = [];
5045

5146
/**
5247
* Occurs before the data is written to session.
@@ -108,8 +103,11 @@ private function hash(string $id, bool $rawOutput = true): string
108103
private function lock(): void
109104
{
110105
if ($this->lockId === null) {
111-
$this->lockId = $this->hash(\session_id(), false);
112-
$this->context->query('SELECT GET_LOCK(?, ?) as `lock`', $this->lockId, $this->lockTimeout);
106+
$sessionId = \session_id();
107+
if ($sessionId) {
108+
$this->lockId = $this->hash($sessionId, false);
109+
$this->context->query('SELECT GET_LOCK(?, ?) as `lock`', $this->lockId, $this->lockTimeout);
110+
}
113111
}
114112
}
115113

@@ -128,7 +126,7 @@ private function unlock(): void
128126
/**
129127
* @param string $savePath
130128
* @param string $name
131-
* @return boolean
129+
* @return bool
132130
*/
133131
public function open($savePath, $name): bool
134132
{
@@ -146,7 +144,7 @@ public function close(): bool
146144

147145
/**
148146
* @param string $sessionId
149-
* @return boolean
147+
* @return bool
150148
*/
151149
public function destroy($sessionId): bool
152150
{
@@ -178,7 +176,7 @@ public function read($sessionId): string
178176
/**
179177
* @param string $sessionId
180178
* @param string $sessionData
181-
* @return boolean
179+
* @return bool
182180
*/
183181
public function write($sessionId, $sessionData): bool
184182
{
@@ -191,7 +189,8 @@ public function write($sessionId, $sessionData): bool
191189
$sessionData = $this->encryptionService->encrypt($sessionData);
192190
}
193191
$this->onBeforeDataWrite();
194-
if ($row = $this->context->table($this->tableName)->get($hashedSessionId)) {
192+
$row = $this->context->table($this->tableName)->get($hashedSessionId);
193+
if ($row) {
195194
$row->update([
196195
'timestamp' => $time,
197196
'data' => $sessionData,
@@ -203,7 +202,7 @@ public function write($sessionId, $sessionData): bool
203202
'data' => $sessionData,
204203
] + $this->additionalData);
205204
}
206-
} elseif ($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) {
205+
} elseif (($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) && $this->row) {
207206
// Optimization: When data has not been changed, only update
208207
// the timestamp after a configured delay, if any.
209208
$this->row->update([
@@ -216,8 +215,8 @@ public function write($sessionId, $sessionData): bool
216215

217216

218217
/**
219-
* @param integer $maxLifeTime
220-
* @return boolean
218+
* @param int $maxLifeTime
219+
* @return bool
221220
*/
222221
public function gc($maxLifeTime): bool
223222
{

0 commit comments

Comments
 (0)