Skip to content

Commit

Permalink
Merge pull request #23 from samsonasik/apply-php74
Browse files Browse the repository at this point in the history
Apply PHP 7.4 syntax and typed property
  • Loading branch information
Ocramius committed Sep 20, 2022
2 parents d8ad605 + aa2caf1 commit 7ce0f00
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 72 deletions.
15 changes: 5 additions & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<code>$route</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="1">
<code>function ($matched, $route) use ($path, $method) {</code>
<code>static function ($matched, $route) use ($path, $method) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="11">
<code>$allowedMethods</code>
Expand Down Expand Up @@ -114,10 +114,7 @@
</file>
<file src="test/FastRouteRouterFactoryTest.php">
<MissingClosureReturnType occurrences="4">
<code>function () {</code>
<code>function () {</code>
<code>function () {</code>
<code>function () {</code>
<code>fn()</code>
</MissingClosureReturnType>
<MissingPropertyType occurrences="1">
<code>$container</code>
Expand Down Expand Up @@ -150,9 +147,7 @@
</file>
<file src="test/FastRouteRouterTest.php">
<MissingClosureReturnType occurrences="3">
<code>function () {</code>
<code>function () {</code>
<code>function () {</code>
<code>fn()</code>
</MissingClosureReturnType>
<MissingParamType occurrences="2">
<code>$method</code>
Expand Down Expand Up @@ -279,8 +274,8 @@
</UndefinedThisPropertyFetch>
</file>
<file src="test/UriGeneratorTest.php">
<MissingClosureReturnType occurrences="1">
<code>function () {</code>
<MissingClosureReturnType occurrences="2">
<code>fn()</code>
</MissingClosureReturnType>
<MissingParamType occurrences="4">
<code>$expected</code>
Expand Down
39 changes: 16 additions & 23 deletions src/FastRouteRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

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

Expand All @@ -462,6 +453,8 @@ private function getDispatchData(): array
return $this->dispatchData;
}

assert($this->router instanceof RouteCollector);

$dispatchData = (array) $this->router->getData();

if ($this->cacheEnabled) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions test/FastRouteRouter/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

class ConfigProviderTest extends TestCase
{
/** @var ConfigProvider */
private $provider;
private ConfigProvider $provider;

protected function setUp(): void
{
Expand Down
21 changes: 6 additions & 15 deletions test/FastRouteRouterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ class FastRouteRouterFactoryTest extends TestCase
{
use ProphecyTrait;

/** @var FastRouteRouterFactory */
private $factory;
private FastRouteRouterFactory $factory;

/** @var ObjectProphecy<ContainerInterface> */
private $container;
private ObjectProphecy $container;

protected function setUp(): void
{
Expand All @@ -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);
}

Expand All @@ -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);
}
}
20 changes: 5 additions & 15 deletions test/FastRouteRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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());
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 4 additions & 7 deletions test/UriGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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}`',
],
Expand All @@ -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(),
Expand Down

0 comments on commit 7ce0f00

Please sign in to comment.