Skip to content

Commit

Permalink
Add linter, CodeSniffer, PHPStan tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Oct 11, 2021
1 parent 0a29b98 commit 6fddc8d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
phpcs.xml export-ignore
phpstan.neon export-ignore
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
"nette/di": "^3.0",
"nette/utils": "^3.2"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3",
"php-parallel-lint/php-console-highlighter": "^0.5.0",
"phpstan/phpstan": "^0.12.99",
"phpstan/phpstan-nette": "^0.12.21",
"spaze/coding-standard": "^0.0",
"spaze/encryption": "^0.4.0"
},
"scripts": {
"lint": "vendor/bin/parallel-lint --colors src/",
"phpcs": "vendor/bin/phpcs src/",
"phpstan": "vendor/phpstan/phpstan/phpstan --ansi analyse --configuration phpstan.neon",
"test": [
"@lint",
"@phpcs",
"@phpstan"
]
},
"autoload": {
"classmap": ["src/"]
},
Expand Down
9 changes: 9 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset name="MysqlSessionHandler">
<arg name="extensions" value="php"/>
<arg name="cache"/>
<arg name="colors"/>
<arg value="s"/>
<arg value="p"/>
<rule ref="vendor/spaze/coding-standard/src/ruleset.xml"/>
</ruleset>
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
paths:
- src
level: 7

includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
2 changes: 1 addition & 1 deletion src/DI/MysqlSessionHandlerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class MysqlSessionHandlerExtension extends CompilerExtension
{

private $defaults = [
private array $defaults = [
'tableName' => 'sessions',
'lockTimeout' => 5,
'unchangedUpdateDelay' => 300,
Expand Down
53 changes: 26 additions & 27 deletions src/MysqlSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,30 @@ class MysqlSessionHandler implements SessionHandlerInterface

use SmartObject;

/** @var string */
private $tableName;

/** @var integer */
private $lockTimeout = 5;
private Context $context;

/** @var integer */
private $unchangedUpdateDelay = 300;
private ?StaticKeyEncryption $encryptionService = null;

/** @var Context */
private $context;
private string $tableName;

/** @var string */
private $lockId;
private int $lockTimeout = 5;

private int $unchangedUpdateDelay = 300;

private ?string $lockId = null;

/** @var string[] */
private $idHashes = [];
private array $idHashes = [];

/** @var ActiveRow */
private $row;
/** @var ActiveRow{data:string, timestamp:int}|null */
private ?ActiveRow $row;

/** @var string[] */
private $data = [];
private array $data = [];

/** @var mixed[] */
private $additionalData = [];

/** @var StaticKeyEncryption */
private $encryptionService;
private array $additionalData = [];

/**
* Occurs before the data is written to session.
Expand Down Expand Up @@ -108,8 +103,11 @@ private function hash(string $id, bool $rawOutput = true): string
private function lock(): void
{
if ($this->lockId === null) {
$this->lockId = $this->hash(\session_id(), false);
$this->context->query('SELECT GET_LOCK(?, ?) as `lock`', $this->lockId, $this->lockTimeout);
$sessionId = \session_id();
if ($sessionId) {
$this->lockId = $this->hash($sessionId, false);
$this->context->query('SELECT GET_LOCK(?, ?) as `lock`', $this->lockId, $this->lockTimeout);
}
}
}

Expand All @@ -128,7 +126,7 @@ private function unlock(): void
/**
* @param string $savePath
* @param string $name
* @return boolean
* @return bool
*/
public function open($savePath, $name): bool
{
Expand All @@ -146,7 +144,7 @@ public function close(): bool

/**
* @param string $sessionId
* @return boolean
* @return bool
*/
public function destroy($sessionId): bool
{
Expand Down Expand Up @@ -178,7 +176,7 @@ public function read($sessionId): string
/**
* @param string $sessionId
* @param string $sessionData
* @return boolean
* @return bool
*/
public function write($sessionId, $sessionData): bool
{
Expand All @@ -191,7 +189,8 @@ public function write($sessionId, $sessionData): bool
$sessionData = $this->encryptionService->encrypt($sessionData);
}
$this->onBeforeDataWrite();
if ($row = $this->context->table($this->tableName)->get($hashedSessionId)) {
$row = $this->context->table($this->tableName)->get($hashedSessionId);
if ($row) {
$row->update([
'timestamp' => $time,
'data' => $sessionData,
Expand All @@ -203,7 +202,7 @@ public function write($sessionId, $sessionData): bool
'data' => $sessionData,
] + $this->additionalData);
}
} elseif ($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) {
} elseif (($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay) && $this->row) {
// Optimization: When data has not been changed, only update
// the timestamp after a configured delay, if any.
$this->row->update([
Expand All @@ -216,8 +215,8 @@ public function write($sessionId, $sessionData): bool


/**
* @param integer $maxLifeTime
* @return boolean
* @param int $maxLifeTime
* @return bool
*/
public function gc($maxLifeTime): bool
{
Expand Down

0 comments on commit 6fddc8d

Please sign in to comment.