From 3ad8a1fdb0f7187ab4394fd95d72d691253a9baa Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 18 Jul 2024 16:10:39 +0100 Subject: [PATCH] qa: Adds parameter/property/return types where missing and marks a number of properties as readonly. Signed-off-by: George Steel --- src/Middleware/RouteMiddleware.php | 6 +-- src/Route.php | 2 +- src/RouteCollector.php | 4 +- src/RouteResult.php | 66 +++++++++++++----------------- 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/Middleware/RouteMiddleware.php b/src/Middleware/RouteMiddleware.php index 7f0c5d7..eb5c66e 100644 --- a/src/Middleware/RouteMiddleware.php +++ b/src/Middleware/RouteMiddleware.php @@ -23,12 +23,8 @@ */ final class RouteMiddleware implements MiddlewareInterface { - /** @var RouterInterface */ - protected $router; - - public function __construct(RouterInterface $router) + public function __construct(private readonly RouterInterface $router) { - $this->router = $router; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface diff --git a/src/Route.php b/src/Route.php index 3483645..cb3814a 100644 --- a/src/Route.php +++ b/src/Route.php @@ -39,7 +39,7 @@ final class Route implements MiddlewareInterface public const HTTP_METHOD_SEPARATOR = ':'; /** @var null|list HTTP methods allowed with this route. */ - private ?array $methods; + private readonly ?array $methods; /** @var array Options related to this route to pass to the routing implementation. */ private array $options = []; diff --git a/src/RouteCollector.php b/src/RouteCollector.php index 1027e9b..d3bf313 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -38,8 +38,8 @@ final class RouteCollector implements RouteCollectorInterface private ?DuplicateRouteDetector $duplicateRouteDetector = null; public function __construct( - protected RouterInterface $router, - protected bool $detectDuplicates = true + private readonly RouterInterface $router, + private readonly bool $detectDuplicates = true ) { } diff --git a/src/RouteResult.php b/src/RouteResult.php index 6e25dae..5fff50f 100644 --- a/src/RouteResult.php +++ b/src/RouteResult.php @@ -33,44 +33,38 @@ */ final class RouteResult implements MiddlewareInterface { - /** @var list|null */ - private $allowedMethods = []; - - /** @var array */ - private $matchedParams = []; - - /** @var string|null */ - private $matchedRouteName; - - /** - * Route matched during routing - * - * @since 1.3.0 - * @var Route|null - */ - private $route; - /** * Only allow instantiation via factory methods. * * @param bool $success Success state of routing. + * @param Route|null $route Route matched during routing. + * @param null|string $matchedRouteName The name of the matched route. Null if routing failed. + * @param null|list $allowedMethods Methods allowed by the route, or null if all methods are allowed. + * @param array $matchedParams Matched routing parameters for successful routes. */ - private function __construct(private bool $success) - { + private function __construct( + private readonly bool $success, + private readonly ?Route $route, + private readonly ?string $matchedRouteName, + private readonly ?array $allowedMethods, + private readonly array $matchedParams = [], + ) { } /** - * Create an instance representing a route succes from the matching route. + * Create an instance representing a route success from the matching route. * * @param array $params Parameters associated with the matched route, if any. */ public static function fromRoute(Route $route, array $params = []): self { - $result = new self(true); - $result->route = $route; - $result->matchedParams = $params; - - return $result; + return new self( + true, + $route, + $route->getName(), + $route->getAllowedMethods(), + $params, + ); } /** @@ -81,10 +75,13 @@ public static function fromRoute(Route $route, array $params = []): self */ public static function fromRouteFailure(?array $methods): self { - $result = new self(false); - $result->allowedMethods = $methods; - - return $result; + return new self( + false, + null, + null, + $methods, + [], + ); } /** @@ -120,7 +117,7 @@ public function isSuccess(): bool * * @return false|Route false if representing a routing failure; Route instance otherwise. */ - public function getMatchedRoute() + public function getMatchedRoute(): Route|false { return $this->route ?? false; } @@ -130,19 +127,14 @@ public function getMatchedRoute() * * If this result represents a failure, return false; otherwise, return the * matched route name. - * - * @return false|string */ - public function getMatchedRouteName() + public function getMatchedRouteName(): false|string { if ($this->isFailure()) { return false; } - if ($this->matchedRouteName === null) { - assert($this->route !== null); - $this->matchedRouteName = $this->route->getName(); - } + assert($this->matchedRouteName !== null); return $this->matchedRouteName; }