diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 917586c..c24c750 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -13,7 +13,7 @@
$route
- function ($matched, $route) use ($path, $method) {
+ static function ($matched, $route) use ($path, $method) {
$allowedMethods
@@ -114,10 +114,7 @@
- function () {
- function () {
- function () {
- function () {
+ fn()
$container
@@ -150,9 +147,7 @@
- function () {
- function () {
- function () {
+ fn()
$method
@@ -279,8 +274,8 @@
-
- function () {
+
+ fn()
$expected
diff --git a/src/FastRouteRouter.php b/src/FastRouteRouter.php
index 04e0c27..714ad65 100644
--- a/src/FastRouteRouter.php
+++ b/src/FastRouteRouter.php
@@ -18,6 +18,7 @@
use function array_reduce;
use function array_reverse;
use function array_unique;
+use function assert;
use function dirname;
use function file_exists;
use function file_put_contents;
@@ -74,56 +75,46 @@ class FastRouteRouter implements RouterInterface
/**
* Cache generated route data?
- *
- * @var bool
*/
- private $cacheEnabled = false;
+ private bool $cacheEnabled = false;
/**
* Cache file path relative to the project directory.
- *
- * @var string
*/
- private $cacheFile = 'data/cache/fastroute.php.cache';
+ private string $cacheFile = 'data/cache/fastroute.php.cache';
/** @var callable A factory callback that can return a dispatcher. */
private $dispatcherCallback;
/**
* Cached data used by the dispatcher.
- *
- * @var array
*/
- private $dispatchData = [];
+ private array $dispatchData = [];
/**
* True if cache is enabled and valid dispatch data has been loaded from
* cache.
- *
- * @var bool
*/
- private $hasCache = false;
+ private bool $hasCache = false;
/**
* FastRoute router
- *
- * @var RouteCollector
*/
- private $router;
+ private ?RouteCollector $router = null;
/**
* All attached routes as Route instances
*
* @var Route[]
*/
- private $routes = [];
+ private array $routes = [];
/**
* Routes to inject into the underlying RouteCollector.
*
* @var Route[]
*/
- private $routesToInject = [];
+ private array $routesToInject = [];
/**
* Constructor
@@ -361,9 +352,7 @@ private function getDispatcher($data): Dispatcher
*/
private function createDispatcherCallback(): callable
{
- return function ($data) {
- return new GroupCountBased($data);
- };
+ return static fn($data): GroupCountBased => new GroupCountBased($data);
}
/**
@@ -387,7 +376,7 @@ private function marshalFailedRoute(array $result): RouteResult
private function marshalMatchedRoute(array $result, string $method): RouteResult
{
$path = $result[1];
- $route = array_reduce($this->routes, function ($matched, $route) use ($path, $method) {
+ $route = array_reduce($this->routes, static function ($matched, $route) use ($path, $method) {
if ($matched) {
return $matched;
}
@@ -447,6 +436,8 @@ private function injectRoute(Route $route): void
$methods = self::HTTP_METHODS_STANDARD;
}
+ assert($this->router instanceof RouteCollector);
+
$this->router->addRoute($methods, $route->getPath(), $route->getPath());
}
@@ -462,6 +453,8 @@ private function getDispatchData(): array
return $this->dispatchData;
}
+ assert($this->router instanceof RouteCollector);
+
$dispatchData = (array) $this->router->getData();
if ($this->cacheEnabled) {
@@ -479,7 +472,7 @@ private function getDispatchData(): array
*/
private function loadDispatchData(): void
{
- set_error_handler(function () {
+ set_error_handler(static function (): void {
}, E_WARNING); // suppress php warnings
$dispatchData = include $this->cacheFile;
restore_error_handler();
@@ -545,7 +538,7 @@ private function cacheDispatchData(array $dispatchData)
private function marshalMethodNotAllowedResult(array $result): RouteResult
{
$path = $result[1];
- $allowedMethods = array_reduce($this->routes, function ($allowedMethods, $route) use ($path) {
+ $allowedMethods = array_reduce($this->routes, static function ($allowedMethods, $route) use ($path) {
if ($path !== $route->getPath()) {
return $allowedMethods;
}
diff --git a/test/FastRouteRouter/ConfigProviderTest.php b/test/FastRouteRouter/ConfigProviderTest.php
index 0daa327..be7fc2e 100644
--- a/test/FastRouteRouter/ConfigProviderTest.php
+++ b/test/FastRouteRouter/ConfigProviderTest.php
@@ -11,8 +11,7 @@
class ConfigProviderTest extends TestCase
{
- /** @var ConfigProvider */
- private $provider;
+ private ConfigProvider $provider;
protected function setUp(): void
{
diff --git a/test/FastRouteRouterFactoryTest.php b/test/FastRouteRouterFactoryTest.php
index c5b193a..4d630fd 100644
--- a/test/FastRouteRouterFactoryTest.php
+++ b/test/FastRouteRouterFactoryTest.php
@@ -16,11 +16,10 @@ class FastRouteRouterFactoryTest extends TestCase
{
use ProphecyTrait;
- /** @var FastRouteRouterFactory */
- private $factory;
+ private FastRouteRouterFactory $factory;
/** @var ObjectProphecy */
- private $container;
+ private ObjectProphecy $container;
protected function setUp(): void
{
@@ -35,14 +34,10 @@ public function testCreatesRouterWithEmptyConfig()
$router = ($this->factory)($this->container->reveal());
$this->assertInstanceOf(FastRouteRouter::class, $router);
- $cacheEnabled = Closure::bind(function () {
- return $this->cacheEnabled;
- }, $router, FastRouteRouter::class)();
+ $cacheEnabled = Closure::bind(fn() => $this->cacheEnabled, $router, FastRouteRouter::class)();
$this->assertFalse($cacheEnabled);
- $cacheFile = Closure::bind(function () {
- return $this->cacheFile;
- }, $router, FastRouteRouter::class)();
+ $cacheFile = Closure::bind(fn() => $this->cacheFile, $router, FastRouteRouter::class)();
$this->assertSame('data/cache/fastroute.php.cache', $cacheFile);
}
@@ -62,14 +57,10 @@ public function testCreatesRouterWithConfig()
$this->assertInstanceOf(FastRouteRouter::class, $router);
- $cacheEnabled = Closure::bind(function () {
- return $this->cacheEnabled;
- }, $router, FastRouteRouter::class)();
+ $cacheEnabled = Closure::bind(fn() => $this->cacheEnabled, $router, FastRouteRouter::class)();
$this->assertTrue($cacheEnabled);
- $cacheFile = Closure::bind(function () {
- return $this->cacheFile;
- }, $router, FastRouteRouter::class)();
+ $cacheFile = Closure::bind(fn() => $this->cacheFile, $router, FastRouteRouter::class)();
$this->assertSame('/foo/bar/file-cache', $cacheFile);
}
}
diff --git a/test/FastRouteRouterTest.php b/test/FastRouteRouterTest.php
index e996da9..2612f94 100644
--- a/test/FastRouteRouterTest.php
+++ b/test/FastRouteRouterTest.php
@@ -45,9 +45,7 @@ protected function setUp(): void
{
$this->fastRouter = $this->prophesize(RouteCollector::class);
$this->dispatcher = $this->prophesize(Dispatcher::class);
- $this->dispatchCallback = function () {
- return $this->dispatcher->reveal();
- };
+ $this->dispatchCallback = fn() => $this->dispatcher->reveal();
}
private function getRouter(): FastRouteRouter
@@ -66,9 +64,7 @@ private function getMiddleware(): MiddlewareInterface
public function testWillLazyInstantiateAFastRouteCollectorIfNoneIsProvidedToConstructor(): void
{
$router = new FastRouteRouter();
- $routeCollector = Closure::bind(function () {
- return $this->router;
- }, $router, FastRouteRouter::class)();
+ $routeCollector = Closure::bind(fn() => $this->router, $router, FastRouteRouter::class)();
$this->assertInstanceOf(RouteCollector::class, $routeCollector);
}
@@ -79,9 +75,7 @@ public function testAddingRouteAggregatesRoute(): void
$router = $this->getRouter();
$router->addRoute($route);
- $routesToInject = Closure::bind(function () {
- return $this->routesToInject;
- }, $router, FastRouteRouter::class)();
+ $routesToInject = Closure::bind(fn() => $this->routesToInject, $router, FastRouteRouter::class)();
$this->assertContains($route, $routesToInject);
}
@@ -104,9 +98,7 @@ public function testMatchingInjectsRouteIntoFastRoute(): void
$uri->getPath()->willReturn('/foo');
$request = $this->prophesize(ServerRequestInterface::class);
- $request->getUri()->will(function () use ($uri) {
- return $uri->reveal();
- });
+ $request->getUri()->will(fn(): object => $uri->reveal());
$request->getMethod()->willReturn(RequestMethod::METHOD_GET);
$router->match($request->reveal());
@@ -636,9 +628,7 @@ public function createServerRequestProphecy(
$uri->getPath()->willReturn($path);
$request = $this->prophesize(ServerRequestInterface::class);
- $request->getUri()->will(function () use ($uri) {
- return $uri->reveal();
- });
+ $request->getUri()->will(fn(): object => $uri->reveal());
$request->getMethod()->willReturn($method);
diff --git a/test/UriGeneratorTest.php b/test/UriGeneratorTest.php
index f0427f5..587cf03 100644
--- a/test/UriGeneratorTest.php
+++ b/test/UriGeneratorTest.php
@@ -28,8 +28,7 @@ class UriGeneratorTest extends TestCase
/** @var callable */
private $dispatchCallback;
- /** @var FastRouteRouter */
- private $router;
+ private FastRouteRouter $router;
/**
* Test routes taken from https://github.com/nikic/FastRoute/blob/master/test/RouteParser/StdTest.php
@@ -70,7 +69,7 @@ public function provideRouteTests(): array
['/test/{param:\d+}', ['param' => 1], '/test/1'],
//['/test/{param:\d+}', ['param' => 'foo'], 'exception', null],
['/test/{ param : \d{1,9} }', ['param' => 1], '/test/1'],
- ['/test/{ param : \d{1,9} }', ['param' => 123456789], '/test/123456789'],
+ ['/test/{ param : \d{1,9} }', ['param' => 123_456_789], '/test/123456789'],
['/test/{ param : \d{1,9} }', ['param' => 0], '/test/0'],
['/test[opt]', [], '/testopt'],
['/test[/{param}]', [], '/test'],
@@ -106,7 +105,7 @@ public function exceptionalRoutes(): iterable
],
[
'/test/{ param : \d{1,9} }',
- ['param' => 1234567890],
+ ['param' => 1_234_567_890],
RuntimeException::class,
'Parameter value for [param] did not match the regex `\d{1,9}`',
],
@@ -123,9 +122,7 @@ protected function setUp(): void
{
$this->fastRouter = $this->prophesize(RouteCollector::class);
$this->dispatcher = $this->prophesize(Dispatcher::class);
- $this->dispatchCallback = function () {
- return $this->dispatcher->reveal();
- };
+ $this->dispatchCallback = fn() => $this->dispatcher->reveal();
$this->router = new FastRouteRouter(
$this->fastRouter->reveal(),