Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit dae09f9

Browse files
xHeavenCommandString
authored andcommitted
several modifications, see description
- fixed docblocks - removed unnecessary `&` from `addRoute(Route &$route)` because object parameters are always passed as reference - replaced self-references with `self::` - replaced `array_merge` with spread operator - replaced `boolval()` with `(bool)` typecast - made anonymous functions static - replaced `in_array()` with `array_key_exists()` - removed unnecessary `EMIT_EMPTY_RESPONSE` in match as its value was the same as the default arm
1 parent e6269e6 commit dae09f9

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/Router.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ final class Router
4646

4747
/**
4848
* @param string $uri
49-
* @param class-string<RequestHandlerInterface>
50-
* @return Route
49+
* @param class-string<RequestHandlerInterface> $controller
50+
* @return Route
5151
*/
5252
public static function get(string $uri, string $controller): Route
5353
{
@@ -60,8 +60,8 @@ public static function get(string $uri, string $controller): Route
6060

6161
/**
6262
* @param string $uri
63-
* @param class-string<RequestHandlerInterface>
64-
* @return Route
63+
* @param class-string<RequestHandlerInterface> $controller
64+
* @return Route
6565
*/
6666
public static function post(string $uri, string $controller): Route
6767
{
@@ -74,8 +74,8 @@ public static function post(string $uri, string $controller): Route
7474

7575
/**
7676
* @param string $uri
77-
* @param class-string<RequestHandlerInterface>
78-
* @return Route
77+
* @param class-string<RequestHandlerInterface> $controller
78+
* @return Route
7979
*/
8080
public static function put(string $uri, string $controller): Route
8181
{
@@ -88,8 +88,8 @@ public static function put(string $uri, string $controller): Route
8888

8989
/**
9090
* @param string $uri
91-
* @param class-string<RequestHandlerInterface>
92-
* @return Route
91+
* @param class-string<RequestHandlerInterface> $controller
92+
* @return Route
9393
*/
9494
public static function delete(string $uri, string $controller): Route
9595
{
@@ -102,8 +102,8 @@ public static function delete(string $uri, string $controller): Route
102102

103103
/**
104104
* @param string $uri
105-
* @param class-string<RequestHandlerInterface>
106-
* @return Route
105+
* @param class-string<RequestHandlerInterface> $controller
106+
* @return Route
107107
*/
108108
public static function options(string $uri, string $controller): Route
109109
{
@@ -116,8 +116,8 @@ public static function options(string $uri, string $controller): Route
116116

117117
/**
118118
* @param string $uri
119-
* @param class-string<RequestHandlerInterface>
120-
* @return Route
119+
* @param class-string<RequestHandlerInterface> $controller
120+
* @return Route
121121
*/
122122
public static function head(string $uri, string $controller): Route
123123
{
@@ -130,8 +130,8 @@ public static function head(string $uri, string $controller): Route
130130

131131
/**
132132
* @param string $uri
133-
* @param class-string<RequestHandlerInterface>
134-
* @return Route
133+
* @param class-string<RequestHandlerInterface> $controller
134+
* @return Route
135135
*/
136136
public static function all(string $uri, string $controller): Route
137137
{
@@ -146,7 +146,7 @@ public static function all(string $uri, string $controller): Route
146146
* @param Route $route
147147
* @return void
148148
*/
149-
public static function addRoute(Route &$route): void
149+
public static function addRoute(Route $route): void
150150
{
151151
if (isset(self::$group)) {
152152
foreach (self::$group["middlewares"] ?? [] as $middlware) {
@@ -169,12 +169,12 @@ public static function group(string $baseUri, Closure $grouping, array $middlewa
169169
{
170170
$oldMount = self::$group;
171171

172-
if (empty(Router::getBaseUri())) {
172+
if (empty(self::getBaseUri())) {
173173
self::$group = compact("baseUri", "middlewares", "postwares");
174174
} else {
175175
self::$group['baseUri'] .= $baseUri;
176-
self::$group['middlewares'] = array_merge(self::$group['middlewares'], $middlewares);
177-
self::$group['postwares'] = array_merge(self::$group['postwares'], $postwares);
176+
self::$group['middlewares'] = [...self::$group['middlewares'], ...$middlewares];
177+
self::$group['postwares'] = [...self::$group['postwares'], ...$postwares];
178178
}
179179

180180
$grouping();
@@ -221,7 +221,7 @@ private static function resolveRoute(array $routes): ?ResolvedRoute
221221

222222
$pattern = preg_replace('/\/{(.*?)}/', '/(.*?)', $uri);
223223

224-
return boolval(preg_match_all('#^' . $pattern . '$#', $requestUri, $matches, PREG_OFFSET_CAPTURE));
224+
return (bool)preg_match_all('#^' . $pattern . '$#', $requestUri, $matches, PREG_OFFSET_CAPTURE);
225225
};
226226

227227
$uri = explode("?", $_SERVER["REQUEST_URI"])[0];
@@ -255,10 +255,13 @@ private static function resolveRoute(array $routes): ?ResolvedRoute
255255
return $resolvedRoute ?? null;
256256
}
257257

258-
/**
259-
* @param class-string<Throwable> $exceptionToCatch
260-
* @param class-string<RequestHandlerInterface> $controller
261-
*/
258+
259+
/**
260+
* @param string $toCatch
261+
* @param class-string $controller
262+
* @param string|null $uri
263+
* @return Route
264+
*/
262265
public static function catch(string $toCatch, string $controller, ?string $uri = "/(.*)"): Route
263266
{
264267
$catchable = array_keys(self::$catchers);
@@ -311,7 +314,7 @@ protected static function invokeRoute(
311314
...$resolvedRoute->route->getPostware()
312315
];
313316

314-
$next = function (
317+
$next = static function (
315318
ServerRequestInterface $request,
316319
ResponseInterface $response,
317320
stdClass $args
@@ -335,7 +338,7 @@ public static function run(EmitterInterface $emitter = null): void
335338
{
336339
self::$emitter = $emitter ?? new SapiEmitter();
337340

338-
$sortByLength = function (Route $a, Route $b) {
341+
$sortByLength = static function (Route $a, Route $b) {
339342
return (strlen($a->uri) > strlen($b->uri));
340343
};
341344

@@ -356,7 +359,7 @@ public static function run(EmitterInterface $emitter = null): void
356359

357360
$response = self::invokeRoute($resolved, $request);
358361
} catch (Throwable $e) {
359-
if (in_array($e::class, array_keys(self::$catchers))) {
362+
if (array_key_exists($e::class, self::$catchers)) {
360363
$resolved = self::resolveRoute(self::$catchers[$e::class]);
361364
} else {
362365
$resolved = self::resolveRoute(self::$catchers[HttpInternalServerError::class] ?? []);
@@ -370,8 +373,7 @@ public static function run(EmitterInterface $emitter = null): void
370373
$class = $e::class;
371374

372375
$method = match (self::$emitHttpExceptions) {
373-
self::EMIT_EMPTY_RESPONSE => "buildEmptyResponse",
374-
self::EMIT_HTML_RESPONSE => "buildHtmlResponse",
376+
self::EMIT_HTML_RESPONSE => "buildHtmlResponse",
375377
self::EMIT_JSON_RESPONSE => "buildJsonResponse",
376378
default => "buildEmptyResponse"
377379
};

0 commit comments

Comments
 (0)