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

Minor refactor of RouteResult - add types where missing #70

Merged
merged 1 commit into from
Jul 18, 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
6 changes: 1 addition & 5 deletions src/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class Route implements MiddlewareInterface
public const HTTP_METHOD_SEPARATOR = ':';

/** @var null|list<string> 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 = [];
Expand Down
4 changes: 2 additions & 2 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down
66 changes: 29 additions & 37 deletions src/RouteResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,38 @@
*/
final class RouteResult implements MiddlewareInterface
{
/** @var list<string>|null */
private $allowedMethods = [];

/** @var array<string, mixed> */
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<string> $allowedMethods Methods allowed by the route, or null if all methods are allowed.
* @param array<string, mixed> $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<string, mixed> $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,
);
}

/**
Expand All @@ -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,
[],
);
}

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