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

refactor: add PHPDoc types in RouteCollection #7129

Merged
merged 4 commits into from
Jan 21, 2023
Merged
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
28 changes: 23 additions & 5 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ class RouteCollection implements RouteCollectionInterface
* verb => [
* routeName => [
* 'route' => [
* routeKey(or from) => handler,
* ]
* routeKey(regex) => handler,
* ],
* 'redirect' => statusCode,
* ]
* ],
* ]
Expand All @@ -138,7 +139,7 @@ class RouteCollection implements RouteCollectionInterface
*
* [
* verb => [
* routeKey(or from) => [
* routeKey(regex) => [
* key => value,
* ]
* ],
Expand Down Expand Up @@ -527,6 +528,8 @@ public function getRoutes(?string $verb = null): array

/**
* Returns one or all routes options
*
* @return array<string, int|string> [key => value]
*/
public function getRoutesOptions(?string $from = null, ?string $verb = null): array
{
Expand Down Expand Up @@ -1149,20 +1152,25 @@ public function getFilterForRoute(string $search, ?string $verb = null): string
* 'role:admin,manager'
*
* has a filter of "role", with parameters of ['admin', 'manager'].
*
* @param string $search routeKey
*
* @return array<int, string> filter_name or filter_name:arguments like 'role:admin,manager'
* @phpstan-return list<string>
*/
public function getFiltersForRoute(string $search, ?string $verb = null): array
{
$options = $this->loadRoutesOptions($verb);

if (! array_key_exists($search, $options)) {
if (! array_key_exists($search, $options) || ! array_key_exists('filter', $options[$search])) {
return [];
}

if (is_string($options[$search]['filter'])) {
return [$options[$search]['filter']];
}

return $options[$search]['filter'] ?? [];
return $options[$search]['filter'];
}

/**
Expand Down Expand Up @@ -1520,6 +1528,16 @@ public function resetRoutes()

/**
* Load routes options based on verb
*
* @return array<string, array<string, array|int|string>> [routeKey(or from) => [key => value]]
* @phpstan-return array<
* string,
* array{
* filter?: string|list<string>, namespace?: string, hostname?: string,
* subdomain?: string, offset?: int, priority?: int, as?: string,
* redirect?: string
* }
* >
*/
protected function loadRoutesOptions(?string $verb = null): array
{
Expand Down