Skip to content

Commit

Permalink
Merge pull request #64 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Nov 15, 2022
2 parents 49964c4 + c5f3ff9 commit 1759578
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ 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);
Expand Down Expand Up @@ -579,7 +579,7 @@ 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);
Expand Down
8 changes: 1 addition & 7 deletions src/AbstractManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -105,8 +101,6 @@ public function __construct(
if ($saveHandler !== null) {
$this->saveHandler = $saveHandler;
}

$this->validators = $validators;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 17 additions & 20 deletions src/Config/SessionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,26 +201,23 @@ 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');
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'),

// 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);
}
'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),
};
}

/**
Expand Down Expand Up @@ -445,7 +442,7 @@ private function locateRegisteredSaveHandlers()

$content = array_shift($matches);

$handlers = false !== strpos($content, '</td>')
$handlers = str_contains($content, '</td>')
? $this->parseSaveHandlersFromHtml($content)
: $this->parseSaveHandlersFromPlainText($content);

Expand Down
28 changes: 10 additions & 18 deletions src/SaveHandler/DbTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

/**
Expand Down
13 changes: 2 additions & 11 deletions src/SaveHandler/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
*/
class MongoDB implements SaveHandlerInterface
{
/**
* MongoClient instance
*
* @var MongoClient
*/
protected $mongoClient;

/**
* MongoCollection instance
*
Expand Down Expand Up @@ -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');
Expand All @@ -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;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/SaveHandler/MongoDBOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions src/Service/SessionManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
));
}
}
Expand All @@ -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)
));
}
}
Expand All @@ -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)
));
}
}
Expand Down
26 changes: 8 additions & 18 deletions src/Storage/AbstractSessionArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,39 @@ 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);
}

/**
* 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);
}

/**
* Isset Offset
*
* @param mixed $key
* @return bool
*/
public function __isset($key)
public function __isset(mixed $key)
{
return $this->offsetExists($key);
}

/**
* Unset Offset
*
* @param mixed $key
* @return void
*/
public function __unset($key)
public function __unset(mixed $key)
{
$this->offsetUnset($key);
}
Expand All @@ -121,23 +116,21 @@ public function __destruct()
/**
* Offset Exists
*
* @param mixed $key
* @return bool
*/
#[ReturnTypeWillChange]
public function offsetExists($key)
public function offsetExists(mixed $key)
{
return isset($_SESSION[$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];
Expand All @@ -149,24 +142,21 @@ 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;
}

/**
* Offset Unset
*
* @param mixed $key
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($key)
public function offsetUnset(mixed $key)
{
unset($_SESSION[$key]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Storage/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -38,7 +39,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)) {
Expand All @@ -60,7 +61,7 @@ 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)
));
}

Expand Down
1 change: 0 additions & 1 deletion src/Validator/RemoteAddr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions src/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 1759578

Please sign in to comment.