From 4d112970b4749f5bdb847785200c582c2cd506cd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 16 Nov 2022 02:26:39 +0700 Subject: [PATCH 1/3] Apply PHP 8.0 Syntax and constructor promotion Signed-off-by: Abdul Malik Ikhsan --- src/AbstractContainer.php | 8 ++-- src/AbstractManager.php | 8 +--- src/Config/ConfigInterface.php | 3 +- src/Config/SessionConfig.php | 31 +++++---------- src/SaveHandler/DbTableGateway.php | 28 +++++--------- src/SaveHandler/MongoDB.php | 13 +------ src/SaveHandler/MongoDBOptions.php | 1 - src/Service/SessionManagerFactory.php | 9 ++--- src/SessionManager.php | 8 +--- src/Storage/AbstractSessionArrayStorage.php | 26 ++++--------- src/Storage/Factory.php | 42 ++++++++------------- src/Validator/Id.php | 18 +++------ src/Validator/RemoteAddr.php | 1 - src/ValidatorChain.php | 9 +---- test/Config/SessionConfigTest.php | 8 +--- test/Config/StandardConfigTest.php | 4 +- test/SaveHandler/CacheTest.php | 8 ++-- test/Service/SessionManagerFactoryTest.php | 2 +- test/SessionManagerTest.php | 10 +---- 19 files changed, 74 insertions(+), 163 deletions(-) diff --git a/src/AbstractContainer.php b/src/AbstractContainer.php index 7b0571ed..2c6416c9 100644 --- a/src/AbstractContainer.php +++ b/src/AbstractContainer.php @@ -532,11 +532,11 @@ public function setExpirationSeconds($ttl, $vars = null) $container = $this; // Filter out any items not in our container - $expires = array_filter($vars, static fn($value) => $container->offsetExists($value)); + $expires = array_filter($vars, static fn($value): bool => $container->offsetExists($value)); // Map item keys => timestamp $expires = array_flip($expires); - $expires = array_map(static fn() => $ts, $expires); + $expires = array_map(static fn(): int => $ts, $expires); // Create metadata array to merge in $data = ['EXPIRE_KEYS' => $expires]; @@ -579,11 +579,11 @@ public function setExpirationHops($hops, $vars = null) $container = $this; // FilterInterface out any items not in our container - $expires = array_filter($vars, static fn($value) => $container->offsetExists($value)); + $expires = array_filter($vars, static fn($value): bool => $container->offsetExists($value)); // Map item keys => timestamp $expires = array_flip($expires); - $expires = array_map(static fn() => ['hops' => $hops, 'ts' => $ts], $expires); + $expires = array_map(static fn(): array => ['hops' => $hops, 'ts' => $ts], $expires); // Create metadata array to merge in $data = ['EXPIRE_HOPS_KEYS' => $expires]; diff --git a/src/AbstractManager.php b/src/AbstractManager.php index 104c0c29..41366b89 100644 --- a/src/AbstractManager.php +++ b/src/AbstractManager.php @@ -42,20 +42,16 @@ abstract class AbstractManager implements Manager /** @var SaveHandler */ protected $saveHandler; - /** @var array */ - protected $validators; - /** * Constructor * - * @param array $validators * @throws Exception\RuntimeException */ public function __construct( ?Config $config = null, ?Storage $storage = null, ?SaveHandler $saveHandler = null, - array $validators = [] + protected array $validators = [] ) { // init config if ($config === null) { @@ -105,8 +101,6 @@ public function __construct( if ($saveHandler !== null) { $this->saveHandler = $saveHandler; } - - $this->validators = $validators; } /** diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php index c4b3c643..114c348e 100644 --- a/src/Config/ConfigInterface.php +++ b/src/Config/ConfigInterface.php @@ -18,10 +18,9 @@ public function getOptions(); /** * @param string $option - * @param mixed $value * @return void */ - public function setOption($option, $value); + public function setOption($option, mixed $value); /** * @param string $option diff --git a/src/Config/SessionConfig.php b/src/Config/SessionConfig.php index 9b4f2899..c417ba9e 100644 --- a/src/Config/SessionConfig.php +++ b/src/Config/SessionConfig.php @@ -29,7 +29,7 @@ use function session_write_close; use function set_error_handler; use function sprintf; -use function strpos; +use function str_contains; use function strtolower; use function trigger_error; use function trim; @@ -201,26 +201,13 @@ public function setStorageOption($storageName, $storageValue) */ public function getStorageOption($storageOption) { - switch ($storageOption) { - case 'remember_me_seconds': - // No remote storage option; just return the current value - return $this->rememberMeSeconds; - case 'url_rewriter_tags': - return ini_get('url_rewriter.tags'); - // The following all need a transformation on the retrieved value; - // however they use the same key naming scheme - case 'use_cookies': - case 'use_only_cookies': - case 'use_trans_sid': - case 'cookie_httponly': - return (bool) ini_get('session.' . $storageOption); - case 'save_handler': - // Save handlers must be treated differently due to changes - // introduced in PHP 7.2. - return $this->saveHandler ?: $this->sessionModuleName(); - default: - return ini_get('session.' . $storageOption); - } + return match ($storageOption) { + 'remember_me_seconds' => $this->rememberMeSeconds, + 'url_rewriter_tags' => ini_get('url_rewriter.tags'), + 'use_cookies', 'use_only_cookies', 'use_trans_sid', 'cookie_httponly' => (bool) ini_get('session.' . $storageOption), + 'save_handler' => $this->saveHandler ?: $this->sessionModuleName(), + default => ini_get('session.' . $storageOption), + }; } /** @@ -445,7 +432,7 @@ private function locateRegisteredSaveHandlers() $content = array_shift($matches); - $handlers = false !== strpos($content, '') + $handlers = str_contains($content, '') ? $this->parseSaveHandlersFromHtml($content) : $this->parseSaveHandlersFromPlainText($content); diff --git a/src/SaveHandler/DbTableGateway.php b/src/SaveHandler/DbTableGateway.php index 24a8a2fd..b548c9c7 100644 --- a/src/SaveHandler/DbTableGateway.php +++ b/src/SaveHandler/DbTableGateway.php @@ -37,27 +37,19 @@ class DbTableGateway implements SaveHandlerInterface */ protected $lifetime; - /** - * Laminas Db Table Gateway - * - * @var TableGateway - */ - protected $tableGateway; - - /** - * DbTableGateway Options - * - * @var DbTableGatewayOptions - */ - protected $options; - /** * Constructor */ - public function __construct(TableGateway $tableGateway, DbTableGatewayOptions $options) - { - $this->tableGateway = $tableGateway; - $this->options = $options; + public function __construct( + /** + * Laminas Db Table Gateway + */ + protected TableGateway $tableGateway, + /** + * DbTableGateway Options + */ + protected DbTableGatewayOptions $options + ) { } /** diff --git a/src/SaveHandler/MongoDB.php b/src/SaveHandler/MongoDB.php index cf2c2ef3..9ced59c3 100644 --- a/src/SaveHandler/MongoDB.php +++ b/src/SaveHandler/MongoDB.php @@ -22,13 +22,6 @@ */ class MongoDB implements SaveHandlerInterface { - /** - * MongoClient instance - * - * @var MongoClient - */ - protected $mongoClient; - /** * MongoCollection instance * @@ -63,7 +56,7 @@ class MongoDB implements SaveHandlerInterface * @param MongoClient $mongoClient * @throws InvalidArgumentException */ - public function __construct($mongoClient, MongoDBOptions $options) + public function __construct(protected $mongoClient, MongoDBOptions $options) { if (null === ($database = $options->getDatabase())) { throw new InvalidArgumentException('The database option cannot be empty'); @@ -72,9 +65,7 @@ public function __construct($mongoClient, MongoDBOptions $options) if (null === ($collection = $options->getCollection())) { throw new InvalidArgumentException('The collection option cannot be empty'); } - - $this->mongoClient = $mongoClient; - $this->options = $options; + $this->options = $options; } /** diff --git a/src/SaveHandler/MongoDBOptions.php b/src/SaveHandler/MongoDBOptions.php index eb688520..e7e91bd4 100644 --- a/src/SaveHandler/MongoDBOptions.php +++ b/src/SaveHandler/MongoDBOptions.php @@ -168,7 +168,6 @@ public function getCollection() * * @see http://php.net/manual/en/mongocollection.save.php * - * @param array $saveOptions * @return MongoDBOptions */ public function setSaveOptions(array $saveOptions) diff --git a/src/Service/SessionManagerFactory.php b/src/Service/SessionManagerFactory.php index bf7f537e..9a0ebec1 100644 --- a/src/Service/SessionManagerFactory.php +++ b/src/Service/SessionManagerFactory.php @@ -17,9 +17,8 @@ use function array_merge; use function class_exists; -use function gettype; +use function get_debug_type; use function is_array; -use function is_object; use function is_subclass_of; use function sprintf; @@ -80,7 +79,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ 'SessionManager requires that the %s service implement %s; received "%s"', ConfigInterface::class, ConfigInterface::class, - is_object($config) ? $config::class : gettype($config) + get_debug_type($config) )); } } @@ -92,7 +91,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ 'SessionManager requires that the %s service implement %s; received "%s"', StorageInterface::class, StorageInterface::class, - is_object($storage) ? $storage::class : gettype($storage) + get_debug_type($storage) )); } } @@ -104,7 +103,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $ 'SessionManager requires that the %s service implement %s; received "%s"', SaveHandlerInterface::class, SaveHandlerInterface::class, - is_object($saveHandler) ? $saveHandler::class : gettype($saveHandler) + get_debug_type($saveHandler) )); } } diff --git a/src/SessionManager.php b/src/SessionManager.php index ef7d967a..707e12d8 100644 --- a/src/SessionManager.php +++ b/src/SessionManager.php @@ -443,12 +443,8 @@ public function expireSessionCookie() } setcookie( $this->getName(), // session name - '', // value - $_SERVER['REQUEST_TIME'] - 42000, // TTL for cookie - $config->getCookiePath(), - $config->getCookieDomain(), - (bool) $config->getCookieSecure(), - (bool) $config->getCookieHttpOnly() + '', + ['expires' => $_SERVER['REQUEST_TIME'] - 42000, 'path' => $config->getCookiePath(), 'domain' => $config->getCookieDomain(), 'secure' => (bool) $config->getCookieSecure(), 'httponly' => (bool) $config->getCookieHttpOnly()] ); } diff --git a/src/Storage/AbstractSessionArrayStorage.php b/src/Storage/AbstractSessionArrayStorage.php index 7768f1e4..659f3104 100644 --- a/src/Storage/AbstractSessionArrayStorage.php +++ b/src/Storage/AbstractSessionArrayStorage.php @@ -67,10 +67,9 @@ public function init($input = null) /** * Get Offset * - * @param mixed $key * @return mixed */ - public function __get($key) + public function __get(mixed $key) { return $this->offsetGet($key); } @@ -78,11 +77,9 @@ public function __get($key) /** * Set Offset * - * @param mixed $key - * @param mixed $value * @return void */ - public function __set($key, $value) + public function __set(mixed $key, mixed $value) { $this->offsetSet($key, $value); } @@ -90,10 +87,9 @@ public function __set($key, $value) /** * Isset Offset * - * @param mixed $key * @return bool */ - public function __isset($key) + public function __isset(mixed $key) { return $this->offsetExists($key); } @@ -101,10 +97,9 @@ public function __isset($key) /** * Unset Offset * - * @param mixed $key * @return void */ - public function __unset($key) + public function __unset(mixed $key) { $this->offsetUnset($key); } @@ -121,11 +116,10 @@ public function __destruct() /** * Offset Exists * - * @param mixed $key * @return bool */ #[ReturnTypeWillChange] - public function offsetExists($key) + public function offsetExists(mixed $key) { return isset($_SESSION[$key]); } @@ -133,11 +127,10 @@ public function offsetExists($key) /** * Offset Get * - * @param mixed $key * @return mixed */ #[ReturnTypeWillChange] - public function offsetGet($key) + public function offsetGet(mixed $key) { if (isset($_SESSION[$key])) { return $_SESSION[$key]; @@ -149,12 +142,10 @@ public function offsetGet($key) /** * Offset Set * - * @param mixed $key - * @param mixed $value * @return void */ #[ReturnTypeWillChange] - public function offsetSet($key, $value) + public function offsetSet(mixed $key, mixed $value) { $_SESSION[$key] = $value; } @@ -162,11 +153,10 @@ public function offsetSet($key, $value) /** * Offset Unset * - * @param mixed $key * @return void */ #[ReturnTypeWillChange] - public function offsetUnset($key) + public function offsetUnset(mixed $key) { unset($_SESSION[$key]); } diff --git a/src/Storage/Factory.php b/src/Storage/Factory.php index 48e58b0a..ab6217bd 100644 --- a/src/Storage/Factory.php +++ b/src/Storage/Factory.php @@ -14,11 +14,9 @@ use function class_exists; use function class_implements; use function class_parents; -use function get_class; -use function gettype; +use function get_debug_type; use function in_array; use function is_array; -use function is_object; use function is_string; use function sprintf; @@ -38,7 +36,7 @@ public static function factory($type, $options = []) throw new Exception\InvalidArgumentException(sprintf( '%s expects the $type argument to be a string class name; received "%s"', __METHOD__, - is_object($type) ? $type::class : gettype($type) + get_debug_type($type) )); } if (! class_exists($type)) { @@ -60,25 +58,20 @@ public static function factory($type, $options = []) throw new Exception\InvalidArgumentException(sprintf( '%s expects the $options argument to be an array or Traversable; received "%s"', __METHOD__, - is_object($options) ? $options::class : gettype($options) + get_debug_type($options) )); } - switch (true) { - case in_array(AbstractSessionArrayStorage::class, class_parents($type)): - return static::createSessionArrayStorage($type, $options); - case $type === ArrayStorage::class: - case in_array(ArrayStorage::class, class_parents($type)): - return static::createArrayStorage($type, $options); - case in_array(StorageInterface::class, class_implements($type)): - return new $type($options); - default: - throw new Exception\InvalidArgumentException(sprintf( - 'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface', - $type, - __NAMESPACE__ - )); - } + return match (true) { + in_array(AbstractSessionArrayStorage::class, class_parents($type)) => static::createSessionArrayStorage($type, $options), + $type === ArrayStorage::class, in_array(ArrayStorage::class, class_parents($type)) => static::createArrayStorage($type, $options), + in_array(StorageInterface::class, class_implements($type)) => new $type($options), + default => throw new Exception\InvalidArgumentException(sprintf( + 'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface', + $type, + __NAMESPACE__ + )), + }; } /** @@ -99,7 +92,7 @@ protected static function createArrayStorage($type, $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be an array; received "%s"', $type, - is_object($options['input']) ? get_class($options['input']) : gettype($options['input']) + get_debug_type($options['input']) )); } $input = $options['input']; @@ -114,9 +107,7 @@ protected static function createArrayStorage($type, $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "iterator_class" option to be a valid class; received "%s"', $type, - is_object($options['iterator_class']) - ? get_class($options['iterator_class']) - : gettype($options['iterator_class']) + get_debug_type($options['iterator_class']) )); } $iteratorClass = $options['iterator_class']; @@ -129,7 +120,6 @@ protected static function createArrayStorage($type, $options) * Create a storage object from a class extending AbstractSessionArrayStorage * * @param string $type - * @param array $options * @return AbstractSessionArrayStorage * @throws Exception\InvalidArgumentException If the input option is invalid. */ @@ -145,7 +135,7 @@ protected static function createSessionArrayStorage($type, array $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"', $type, - is_object($input) ? $input::class : gettype($input) + get_debug_type($input) )); } } diff --git a/src/Validator/Id.php b/src/Validator/Id.php index 71eace4b..eea5316d 100644 --- a/src/Validator/Id.php +++ b/src/Validator/Id.php @@ -58,19 +58,11 @@ public function isValid() // Get the session id bits per character INI setting, using 5 if unavailable $hashBitsPerChar = ini_get('session.sid_bits_per_character') ?: 5; - switch ($hashBitsPerChar) { - case 4: - $pattern = '#^[0-9a-f]*$#'; - break; - case 6: - $pattern = '#^[0-9a-zA-Z-,]*$#'; - break; - case 5: - // intentionally fall-through - default: - $pattern = '#^[0-9a-v]*$#'; - break; - } + $pattern = match ($hashBitsPerChar) { + 4 => '#^[0-9a-f]*$#', + 6 => '#^[0-9a-zA-Z-,]*$#', + default => '#^[0-9a-v]*$#', + }; return (bool) preg_match($pattern, $id); } diff --git a/src/Validator/RemoteAddr.php b/src/Validator/RemoteAddr.php index a3e2f1e8..e63ed938 100644 --- a/src/Validator/RemoteAddr.php +++ b/src/Validator/RemoteAddr.php @@ -92,7 +92,6 @@ public static function getUseProxy() /** * Set list of trusted proxy addresses * - * @param array $trustedProxies * @return void */ public static function setTrustedProxies(array $trustedProxies) diff --git a/src/ValidatorChain.php b/src/ValidatorChain.php index 30709858..50f24974 100644 --- a/src/ValidatorChain.php +++ b/src/ValidatorChain.php @@ -12,15 +12,10 @@ class ValidatorChain extends EventManager { - /** @var StorageInterface */ - protected $storage; - - public function __construct(StorageInterface $storage) + public function __construct(protected StorageInterface $storage) { parent::__construct(); - - $this->storage = $storage; - $validators = $storage->getMetadata('_VALID'); + $validators = $storage->getMetadata('_VALID'); if ($validators) { foreach ($validators as $validator => $data) { $this->attachValidator('session.validate', [new $validator($data), 'isValid'], 1); diff --git a/test/Config/SessionConfigTest.php b/test/Config/SessionConfigTest.php index 5331a3c9..28ea08eb 100644 --- a/test/Config/SessionConfigTest.php +++ b/test/Config/SessionConfigTest.php @@ -814,12 +814,10 @@ public function testRememberMeSecondsIsMutable(): void } // setOption - /** * @dataProvider optionsProvider - * @param mixed $value */ - public function testSetOptionSetsIniSetting(string $option, string $getter, $value): void + public function testSetOptionSetsIniSetting(string $option, string $getter, mixed $value): void { // Leaving out special cases. if ($option === 'remember_me_seconds' || $option === 'url_rewriter_tags') { @@ -852,15 +850,13 @@ public function testSetOptionsThrowsExceptionOnInvalidKey(): void } // setOptions - /** * @dataProvider optionsProvider - * @param mixed $value */ public function testSetOptionsTranslatesUnderscoreSeparatedKeys( string $option, string $getter, - $value + mixed $value ): void { $options = [$option => $value]; $this->config->setOptions($options); diff --git a/test/Config/StandardConfigTest.php b/test/Config/StandardConfigTest.php index 401e5704..3bdcf23c 100644 --- a/test/Config/StandardConfigTest.php +++ b/test/Config/StandardConfigTest.php @@ -460,12 +460,10 @@ public function testSettingInvalidRememberMeSecondsRaisesException2(): void } // setOptions - /** * @dataProvider optionsProvider - * @param mixed $value */ - public function testSetOptionsTranslatesUnderscoreSeparatedKeys(string $option, string $getter, $value): void + public function testSetOptionsTranslatesUnderscoreSeparatedKeys(string $option, string $getter, mixed $value): void { $options = [$option => $value]; $this->config->setOptions($options); diff --git a/test/SaveHandler/CacheTest.php b/test/SaveHandler/CacheTest.php index 1b1cb8bd..677c68aa 100644 --- a/test/SaveHandler/CacheTest.php +++ b/test/SaveHandler/CacheTest.php @@ -46,7 +46,7 @@ public function testReadWrite(): void $cacheStorage->expects(self::any()) ->method('setItem') ->with('242', self::anything()) - ->willReturnCallback(function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { + ->willReturnCallback(static function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { $cacheStorage->expects(self::any()) ->method('getItem') ->with('242') @@ -74,7 +74,7 @@ public function testReadWriteComplex(): void $cacheStorage->expects(self::any()) ->method('setItem') ->with('242', self::anything()) - ->willReturnCallback(function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { + ->willReturnCallback(static function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { $cacheStorage->expects(self::any()) ->method('getItem') ->with('242') @@ -97,7 +97,7 @@ public function testReadWriteTwice(): void $cacheStorage->expects(self::exactly(2)) ->method('setItem') ->with('242', self::anything()) - ->willReturnCallback(function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { + ->willReturnCallback(static function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { $cacheStorage->expects(self::any()) ->method('getItem') ->with('242') @@ -149,7 +149,7 @@ public function testDestroyReturnsTrueWhenSessionIsDeleted(): void $cacheStorage->expects(self::any()) ->method('setItem') ->with('242', self::anything()) - ->willReturnCallback(function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { + ->willReturnCallback(static function (string $firstArgs, string $secondArgs) use ($cacheStorage): bool { $cacheStorage->expects(self::any()) ->method('getItem') ->with('242') diff --git a/test/Service/SessionManagerFactoryTest.php b/test/Service/SessionManagerFactoryTest.php index 0adbe3a5..87203be1 100644 --- a/test/Service/SessionManagerFactoryTest.php +++ b/test/Service/SessionManagerFactoryTest.php @@ -199,7 +199,7 @@ public function testFactoryDoesNotAttachValidatorTwoTimes(): void $manager = $this->services->get(ManagerInterface::class); try { $manager->start(); - } catch (RuntimeException $e) { + } catch (RuntimeException) { // Ignore exception, because we are not interested whether session validation passes in this test } diff --git a/test/SessionManagerTest.php b/test/SessionManagerTest.php index 48426746..bd99bd65 100644 --- a/test/SessionManagerTest.php +++ b/test/SessionManagerTest.php @@ -857,19 +857,13 @@ public function testIdValidationWillFailOnInvalidData(): void $this->manager->start(); } - /** - * @param mixed $expected - */ - private function assertAttributeEquals($expected, string $property, object $object): void + private function assertAttributeEquals(mixed $expected, string $property, object $object): void { $value = $this->getReflectionProperty($object, $property); self::assertEquals($expected, $value); } - /** - * @param mixed $expected - */ - private function assertAttributeContains($expected, string $property, object $object): void + private function assertAttributeContains(mixed $expected, string $property, object $object): void { $value = $this->getReflectionProperty($object, $property); self::assertContains($expected, $value); From 75bc902bc3ae43a362de7832c75883ab93452944 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 16 Nov 2022 02:34:28 +0700 Subject: [PATCH 2/3] Fix cs and rollback numeric change for match -> switch Signed-off-by: Abdul Malik Ikhsan --- src/AbstractContainer.php | 4 ++-- src/Config/SessionConfig.php | 12 +++++++++++- src/SessionManager.php | 8 ++++++-- src/Validator/Id.php | 18 +++++++++++++----- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/AbstractContainer.php b/src/AbstractContainer.php index 2c6416c9..80b525fa 100644 --- a/src/AbstractContainer.php +++ b/src/AbstractContainer.php @@ -536,7 +536,7 @@ public function setExpirationSeconds($ttl, $vars = null) // Map item keys => timestamp $expires = array_flip($expires); - $expires = array_map(static fn(): int => $ts, $expires); + $expires = array_map(static fn() => $ts, $expires); // Create metadata array to merge in $data = ['EXPIRE_KEYS' => $expires]; @@ -583,7 +583,7 @@ public function setExpirationHops($hops, $vars = null) // Map item keys => timestamp $expires = array_flip($expires); - $expires = array_map(static fn(): array => ['hops' => $hops, 'ts' => $ts], $expires); + $expires = array_map(static fn() => ['hops' => $hops, 'ts' => $ts], $expires); // Create metadata array to merge in $data = ['EXPIRE_HOPS_KEYS' => $expires]; diff --git a/src/Config/SessionConfig.php b/src/Config/SessionConfig.php index c417ba9e..04e75362 100644 --- a/src/Config/SessionConfig.php +++ b/src/Config/SessionConfig.php @@ -202,10 +202,20 @@ public function setStorageOption($storageName, $storageValue) public function getStorageOption($storageOption) { return match ($storageOption) { + // No remote storage option; just return the current value 'remember_me_seconds' => $this->rememberMeSeconds, + 'url_rewriter_tags' => ini_get('url_rewriter.tags'), - 'use_cookies', 'use_only_cookies', 'use_trans_sid', 'cookie_httponly' => (bool) ini_get('session.' . $storageOption), + + // The following all need a transformation on the retrieved value; + // however they use the same key naming scheme + 'use_cookies', + 'use_only_cookies', + 'use_trans_sid', + 'cookie_httponly' => (bool) ini_get('session.' . $storageOption), + 'save_handler' => $this->saveHandler ?: $this->sessionModuleName(), + default => ini_get('session.' . $storageOption), }; } diff --git a/src/SessionManager.php b/src/SessionManager.php index 707e12d8..ef7d967a 100644 --- a/src/SessionManager.php +++ b/src/SessionManager.php @@ -443,8 +443,12 @@ public function expireSessionCookie() } setcookie( $this->getName(), // session name - '', - ['expires' => $_SERVER['REQUEST_TIME'] - 42000, 'path' => $config->getCookiePath(), 'domain' => $config->getCookieDomain(), 'secure' => (bool) $config->getCookieSecure(), 'httponly' => (bool) $config->getCookieHttpOnly()] + '', // value + $_SERVER['REQUEST_TIME'] - 42000, // TTL for cookie + $config->getCookiePath(), + $config->getCookieDomain(), + (bool) $config->getCookieSecure(), + (bool) $config->getCookieHttpOnly() ); } diff --git a/src/Validator/Id.php b/src/Validator/Id.php index eea5316d..71eace4b 100644 --- a/src/Validator/Id.php +++ b/src/Validator/Id.php @@ -58,11 +58,19 @@ public function isValid() // Get the session id bits per character INI setting, using 5 if unavailable $hashBitsPerChar = ini_get('session.sid_bits_per_character') ?: 5; - $pattern = match ($hashBitsPerChar) { - 4 => '#^[0-9a-f]*$#', - 6 => '#^[0-9a-zA-Z-,]*$#', - default => '#^[0-9a-v]*$#', - }; + switch ($hashBitsPerChar) { + case 4: + $pattern = '#^[0-9a-f]*$#'; + break; + case 6: + $pattern = '#^[0-9a-zA-Z-,]*$#'; + break; + case 5: + // intentionally fall-through + default: + $pattern = '#^[0-9a-v]*$#'; + break; + } return (bool) preg_match($pattern, $id); } From c5f3ff9329e8cd412e3afc780e585f5133722c89 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 16 Nov 2022 02:39:03 +0700 Subject: [PATCH 3/3] pslam general object return Signed-off-by: Abdul Malik Ikhsan --- src/Storage/Factory.php | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Storage/Factory.php b/src/Storage/Factory.php index ab6217bd..57362820 100644 --- a/src/Storage/Factory.php +++ b/src/Storage/Factory.php @@ -14,9 +14,12 @@ use function class_exists; use function class_implements; use function class_parents; +use function get_class; use function get_debug_type; +use function gettype; use function in_array; use function is_array; +use function is_object; use function is_string; use function sprintf; @@ -62,16 +65,21 @@ public static function factory($type, $options = []) )); } - return match (true) { - in_array(AbstractSessionArrayStorage::class, class_parents($type)) => static::createSessionArrayStorage($type, $options), - $type === ArrayStorage::class, in_array(ArrayStorage::class, class_parents($type)) => static::createArrayStorage($type, $options), - in_array(StorageInterface::class, class_implements($type)) => new $type($options), - default => throw new Exception\InvalidArgumentException(sprintf( - 'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface', - $type, - __NAMESPACE__ - )), - }; + switch (true) { + case in_array(AbstractSessionArrayStorage::class, class_parents($type)): + return static::createSessionArrayStorage($type, $options); + case $type === ArrayStorage::class: + case in_array(ArrayStorage::class, class_parents($type)): + return static::createArrayStorage($type, $options); + case in_array(StorageInterface::class, class_implements($type)): + return new $type($options); + default: + throw new Exception\InvalidArgumentException(sprintf( + 'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface', + $type, + __NAMESPACE__ + )); + } } /** @@ -92,7 +100,7 @@ protected static function createArrayStorage($type, $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be an array; received "%s"', $type, - get_debug_type($options['input']) + is_object($options['input']) ? get_class($options['input']) : gettype($options['input']) )); } $input = $options['input']; @@ -107,7 +115,9 @@ protected static function createArrayStorage($type, $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "iterator_class" option to be a valid class; received "%s"', $type, - get_debug_type($options['iterator_class']) + is_object($options['iterator_class']) + ? get_class($options['iterator_class']) + : gettype($options['iterator_class']) )); } $iteratorClass = $options['iterator_class']; @@ -120,6 +130,7 @@ protected static function createArrayStorage($type, $options) * Create a storage object from a class extending AbstractSessionArrayStorage * * @param string $type + * @param array $options * @return AbstractSessionArrayStorage * @throws Exception\InvalidArgumentException If the input option is invalid. */ @@ -135,7 +146,7 @@ protected static function createSessionArrayStorage($type, array $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"', $type, - get_debug_type($input) + is_object($input) ? $input::class : gettype($input) )); } }