From 967b6ac2a48a4d697103b071c349382c028de2a9 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 9 Aug 2020 13:58:24 -0400 Subject: [PATCH] Add type hints for Routes --- src/Routing/Route/MethodMap.php | 17 ++++++---- src/Routing/Route/PrefixRoute.php | 8 ++--- src/Routing/Route/RegexRoute.php | 9 +++--- src/Routing/Route/Route.php | 9 ++---- src/Routing/Route/RouteInterface.php | 8 ++--- src/Routing/Route/StaticRoute.php | 6 ++-- src/Routing/Route/TemplateRoute.php | 32 +++++++++---------- test/tests/unit/Routing/Route/RouteTest.php | 3 +- .../unit/Routing/Route/TemplateRouteTest.php | 22 ++++++------- 9 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/Routing/Route/MethodMap.php b/src/Routing/Route/MethodMap.php index 11a2188..0e49959 100644 --- a/src/Routing/Route/MethodMap.php +++ b/src/Routing/Route/MethodMap.php @@ -8,7 +8,9 @@ use WellRESTed\Dispatching\DispatcherInterface; class MethodMap { + /** @var DispatcherInterface */ private $dispatcher; + /** @var array */ private $map; // ------------------------------------------------------------------------ @@ -39,7 +41,7 @@ class MethodMap * @param string $method * @param mixed $dispatchable */ - public function register($method, $dispatchable) + public function register($method, $dispatchable): void { $methods = explode(",", $method); $methods = array_map("trim", $methods); @@ -90,13 +92,16 @@ class MethodMap // ------------------------------------------------------------------------ - private function addAllowHeader(ResponseInterface $response) + private function addAllowHeader(ResponseInterface $response): ResponseInterface { $methods = join(",", $this->getAllowedMethods()); return $response->withHeader("Allow", $methods); } - private function getAllowedMethods() + /** + * @return string[] + */ + private function getAllowedMethods(): array { $methods = array_keys($this->map); // Add HEAD if GET is allowed and HEAD is not present. @@ -111,16 +116,16 @@ class MethodMap } /** - * @param $middleware + * @param mixed $middleware * @param ServerRequestInterface $request * @param ResponseInterface $response - * @param $next + * @param callable $next * @return ResponseInterface */ private function dispatchMiddleware( $middleware, ServerRequestInterface $request, - ResponseInterface &$response, + ResponseInterface $response, $next ) { return $this->dispatcher->dispatch($middleware, $request, $response, $next); diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php index 37a0d0f..f93c382 100644 --- a/src/Routing/Route/PrefixRoute.php +++ b/src/Routing/Route/PrefixRoute.php @@ -4,12 +4,12 @@ namespace WellRESTed\Routing\Route; class PrefixRoute extends Route { - public function __construct($target, $methodMap) + public function __construct(string $target, MethodMap $methodMap) { parent::__construct(rtrim($target, "*"), $methodMap); } - public function getType() + public function getType(): int { return RouteInterface::TYPE_PREFIX; } @@ -20,7 +20,7 @@ class PrefixRoute extends Route * @param string $requestTarget * @return boolean */ - public function matchesRequestTarget($requestTarget) + public function matchesRequestTarget(string $requestTarget): bool { return strrpos($requestTarget, $this->target, -strlen($requestTarget)) !== false; } @@ -28,7 +28,7 @@ class PrefixRoute extends Route /** * Always returns an empty array. */ - public function getPathVariables() + public function getPathVariables(): array { return []; } diff --git a/src/Routing/Route/RegexRoute.php b/src/Routing/Route/RegexRoute.php index 4afd45a..8f41bdc 100644 --- a/src/Routing/Route/RegexRoute.php +++ b/src/Routing/Route/RegexRoute.php @@ -4,9 +4,10 @@ namespace WellRESTed\Routing\Route; class RegexRoute extends Route { - private $captures; + /** @var array */ + private $captures = []; - public function getType() + public function getType(): int { return RouteInterface::TYPE_PATTERN; } @@ -17,7 +18,7 @@ class RegexRoute extends Route * @param string $requestTarget * @return boolean */ - public function matchesRequestTarget($requestTarget) + public function matchesRequestTarget(string $requestTarget): bool { $this->captures = []; $matched = preg_match($this->getTarget(), $requestTarget, $captures); @@ -36,7 +37,7 @@ class RegexRoute extends Route * @see \preg_match * @return array */ - public function getPathVariables() + public function getPathVariables(): array { return $this->captures; } diff --git a/src/Routing/Route/Route.php b/src/Routing/Route/Route.php index 2cbe7ca..0ba21ce 100644 --- a/src/Routing/Route/Route.php +++ b/src/Routing/Route/Route.php @@ -12,16 +12,13 @@ abstract class Route implements RouteInterface /** @var MethodMap */ protected $methodMap; - public function __construct($target, $methodMap) + public function __construct(string $target, MethodMap $methodMap) { $this->target = $target; $this->methodMap = $methodMap; } - /** - * @return string - */ - public function getTarget() + public function getTarget(): string { return $this->target; } @@ -40,7 +37,7 @@ abstract class Route implements RouteInterface * @param string $method * @param mixed $dispatchable */ - public function register($method, $dispatchable) + public function register(string $method, $dispatchable): void { $this->methodMap->register($method, $dispatchable); } diff --git a/src/Routing/Route/RouteInterface.php b/src/Routing/Route/RouteInterface.php index 22413a3..8eaa017 100644 --- a/src/Routing/Route/RouteInterface.php +++ b/src/Routing/Route/RouteInterface.php @@ -34,7 +34,7 @@ interface RouteInterface extends MiddlewareInterface * * @return int One of the RouteInterface::TYPE_ constants. */ - public function getType(); + public function getType(): int; /** * Return an array of variables extracted from the path most recently @@ -45,7 +45,7 @@ interface RouteInterface extends MiddlewareInterface * * @return array */ - public function getPathVariables(); + public function getPathVariables(): array; /** * Examines a request target to see if it is a match for the route. @@ -55,7 +55,7 @@ interface RouteInterface extends MiddlewareInterface * @throw \RuntimeException Error occurred testing the target such as an * invalid regular expression */ - public function matchesRequestTarget($requestTarget); + public function matchesRequestTarget(string $requestTarget): bool; /** * Register a dispatchable (handler or middleware) with a method. @@ -71,5 +71,5 @@ interface RouteInterface extends MiddlewareInterface * @param string $method * @param mixed $dispatchable */ - public function register($method, $dispatchable); + public function register(string $method, $dispatchable): void; } diff --git a/src/Routing/Route/StaticRoute.php b/src/Routing/Route/StaticRoute.php index d31b84c..6cf9d77 100644 --- a/src/Routing/Route/StaticRoute.php +++ b/src/Routing/Route/StaticRoute.php @@ -4,7 +4,7 @@ namespace WellRESTed\Routing\Route; class StaticRoute extends Route { - public function getType() + public function getType(): int { return RouteInterface::TYPE_STATIC; } @@ -15,7 +15,7 @@ class StaticRoute extends Route * @param string $requestTarget * @return boolean */ - public function matchesRequestTarget($requestTarget) + public function matchesRequestTarget(string $requestTarget): bool { return $requestTarget === $this->getTarget(); } @@ -23,7 +23,7 @@ class StaticRoute extends Route /** * Always returns an empty array. */ - public function getPathVariables() + public function getPathVariables(): array { return []; } diff --git a/src/Routing/Route/TemplateRoute.php b/src/Routing/Route/TemplateRoute.php index 8b71790..9396f11 100644 --- a/src/Routing/Route/TemplateRoute.php +++ b/src/Routing/Route/TemplateRoute.php @@ -4,25 +4,27 @@ namespace WellRESTed\Routing\Route; class TemplateRoute extends Route { - private $pathVariables; - private $explosions; + /** @var array */ + private $pathVariables = []; + /** @var array */ + private $explosions = []; + /** Regular expression matching a URI template variable (e.g., {id}) */ + public const URI_TEMPLATE_EXPRESSION_RE = '/{([+.\/]?[a-zA-Z0-9_,]+\*?)}/'; /** * Regular expression matching 1 or more unreserved characters. * ALPHA / DIGIT / "-" / "." / "_" / "~" */ - const RE_UNRESERVED = '[0-9a-zA-Z\-._\~%]*'; - /** Regular expression matching a URI template variable (e.g., {id}) */ - const URI_TEMPLATE_EXPRESSION_RE = '/{([+.\/]?[a-zA-Z0-9_,]+\*?)}/'; + private const RE_UNRESERVED = '[0-9a-zA-Z\-._\~%]*'; - public function getType() + public function getType(): int { return RouteInterface::TYPE_PATTERN; } - public function getPathVariables() + public function getPathVariables(): array { - return $this->pathVariables ?: []; + return $this->pathVariables; } /** @@ -31,7 +33,7 @@ class TemplateRoute extends Route * @param string $requestTarget * @return bool */ - public function matchesRequestTarget($requestTarget) + public function matchesRequestTarget(string $requestTarget): bool { $this->pathVariables = []; $this->explosions = []; @@ -49,11 +51,7 @@ class TemplateRoute extends Route return false; } - /** - * @param $requestTarget - * @return bool - */ - private function matchesStartOfRequestTarget($requestTarget) + private function matchesStartOfRequestTarget(string $requestTarget): bool { $firstVarPos = strpos($this->target, "{"); if ($firstVarPos === false) { @@ -62,7 +60,7 @@ class TemplateRoute extends Route return (substr($requestTarget, 0, $firstVarPos) === substr($this->target, 0, $firstVarPos)); } - private function processMatches($matches) + private function processMatches(array $matches): array { $variables = []; @@ -87,7 +85,7 @@ class TemplateRoute extends Route return $variables; } - private function getMatchingPattern() + private function getMatchingPattern(): string { // Convert the template into the pattern $pattern = $this->target; @@ -119,7 +117,7 @@ class TemplateRoute extends Route return $pattern; } - private function uriVariableReplacementCallback($matches) + private function uriVariableReplacementCallback(array $matches): string { $name = $matches[1]; $pattern = self::RE_UNRESERVED; diff --git a/test/tests/unit/Routing/Route/RouteTest.php b/test/tests/unit/Routing/Route/RouteTest.php index 993e411..694e53a 100644 --- a/test/tests/unit/Routing/Route/RouteTest.php +++ b/test/tests/unit/Routing/Route/RouteTest.php @@ -23,8 +23,7 @@ class RouteTest extends TestCase public function setUp(): void { $this->methodMap = $this->prophesize(MethodMap::class); - $this->methodMap->register(Argument::cetera()) - ->willReturn(); + $this->methodMap->register(Argument::cetera()); $this->methodMap->__invoke(Argument::cetera()) ->willReturn(new Response()); diff --git a/test/tests/unit/Routing/Route/TemplateRouteTest.php b/test/tests/unit/Routing/Route/TemplateRouteTest.php index c480db2..37a63d5 100644 --- a/test/tests/unit/Routing/Route/TemplateRouteTest.php +++ b/test/tests/unit/Routing/Route/TemplateRouteTest.php @@ -57,7 +57,7 @@ class TemplateRouteTest extends TestCase /** @dataProvider nonMatchingTargetProvider */ public function testFailsToMatchNonMatchingTarget($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertFalse($route->matchesRequestTarget($target)); } @@ -77,14 +77,14 @@ class TemplateRouteTest extends TestCase /** @dataProvider simpleStringProvider */ public function testMatchesSimpleStrings($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($target)); } /** @dataProvider simpleStringProvider */ public function testCapturesFromSimpleStrings($template, $target, $variables) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $route->matchesRequestTarget($target); $this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables()); } @@ -106,14 +106,14 @@ class TemplateRouteTest extends TestCase /** @dataProvider reservedStringProvider */ public function testMatchesReservedStrings($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($target)); } /** @dataProvider reservedStringProvider */ public function testCapturesFromReservedStrings($template, $target, $variables) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $route->matchesRequestTarget($target); $this->assertSame($this->getExpectedValues($variables), $route->getPathVariables()); } @@ -133,14 +133,14 @@ class TemplateRouteTest extends TestCase /** @dataProvider labelWithDotPrefixProvider */ public function testMatchesLabelWithDotPrefix($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($target)); } /** @dataProvider labelWithDotPrefixProvider */ public function testCapturesFromLabelWithDotPrefix($template, $target, $variables) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $route->matchesRequestTarget($target); $this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables()); } @@ -160,14 +160,14 @@ class TemplateRouteTest extends TestCase /** @dataProvider pathSegmentProvider */ public function testMatchesPathSegments($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($target)); } /** @dataProvider pathSegmentProvider */ public function testCapturesFromPathSegments($template, $target, $variables) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $route->matchesRequestTarget($target); $this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables()); } @@ -187,14 +187,14 @@ class TemplateRouteTest extends TestCase /** @dataProvider pathExplosionProvider */ public function testMatchesExplosion($template, $target) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget($target)); } /** @dataProvider pathExplosionProvider */ public function testCapturesFromExplosion($template, $target, $variables) { - $route = new TemplateRoute($template, $this->methodMap); + $route = new TemplateRoute($template, $this->methodMap->reveal()); $route->matchesRequestTarget($target); $this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables()); }