Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest PHP 8.1 syntax #64

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/Http/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
class Literal implements RouteInterface
{
/**
* RouteInterface to match.
*
* @var string
*/
protected $route;

/**
* Default values.
*
Expand All @@ -48,9 +41,13 @@ class Literal implements RouteInterface
* @param string $route
* @param array $defaults
*/
public function __construct($route, array $defaults = [])
{
$this->route = $route;
public function __construct(
/**
* RouteInterface to match.
*/
protected $route,
array $defaults = []
) {
$this->defaults = $defaults;
}

Expand Down
17 changes: 7 additions & 10 deletions src/Http/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
*/
class Method implements RouteInterface
{
/**
* Verb to match.
*
* @var string
*/
protected $verb;

/**
* Default values.
*
Expand All @@ -50,9 +43,13 @@ class Method implements RouteInterface
* @param string $verb
* @param array $defaults
*/
public function __construct($verb, array $defaults = [])
{
$this->verb = $verb;
public function __construct(
/**
* Verb to match.
*/
protected $verb,
array $defaults = []
) {
$this->defaults = $defaults;
}

Expand Down
19 changes: 7 additions & 12 deletions src/Http/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ class Part extends TreeRouteStack implements RouteInterface
*/
protected $route;

/**
* Whether the route may terminate.
*
* @var bool
*/
protected $mayTerminate;

/**
* Child routes.
*
Expand All @@ -58,7 +51,10 @@ class Part extends TreeRouteStack implements RouteInterface
*/
public function __construct(
$route,
$mayTerminate,
/**
* Whether the route may terminate.
*/
protected $mayTerminate,
RoutePluginManager $routePlugins,
?array $childRoutes = null,
?ArrayObject $prototypes = null
Expand All @@ -73,10 +69,9 @@ public function __construct(
throw new Exception\InvalidArgumentException('Base route may not be a part route');
}

$this->route = $route;
$this->mayTerminate = $mayTerminate;
$this->childRoutes = $childRoutes;
$this->prototypes = $prototypes;
$this->route = $route;
$this->childRoutes = $childRoutes;
$this->prototypes = $prototypes;
/** @var PriorityList<string, TRoute> $this->routes */
$this->routes = new PriorityList();
}
Expand Down
5 changes: 1 addition & 4 deletions src/Http/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
class Placeholder implements RouteInterface
{
private array $defaults;

/**
* @internal
* @deprecated Since 3.9.0 This property will be removed or made private in version 4.0
Expand All @@ -27,9 +25,8 @@ class Placeholder implements RouteInterface
*/
public $priority;

public function __construct(array $defaults)
public function __construct(private readonly array $defaults)
{
$this->defaults = $defaults;
}

/**
Expand Down
37 changes: 15 additions & 22 deletions src/Http/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,22 @@
use function rawurldecode;
use function rawurlencode;
use function sprintf;
use function str_contains;
use function str_replace;
use function strlen;
use function strpos;

/**
* Regex route.
*/
class Regex implements RouteInterface
{
/**
* Regex to match.
*
* @var string
*/
protected $regex;

/**
* Default values.
*
* @var array
*/
protected $defaults;

/**
* Specification for URL assembly.
*
* Parameters accepting substitutions should be denoted as "%key%"
*
* @var string
*/
protected $spec;

/**
* List of assembled parameters.
*
Expand All @@ -73,10 +57,19 @@ class Regex implements RouteInterface
* @param string $spec
* @param array $defaults
*/
public function __construct($regex, $spec, array $defaults = [])
{
$this->regex = $regex;
$this->spec = $spec;
public function __construct(
/**
* Regex to match.
*/
protected $regex,
/**
* Specification for URL assembly.
*
* Parameters accepting substitutions should be denoted as "%key%"
*/
protected $spec,
array $defaults = []
) {
$this->defaults = $defaults;
}

Expand Down Expand Up @@ -171,7 +164,7 @@ public function assemble(array $params = [], array $options = [])
foreach ($mergedParams as $key => $value) {
$spec = '%' . $key . '%';

if (strpos($url, $spec) !== false) {
if (str_contains($url, $spec)) {
$url = str_replace($spec, rawurlencode((string) $value), $url);

$this->assembledParams[] = $key;
Expand Down
18 changes: 7 additions & 11 deletions src/Http/RouteMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@
*/
class RouteMatch extends BaseRouteMatch
{
/**
* Length of the matched path.
*
* @var int
*/
protected $length;

/**
* Create a part RouteMatch with given parameters and length.
*
* @param array $params
* @param int $length
*/
public function __construct(array $params, $length = 0)
{
public function __construct(
array $params,
/**
* Length of the matched path.
*/
protected $length = 0
) {
parent::__construct($params);

$this->length = $length;
}

/**
Expand Down
17 changes: 7 additions & 10 deletions src/Http/Scheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
*/
class Scheme implements RouteInterface
{
/**
* Scheme to match.
*
* @var string
*/
protected $scheme;

/**
* Default values.
*
Expand All @@ -46,9 +39,13 @@ class Scheme implements RouteInterface
* @param string $scheme
* @param array $defaults
*/
public function __construct($scheme, array $defaults = [])
{
$this->scheme = $scheme;
public function __construct(
/**
* Scheme to match.
*/
protected $scheme,
array $defaults = []
) {
$this->defaults = $defaults;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Http/TreeRouteStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ public function addPrototype($name, $route)
*/
public function getPrototype($name)
{
if (isset($this->prototypes[$name])) {
return $this->prototypes[$name];
}

return null;
return $this->prototypes[$name] ?? null;
}

/**
Expand Down
31 changes: 12 additions & 19 deletions src/Http/Wildcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@
*/
class Wildcard implements RouteInterface
{
/**
* Delimiter between keys and values.
*
* @var string
*/
protected $keyValueDelimiter;

/**
* Delimiter before parameters.
*
* @var string
*/
protected $paramDelimiter;

/**
* Default values.
*
Expand All @@ -68,11 +54,18 @@ class Wildcard implements RouteInterface
* @param string $paramDelimiter
* @param array $defaults
*/
public function __construct($keyValueDelimiter = '/', $paramDelimiter = '/', array $defaults = [])
{
$this->keyValueDelimiter = $keyValueDelimiter;
$this->paramDelimiter = $paramDelimiter;
$this->defaults = $defaults;
public function __construct(
/**
* Delimiter between keys and values.
*/
protected $keyValueDelimiter = '/',
/**
* Delimiter before parameters.
*/
protected $paramDelimiter = '/',
array $defaults = []
) {
$this->defaults = $defaults;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/RouteInvokableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $container, $no
*/
public function __invoke(ContainerInterface $container, $routeName, ?array $options = null)
{
$options = $options ?? [];
$options ??= [];

if (! class_exists($routeName)) {
throw new ServiceNotCreatedException(sprintf(
Expand Down Expand Up @@ -134,7 +134,7 @@ public function createServiceWithName(ServiceLocatorInterface $container, $norma
*/
public function createService(ServiceLocatorInterface $container, $normalizedName = null, $routeName = null)
{
$routeName = $routeName ?? RouteInterface::class;
$routeName ??= RouteInterface::class;
return $this($container, $routeName, $this->creationOptions);
}

Expand Down
5 changes: 2 additions & 3 deletions src/RoutePluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use Psr\Container\ContainerInterface;

use function array_merge;
use function gettype;
use function is_object;
use function get_debug_type;
use function sprintf;

/**
Expand Down Expand Up @@ -81,7 +80,7 @@ public function validate(mixed $instance)
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'Plugin of type %s is invalid; must implement %s',
is_object($instance) ? $instance::class : gettype($instance),
get_debug_type($instance),
RouteInterface::class
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/RoutePluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RoutePluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, ?array $options = null)
{
$options = $options ?? [];
$options ??= [];
return new RoutePluginManager($container, $options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(ContainerInterface $container, $name, ?array $options =
*/
public function createService(ServiceLocatorInterface $container, $normalizedName = null, $requestedName = null)
{
$requestedName = $requestedName ?? 'Router';
$requestedName ??= 'Router';
return $this($container, $requestedName);
}
}
6 changes: 1 addition & 5 deletions test/Http/TestAsset/DummyRouteWithParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ public function match(RequestInterface $request, $pathOffset = null)
*/
public function assemble(?array $params = null, ?array $options = null)
{
if (isset($params['foo'])) {
return $params['foo'];
}

return '';
return $params['foo'] ?? '';
}
}
6 changes: 1 addition & 5 deletions test/TestAsset/DummyRouteWithParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public function match(RequestInterface $request)
*/
public function assemble(?array $params = null, ?array $options = null)
{
if (isset($params['foo'])) {
return $params['foo'];
}

return '';
return $params['foo'] ?? '';
}
}