diff --git a/README.md b/README.md index 9210c06..eb4acb0 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ use SimpleRouting\Router; use SimpleRouting\Exception\HttpException; // Create router instance -$router = new Router; +$router = new Router(); // Add route with GET method $router->get('/foo', 'foo'); diff --git a/src/Exception/BadRouteException.php b/src/Exception/BadRouteException.php index d391c95..d10cb6b 100644 --- a/src/Exception/BadRouteException.php +++ b/src/Exception/BadRouteException.php @@ -1,4 +1,6 @@ -httpMethod = $httpMethod; $this->uri = $uri; @@ -47,33 +19,21 @@ public function __construct(string $httpMethod, string $uri, $handler, ?array $r $this->regex = $regex; } - /** - * @return string - */ public function getHttpMethod(): string { return $this->httpMethod; } - /** - * @return string - */ public function getUri(): string { return $this->uri; } - /** - * @return callable|string - */ - public function getHandler() + public function getHandler(): callable|string { return $this->handler; } - /** - * @return array - */ public function getRegex(): array { return $this->regex; diff --git a/src/RouteCollection.php b/src/RouteCollection.php index cad2dc0..e817456 100644 --- a/src/RouteCollection.php +++ b/src/RouteCollection.php @@ -1,97 +1,45 @@ -groupPrefix = ''; - $this->routeParser = $routeParser ?? new RouteParser; + $this->routeParser = $routeParser ?? new RouteParser(); } - /** - * Get route collection - * - * @return array - */ public function getRoutes(): array { return $this->routes; } - /** - * Set group prefix - * - * @param string $prefix - * @return void - */ public function setGroupPrefix(string $prefix): void { $this->groupPrefix = $prefix; } - /** - * Get group prefix - * - * @return string - */ public function getGroupPrefix(): string { return $this->groupPrefix; } - /** - * Add group prefix - * - * @param string $prefix - * @param callable $callback - * @return void - */ public function addGroup(string $prefix, callable $callback): void { $oldGroupPrefix = $this->getGroupPrefix(); - - // Change prefix + $this->setGroupPrefix($oldGroupPrefix . $prefix); - - // Call function $callback($this); - - // Return old prefix $this->setGroupPrefix($oldGroupPrefix); } - /** - * Adding route to collection - * - * @param string $httpMethod - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void - */ public function addRoute(string $httpMethod, string $uri, $handler, ?array $regex = null): void { $uri = $this->getGroupPrefix() . $uri; diff --git a/src/RouteDispatcher.php b/src/RouteDispatcher.php index f0558c4..765a173 100644 --- a/src/RouteDispatcher.php +++ b/src/RouteDispatcher.php @@ -1,28 +1,18 @@ -routes = $routes; } - /** - * @param string $httpMethod - * @param string $uri - * - * @return array|null - */ public function handle(string $httpMethod, string $uri): ?array { $routes = $this->routes; @@ -59,12 +49,6 @@ public function handle(string $httpMethod, string $uri): ?array ]; } - /** - * @param array|null $matches - * @param array|null $regex - * - * @return array - */ private function processArgs(?array $matches, ?array $regex): array { $i = 1; diff --git a/src/RouteParser.php b/src/RouteParser.php index dd7c01c..fc19cda 100644 --- a/src/RouteParser.php +++ b/src/RouteParser.php @@ -1,4 +1,6 @@ - ['[a-zA-Z]']], finalize - ~\/test\/[a-zA-Z]~ * - * @param string $uri - * @param array|null $regex - * * @throws BadRouteException - * - * @return string */ public function make(string $uri, ?array &$regex = null): string { diff --git a/src/Router.php b/src/Router.php index e574f1f..2e47ac4 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,4 +1,6 @@ -routeCollection = new RouteCollection($routeParser); } - /** - * Short function of adding group - * - * @param string $prefix - * @param callable $callback - * - * @return void - */ public function group(string $prefix, callable $callback): void { $this->routeCollection->addGroup($prefix, $callback); @@ -35,13 +23,6 @@ public function group(string $prefix, callable $callback): void /** * Adding route with multiple HTTP methods to collection - * - * @param array $httpMethods - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void */ public function map(array $httpMethods, string $uri, $handler, ?array $regex = null): void { @@ -52,13 +33,6 @@ public function map(array $httpMethods, string $uri, $handler, ?array $regex = n /** * Adding route as GET to collection - * - * @param array $httpMethods - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void */ public function get(string $uri, $handler, ?array $regex = null): void { @@ -67,13 +41,6 @@ public function get(string $uri, $handler, ?array $regex = null): void /** * Adding route as POST to collection - * - * @param array $httpMethods - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void */ public function post(string $uri, $handler, ?array $regex = null): void { @@ -82,13 +49,6 @@ public function post(string $uri, $handler, ?array $regex = null): void /** * Adding route as DELETE to collection - * - * @param array $httpMethods - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void */ public function delete(string $uri, $handler, ?array $regex = null): void { @@ -97,13 +57,6 @@ public function delete(string $uri, $handler, ?array $regex = null): void /** * Adding route as PUT to collection - * - * @param array $httpMethods - * @param string $uri - * @param string|callable $handler - * @param array|null $regex - * - * @return void */ public function put(string $uri, $handler, ?array $regex = null): void { @@ -113,10 +66,6 @@ public function put(string $uri, $handler, ?array $regex = null): void /** * @param string $httpMethod * @param string $uri - * - * @throws HttpException - * - * @return array */ public function dispatch(?string $httpMethod = null, ?string $uri = null): array { diff --git a/tests/RouteCollectionTest.php b/tests/RouteCollectionTest.php index 5184ab7..a0dc4d0 100644 --- a/tests/RouteCollectionTest.php +++ b/tests/RouteCollectionTest.php @@ -1,4 +1,6 @@ -addRoute('GET', '/foo', 'foo'); $routeCollection->addRoute('POST', '/foo/bar', 'foo-bar'); diff --git a/tests/RouteParserTest.php b/tests/RouteParserTest.php index 4ae3e78..820b7bc 100644 --- a/tests/RouteParserTest.php +++ b/tests/RouteParserTest.php @@ -1,4 +1,6 @@ - '[0-9]']; diff --git a/tests/RouterTest.php b/tests/RouterTest.php index e783c6e..c0810a2 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -1,4 +1,6 @@ -get('/foo/{bar}/', 'foo-bar', ['bar' => '[0-9]']); $result = $router->dispatch('GET', '/foo/1/'); @@ -18,7 +20,7 @@ public function testRouteDispatching(): void public function testRouteNotFound(): void { - $router = new Router; + $router = new Router(); try { $router->dispatch('GET', '/foo');