From 8de7627349fa76dbcace83f1827a4ea3119b14f5 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Sat, 11 Nov 2023 16:49:08 -0300 Subject: [PATCH] Upgrade Coding Standard --- composer.json | 2 +- src/Debug/SessionCollector.php | 4 ++-- src/SaveHandler.php | 3 ++- src/SaveHandlers/DatabaseHandler.php | 10 +++++----- src/SaveHandlers/FilesHandler.php | 22 +++++++++++----------- src/SaveHandlers/MemcachedHandler.php | 22 +++++++++++----------- src/SaveHandlers/RedisHandler.php | 22 +++++++++++----------- src/Session.php | 2 +- tests/Debug/SessionCollectorTest.php | 2 +- tests/SaveHandlers/FilesHandlerTest.php | 2 +- 10 files changed, 46 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index d38d819..9d74d62 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ }, "require-dev": { "ext-xdebug": "*", - "aplus/coding-standard": "^1.14", + "aplus/coding-standard": "^2.0", "ergebnis/composer-normalize": "^2.25", "jetbrains/phpstorm-attributes": "^1.0", "phpmd/phpmd": "^2.13", diff --git a/src/Debug/SessionCollector.php b/src/Debug/SessionCollector.php index 43ef7bc..dd00396 100644 --- a/src/Debug/SessionCollector.php +++ b/src/Debug/SessionCollector.php @@ -58,10 +58,10 @@ public function setSaveHandler(SaveHandler $handler) : static public function getContents() : string { - if ( ! isset($this->session)) { + if (!isset($this->session)) { return '

No Session instance has been set on this collector.

'; } - if ( ! $this->session->isActive()) { + if (!$this->session->isActive()) { return '

Session is inactive.

'; } \ob_start(); ?> diff --git a/src/SaveHandler.php b/src/SaveHandler.php index 424538a..5422856 100644 --- a/src/SaveHandler.php +++ b/src/SaveHandler.php @@ -68,7 +68,8 @@ abstract class SaveHandler implements \SessionHandlerInterface, \SessionUpdateTi * @param Logger|null $logger */ public function __construct( - #[SensitiveParameter] array $config = [], + #[SensitiveParameter] + array $config = [], Logger $logger = null ) { $this->prepareConfig($config); diff --git a/src/SaveHandlers/DatabaseHandler.php b/src/SaveHandlers/DatabaseHandler.php index 98c401e..2c1bd37 100644 --- a/src/SaveHandlers/DatabaseHandler.php +++ b/src/SaveHandlers/DatabaseHandler.php @@ -173,11 +173,11 @@ public function open($path, $name) : bool public function read($id) : string { - if ( ! isset($this->database) || $this->lock($id) === false) { + if (!isset($this->database) || $this->lock($id) === false) { $this->setFingerprint(''); return ''; } - if ( ! isset($this->sessionId)) { + if (!isset($this->sessionId)) { $this->sessionId = $id; } $statement = $this->database @@ -194,7 +194,7 @@ public function read($id) : string public function write($id, $data) : bool { - if ( ! isset($this->database)) { + if (!isset($this->database)) { return false; } if ($this->lockId === false) { @@ -245,7 +245,7 @@ protected function writeUpdate(string $id, string $data) : bool return 'NOW()'; }, ]; - if ( ! $this->hasSameFingerprint($data)) { + if (!$this->hasSameFingerprint($data)) { $columns[$this->getColumn('data')] = $data; } $this->addUserIdColumn($columns); @@ -277,7 +277,7 @@ public function updateTimestamp($id, $data) : bool public function close() : bool { - $closed = ! ($this->lockId && ! $this->unlock()); + $closed = !($this->lockId && !$this->unlock()); $this->database = null; return $closed; } diff --git a/src/SaveHandlers/FilesHandler.php b/src/SaveHandlers/FilesHandler.php index 2690a8a..092eadb 100644 --- a/src/SaveHandlers/FilesHandler.php +++ b/src/SaveHandlers/FilesHandler.php @@ -62,14 +62,14 @@ protected function prepareConfig(#[SensitiveParameter] array $config) : void $this->config['directory'], \DIRECTORY_SEPARATOR ) . \DIRECTORY_SEPARATOR; - if ( ! \is_dir($this->config['directory'])) { + if (!\is_dir($this->config['directory'])) { throw new LogicException( 'Session config directory does not exist: ' . $this->config['directory'] ); } if ($this->config['prefix']) { $dirname = $this->config['directory'] . $this->config['prefix'] . \DIRECTORY_SEPARATOR; - if ( ! \is_dir($dirname) && ! \mkdir($dirname, 0700) && ! \is_dir($dirname)) { + if (!\is_dir($dirname) && !\mkdir($dirname, 0700) && !\is_dir($dirname)) { throw new RuntimeException( "Session prefix directory '{$dirname}' was not created", ); @@ -105,19 +105,19 @@ public function read($id) : string } $filename = $this->getFilename($id); $dirname = \dirname($filename); - if ( ! \is_dir($dirname) && ! \mkdir($dirname, 0700) && ! \is_dir($dirname)) { + if (!\is_dir($dirname) && !\mkdir($dirname, 0700) && !\is_dir($dirname)) { throw new RuntimeException( "Session subdirectory '{$dirname}' was not created", ); } $this->sessionExists = \is_file($filename); - if ( ! $this->lock($filename)) { + if (!$this->lock($filename)) { return ''; } - if ( ! isset($this->sessionId)) { + if (!isset($this->sessionId)) { $this->sessionId = $id; } - if ( ! $this->sessionExists) { + if (!$this->sessionExists) { \chmod($filename, 0600); $this->setFingerprint(''); return ''; @@ -128,7 +128,7 @@ public function read($id) : string protected function readData() : string { $data = ''; - while ( ! \feof($this->stream)) { + while (!\feof($this->stream)) { $data .= \fread($this->stream, 1024); } $this->setFingerprint($data); @@ -137,14 +137,14 @@ protected function readData() : string public function write($id, $data) : bool { - if ( ! isset($this->stream)) { + if (!isset($this->stream)) { return false; } if ($id !== $this->sessionId) { $this->sessionId = $id; } if ($this->hasSameFingerprint($data)) { - return ! $this->sessionExists || \touch($this->getFilename($id)); + return !$this->sessionExists || \touch($this->getFilename($id)); } if ($this->sessionExists) { \ftruncate($this->stream, 0); @@ -171,7 +171,7 @@ public function updateTimestamp($id, $data) : bool public function close() : bool { - if ( ! \is_resource($this->stream)) { + if (!\is_resource($this->stream)) { return true; } $this->unlock(); @@ -184,7 +184,7 @@ public function destroy($id) : bool $this->close(); \clearstatcache(); $filename = $this->getFilename($id); - return ! \is_file($filename) || \unlink($filename); + return !\is_file($filename) || \unlink($filename); } public function gc($max_lifetime) : int | false diff --git a/src/SaveHandlers/MemcachedHandler.php b/src/SaveHandlers/MemcachedHandler.php index ac78808..948587d 100644 --- a/src/SaveHandlers/MemcachedHandler.php +++ b/src/SaveHandlers/MemcachedHandler.php @@ -84,7 +84,7 @@ protected function prepareConfig(#[SensitiveParameter] array $config) : void 'match_ua' => false, ], $config); foreach ($this->config['servers'] as $index => $server) { - if ( ! isset($server['host'])) { + if (!isset($server['host'])) { throw new OutOfBoundsException( "Memcached host not set on server config '{$index}'" ); @@ -165,7 +165,7 @@ public function open($path, $name) : bool if ($result === false) { $this->log('Session (memcached): ' . $this->memcached->getLastErrorMessage()); } - if ( ! $this->memcached->getStats()) { + if (!$this->memcached->getStats()) { $this->log('Session (memcached): Could not connect to any server'); return false; } @@ -174,10 +174,10 @@ public function open($path, $name) : bool public function read($id) : string { - if ( ! isset($this->memcached) || ! $this->lock($id)) { + if (!isset($this->memcached) || !$this->lock($id)) { return ''; } - if ( ! isset($this->sessionId)) { + if (!isset($this->sessionId)) { $this->sessionId = $id; } $data = (string) $this->memcached->get($this->getKey($id)); @@ -187,11 +187,11 @@ public function read($id) : string public function write($id, $data) : bool { - if ( ! isset($this->memcached)) { + if (!isset($this->memcached)) { return false; } if ($id !== $this->sessionId) { - if ( ! $this->unlock() || ! $this->lock($id)) { + if (!$this->unlock() || !$this->lock($id)) { return false; } $this->setFingerprint(''); @@ -229,7 +229,7 @@ public function close() : bool if ($this->lockId) { $this->memcached->delete($this->lockId); } - if ( ! $this->memcached->quit()) { + if (!$this->memcached->quit()) { return false; } $this->memcached = null; @@ -238,11 +238,11 @@ public function close() : bool public function destroy($id) : bool { - if ( ! $this->lockId) { + if (!$this->lockId) { return false; } $destroyed = $this->memcached->delete($this->getKey($id)); - return ! ($destroyed === false + return !($destroyed === false && $this->memcached->getResultCode() !== Memcached::RES_NOTFOUND); } @@ -265,7 +265,7 @@ protected function lock(string $id) : bool \usleep($this->config['lock_sleep']); continue; } - if ( ! $this->memcached->set($lockId, \time(), $expiration)) { + if (!$this->memcached->set($lockId, \time(), $expiration)) { $this->log('Session (memcached): Error while trying to lock ' . $lockId); return false; } @@ -286,7 +286,7 @@ protected function unlock() : bool if ($this->lockId === false) { return true; } - if ( ! $this->memcached->delete($this->lockId) && + if (!$this->memcached->delete($this->lockId) && $this->memcached->getResultCode() !== Memcached::RES_NOTFOUND ) { $this->log('Session (memcached): Error while trying to unlock ' . $this->lockId); diff --git a/src/SaveHandlers/RedisHandler.php b/src/SaveHandlers/RedisHandler.php index 5be0802..3b6d35f 100644 --- a/src/SaveHandlers/RedisHandler.php +++ b/src/SaveHandlers/RedisHandler.php @@ -130,7 +130,7 @@ public function open($path, $name) : bool } } if (isset($this->config['database']) - && ! $this->redis->select($this->config['database']) + && !$this->redis->select($this->config['database']) ) { $this->log( "Session (redis): Could not select the database '{$this->config['database']}'" @@ -142,10 +142,10 @@ public function open($path, $name) : bool public function read($id) : string { - if ( ! isset($this->redis) || ! $this->lock($id)) { + if (!isset($this->redis) || !$this->lock($id)) { return ''; } - if ( ! isset($this->sessionId)) { + if (!isset($this->sessionId)) { $this->sessionId = $id; } $data = $this->redis->get($this->getKey($id)); @@ -156,11 +156,11 @@ public function read($id) : string public function write($id, $data) : bool { - if ( ! isset($this->redis)) { + if (!isset($this->redis)) { return false; } if ($id !== $this->sessionId) { - if ( ! $this->unlock() || ! $this->lock($id)) { + if (!$this->unlock() || !$this->lock($id)) { return false; } $this->sessionExists = false; @@ -171,7 +171,7 @@ public function write($id, $data) : bool } $maxlifetime = $this->getMaxlifetime(); $this->redis->expire($this->lockId, $this->config['lock_ttl']); - if ($this->sessionExists === false || ! $this->hasSameFingerprint($data)) { + if ($this->sessionExists === false || !$this->hasSameFingerprint($data)) { if ($this->redis->set($this->getKey($id), $data, $maxlifetime)) { $this->setFingerprint($data); $this->sessionExists = true; @@ -189,7 +189,7 @@ public function updateTimestamp($id, $data) : bool public function close() : bool { - if ( ! isset($this->redis)) { + if (!isset($this->redis)) { return true; } try { @@ -197,7 +197,7 @@ public function close() : bool if ($this->lockId) { $this->redis->del($this->lockId); } - if ( ! $this->redis->close()) { + if (!$this->redis->close()) { return false; } } @@ -210,7 +210,7 @@ public function close() : bool public function destroy($id) : bool { - if ( ! $this->lockId) { + if (!$this->lockId) { return false; } $result = $this->redis->del($this->getKey($id)); @@ -243,7 +243,7 @@ protected function lock(string $id) : bool \usleep($this->config['lock_sleep']); continue; } - if ( ! $this->redis->setex($lockId, $ttl, (string) \time())) { + if (!$this->redis->setex($lockId, $ttl, (string) \time())) { $this->log('Session (redis): Error while trying to lock ' . $lockId); return false; } @@ -270,7 +270,7 @@ protected function unlock() : bool if ($this->lockId === false) { return true; } - if ( ! $this->redis->del($this->lockId)) { + if (!$this->redis->del($this->lockId)) { $this->log('Session (redis): Error while trying to unlock ' . $this->lockId); return false; } diff --git a/src/Session.php b/src/Session.php index 4790441..10648db 100644 --- a/src/Session.php +++ b/src/Session.php @@ -134,7 +134,7 @@ public function start(array $customOptions = []) : bool if ($this->isActive()) { throw new LogicException('Session was already active'); } - if ( ! @\session_start($this->getOptions($customOptions))) { + if (!@\session_start($this->getOptions($customOptions))) { $message = ''; if (\error_get_last()) { $message = ': ' . \error_get_last()['message']; diff --git a/tests/Debug/SessionCollectorTest.php b/tests/Debug/SessionCollectorTest.php index 7eeb5fb..a653749 100644 --- a/tests/Debug/SessionCollectorTest.php +++ b/tests/Debug/SessionCollectorTest.php @@ -167,7 +167,7 @@ public function testAutoRegenerateId() : void public function saveHandlerProvider() : Generator { $directory = \sys_get_temp_dir() . '/sessions'; - if ( ! \is_dir($directory)) { + if (!\is_dir($directory)) { \mkdir($directory); } yield [ diff --git a/tests/SaveHandlers/FilesHandlerTest.php b/tests/SaveHandlers/FilesHandlerTest.php index 516d726..ebbced2 100644 --- a/tests/SaveHandlers/FilesHandlerTest.php +++ b/tests/SaveHandlers/FilesHandlerTest.php @@ -23,7 +23,7 @@ class FilesHandlerTest extends AbstractHandler public function setUp() : void { $directory = \getenv('FILES_DIR'); - if ($directory && ! \is_dir($directory)) { + if ($directory && !\is_dir($directory)) { \mkdir($directory, 0700, true); } $this->replaceConfig([