Skip to content

Commit

Permalink
Add Router comments
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 12, 2023
1 parent 9502d57 commit 04c331a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Handler/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Buggregator\Trap\Handler\Router;

use Buggregator\Trap\Handler\Router\Attribute\Route as RouteAttribute;
use Throwable;

/**
* @internal
Expand All @@ -26,6 +27,10 @@ private function __construct(
}

/**
* @param class-string|object $classOrObject Class name or object to create router for.
* Specify an object to associate router with an object. It is important for routes defined in
* non-static methods.
*
* @throws \Exception
*/
public static function new(string|object $classOrObject): self
Expand All @@ -35,6 +40,9 @@ public static function new(string|object $classOrObject): self
: self::newStatic($classOrObject);
}

/**
* Associate router with an object.
*/
private function withObject(object $object): self
{
$new = clone $this;
Expand All @@ -43,6 +51,8 @@ private function withObject(object $object): self
}

/**
* Create a new instance of Router for specified class. To associate router with an object use {@see withObject()}.
*
* @param class-string $class
*
* @throws \Exception
Expand Down Expand Up @@ -75,6 +85,8 @@ private static function newStatic(string $class): self
}

/**
* Collect routes from class.
*
* @param class-string $class
*
* @return list<RouteDto>
Expand All @@ -83,7 +95,7 @@ private static function newStatic(string $class): self
*/
private static function collectRoutes(string $class): array
{
/** @var RouteAttribute[] $result */
/** @var list<RouteDto> $result */
$result = [];

// Find all public methods with #[Route] attribute
Expand All @@ -104,7 +116,7 @@ private static function collectRoutes(string $class): array
}

/**
* @param null|object $object Null for static methods
* Find a route for specified method and path.
*
* @return null|callable(mixed...): mixed Returns null if no route matches
*
Expand All @@ -114,8 +126,8 @@ public function match(Method $method, string $path): ?callable
{
foreach ($this->routes[$method->value] as $route) {
$match = match ($route->route::class) {
Attribute\StaticRoute::class => $path === $route->route->path,
Attribute\RegexpRoute::class => \preg_match($route->route->regexp, $path, $matches) === 1
Attribute\StaticRoute::class => $path === (string)$route->route->path,

Check failure on line 129 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedPropertyFetch

src/Handler/Router/Router.php:129:67: UndefinedPropertyFetch: Instance property Buggregator\Trap\Handler\Router\Attribute\Route::$path is not defined (see https://psalm.dev/039)

Check failure on line 129 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedPropertyFetch

src/Handler/Router/Router.php:129:67: UndefinedPropertyFetch: Instance property Buggregator\Trap\Handler\Router\Attribute\Route::$path is not defined (see https://psalm.dev/039)
Attribute\RegexpRoute::class => \preg_match((string)$route->route->regexp, $path, $matches) === 1

Check failure on line 130 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

ArgumentTypeCoercion

src/Handler/Router/Router.php:130:61: ArgumentTypeCoercion: Argument 1 of preg_match expects non-empty-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 130 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedPropertyFetch

src/Handler/Router/Router.php:130:69: UndefinedPropertyFetch: Instance property Buggregator\Trap\Handler\Router\Attribute\Route::$regexp is not defined (see https://psalm.dev/039)

Check failure on line 130 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

ArgumentTypeCoercion

src/Handler/Router/Router.php:130:61: ArgumentTypeCoercion: Argument 1 of preg_match expects non-empty-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 130 in src/Handler/Router/Router.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

UndefinedPropertyFetch

src/Handler/Router/Router.php:130:69: UndefinedPropertyFetch: Instance property Buggregator\Trap\Handler\Router\Attribute\Route::$regexp is not defined (see https://psalm.dev/039)
? \array_filter($matches, '\is_string', \ARRAY_FILTER_USE_KEY)
: false,
default => throw new \LogicException(\sprintf(
Expand Down Expand Up @@ -143,9 +155,14 @@ public function match(Method $method, string $path): ?callable
return null;
}

/**
* Invoke a method with specified arguments. The arguments will be filtered by parameter names.
*
* @throws Throwable
*/
private static function invoke(\ReflectionMethod $method, ?object $object, array $args): mixed
{
// Filter args
/** @var array<string, mixed> $filteredArgs Filter args */
$filteredArgs = [];
foreach ($method->getParameters() as $param) {
$name = $param->getName();
Expand Down

0 comments on commit 04c331a

Please sign in to comment.