Skip to content

Commit

Permalink
Centralise array shape definitions
Browse files Browse the repository at this point in the history
This makes things much less error-prone.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Jan 8, 2024
1 parent 9af18eb commit ca2ea1b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

namespace FastRoute;

/** @phpstan-import-type RouteData from DataGenerator */
interface Cache
{
/**
* @param callable():array{0: array<string, array<string, mixed>>, 1: array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>>} $loader
* @param callable():RouteData $loader
*
* @return array{0: array<string, array<string, mixed>>, 1: array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>>}
* @return RouteData
*/
public function get(string $key, callable $loader): array;
}
8 changes: 7 additions & 1 deletion src/DataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

namespace FastRoute;

/**
* @phpstan-type StaticRoutes array<string, array<string, mixed>>
* @phpstan-type DynamicRouteChunk array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}
* @phpstan-type DynamicRoutes array<string, list<DynamicRouteChunk>>
* @phpstan-type RouteData array{StaticRoutes, DynamicRoutes}
*/
interface DataGenerator
{
/**
Expand All @@ -21,7 +27,7 @@ public function addRoute(string $httpMethod, array $routeData, mixed $handler):
* Returns dispatcher data in some unspecified format, which
* depends on the used method of dispatch.
*
* @return array{0: array<string, array<string, mixed>>, 1: array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>>}
* @return RouteData
*/
public function getData(): array;
}
12 changes: 9 additions & 3 deletions src/DataGenerator/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use function max;
use function round;

/**
* @phpstan-import-type StaticRoutes from DataGenerator
* @phpstan-import-type DynamicRouteChunk from DataGenerator
* @phpstan-import-type DynamicRoutes from DataGenerator
* @phpstan-import-type RouteData from DataGenerator
*/
abstract class RegexBasedAbstract implements DataGenerator
{
/** @var array<string, array<string, mixed>> */
/** @var StaticRoutes */
protected array $staticRoutes = [];

/** @var array<string, array<string, Route>> */
Expand All @@ -29,7 +35,7 @@ abstract protected function getApproxChunkSize(): int;
/**
* @param array<string, Route> $regexToRoutesMap
*
* @return array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}
* @return DynamicRouteChunk
*/
abstract protected function processChunk(array $regexToRoutesMap): array;

Expand All @@ -53,7 +59,7 @@ public function getData(): array
return [$this->staticRoutes, $this->generateVariableRouteData()];
}

/** @return array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>> */
/** @return DynamicRoutes */
private function generateVariableRouteData(): array
{
$data = [];
Expand Down
12 changes: 9 additions & 3 deletions src/Dispatcher/RegexBasedAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@

namespace FastRoute\Dispatcher;

use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use FastRoute\Dispatcher\Result\MethodNotAllowed;
use FastRoute\Dispatcher\Result\NotMatched;

/**
* @phpstan-import-type StaticRoutes from DataGenerator
* @phpstan-import-type DynamicRoutes from DataGenerator
* @phpstan-import-type RouteData from DataGenerator
*/
abstract class RegexBasedAbstract implements Dispatcher
{
/** @var array<string, array<string, mixed>> */
/** @var StaticRoutes */
protected array $staticRouteMap = [];

/** @var array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>> */
/** @var DynamicRoutes */
protected array $variableRouteData = [];

/** @param array{0: array<string, array<string, mixed>>, 1: array<string, array<array{regex: string, suffix?: string, routeMap: array<int|string, array{0: mixed, 1: array<string, string>}>}>>} $data */
/** @param RouteData $data */
public function __construct(array $data)
{
[$this->staticRouteMap, $this->variableRouteData] = $data;
Expand Down

0 comments on commit ca2ea1b

Please sign in to comment.