Skip to content

Commit

Permalink
Route interfaces refactoring + fix psalm errors (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 3, 2021
1 parent 5721599 commit 44b64c4
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 27 deletions.
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -29,12 +29,12 @@
},
"require-dev": {
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^9.4",
"phpunit/phpunit": "^9.5",
"psr/event-dispatcher": "^1.0",
"roave/infection-static-analysis-plugin": "^1.5",
"roave/infection-static-analysis-plugin": "^1.8",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.2",
"yiisoft/router-fastroute": "^3.0@dev"
"vimeo/psalm": "^4.8",
"yiisoft/dummy-provider": "^1.0.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 0 additions & 4 deletions psalm.xml
@@ -1,15 +1,11 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
8 changes: 4 additions & 4 deletions src/Group.php
Expand Up @@ -16,7 +16,7 @@
final class Group implements RouteCollectorInterface
{
/**
* @var Group[]|RouteParametersInterface[]
* @var Group[]|Route[]
*/
protected array $items = [];
protected ?string $prefix;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function routes(...$routes): self
} elseif (is_array($routes)) {
$callback = static function (self $group) use (&$routes) {
foreach ($routes as $route) {
if ($route instanceof RouteInterface) {
if ($route instanceof Route) {
$group->addRoute($route);
} elseif ($route instanceof self) {
$group->addGroup($route);
Expand Down Expand Up @@ -97,7 +97,7 @@ public function hasDispatcher(): bool
return $this->dispatcher !== null;
}

public function addRoute(RouteInterface $route): self
public function addRoute(Route $route): self
{
if (!$route->hasDispatcher() && $this->hasDispatcher()) {
$route->injectDispatcher($this->dispatcher);
Expand Down Expand Up @@ -166,7 +166,7 @@ public function disableMiddleware($middlewareDefinition): self
}

/**
* @return Group[]|RouteParametersInterface[]
* @return Group[]|Route[]
*/
public function getItems(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/MatchingResult.php
Expand Up @@ -14,7 +14,7 @@
final class MatchingResult implements MiddlewareInterface
{
private bool $success;
private RouteParametersInterface $route;
private Route $route;
private array $parameters = [];
private array $methods = [];
private ?MiddlewareDispatcher $dispatcher = null;
Expand All @@ -30,7 +30,7 @@ public function withDispatcher(MiddlewareDispatcher $dispatcher): self
return $new;
}

public static function fromSuccess(RouteParametersInterface $route, array $parameters): self
public static function fromSuccess(Route $route, array $parameters): self
{
$new = new self();
$new->success = true;
Expand Down
30 changes: 30 additions & 0 deletions src/Route.php
Expand Up @@ -35,6 +35,9 @@ public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
$this->dispatcher = $dispatcher;
}

/**
* @return self
*/
public function withDispatcher(MiddlewareDispatcher $dispatcher): RouteInterface
{
$route = clone $this;
Expand Down Expand Up @@ -158,41 +161,59 @@ public static function methods(
return $route;
}

/**
* @return self
*/
public function name(string $name): RouteInterface
{
$route = clone $this;
$route->name = $name;
return $route;
}

/**
* @return self
*/
public function pattern(string $pattern): RouteInterface
{
$new = clone $this;
$new->pattern = $pattern;
return $new;
}

/**
* @return self
*/
public function host(string $host): RouteInterface
{
$route = clone $this;
$route->host = rtrim($host, '/');
return $route;
}

/**
* @return self
*/
public function override(): RouteInterface
{
$route = clone $this;
$route->override = true;
return $route;
}

/**
* @return self
*/
public function defaults(array $defaults): RouteInterface
{
$route = clone $this;
$route->defaults = $defaults;
return $route;
}

/**
* @return self
*/
public function middleware($middlewareDefinition): RouteInterface
{
if ($this->actionAdded) {
Expand All @@ -203,6 +224,9 @@ public function middleware($middlewareDefinition): RouteInterface
return $route;
}

/**
* @return self
*/
public function prependMiddleware($middlewareDefinition): RouteInterface
{
if (!$this->actionAdded) {
Expand All @@ -213,6 +237,9 @@ public function prependMiddleware($middlewareDefinition): RouteInterface
return $route;
}

/**
* @return self
*/
public function action($middlewareDefinition): RouteInterface
{
$route = clone $this;
Expand All @@ -221,6 +248,9 @@ public function action($middlewareDefinition): RouteInterface
return $route;
}

/**
* @return self
*/
public function disableMiddleware($middlewareDefinition): RouteInterface
{
$route = clone $this;
Expand Down
13 changes: 6 additions & 7 deletions src/RouteCollection.php
Expand Up @@ -15,7 +15,7 @@ final class RouteCollection implements RouteCollectionInterface
/**
* All attached routes as Route instances
*
* @var RouteParametersInterface[]
* @var Route[]
*/
private array $routes = [];

Expand All @@ -25,7 +25,7 @@ public function __construct(RouteCollectorInterface $collector)
}

/**
* @return RouteParametersInterface[]
* @return Route[]
*/
public function getRoutes(): array
{
Expand All @@ -36,9 +36,9 @@ public function getRoutes(): array
/**
* @param string $name
*
* @return RouteParametersInterface
* @return Route
*/
public function getRoute(string $name): RouteParametersInterface
public function getRoute(string $name): Route
{
$this->ensureItemsInjected();
if (!array_key_exists($name, $this->routes)) {
Expand Down Expand Up @@ -71,7 +71,7 @@ private function ensureItemsInjected(): void
/**
* Build routes array
*
* @param Group[]|RouteCollectorInterface[]|RouteParametersInterface[] $items
* @param Group[]|Route[]|RouteCollectorInterface[] $items
*/
private function injectItems(array $items): void
{
Expand All @@ -83,7 +83,7 @@ private function injectItems(array $items): void
/**
* Add an item into routes array
*
* @param Group|RouteParametersInterface $route
* @param Group|Route $route
*/
private function injectItem($route): void
{
Expand All @@ -106,7 +106,6 @@ private function injectItem($route): void
private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
{
$prefix .= $group->getPrefix();
/** @var $items Group[]|Route[] */
$items = $group->getItems();
foreach ($items as $item) {
if ($item instanceof Group || $item->hasMiddlewares()) {
Expand Down
6 changes: 3 additions & 3 deletions src/RouteCollectionInterface.php
Expand Up @@ -7,16 +7,16 @@
interface RouteCollectionInterface
{
/**
* @return RouteParametersInterface[]
* @return Route[]
*/
public function getRoutes(): array;

/**
* @param string $name
*
* @return RouteParametersInterface
* @return Route
*/
public function getRoute(string $name): RouteParametersInterface;
public function getRoute(string $name): Route;

/**
* Returns routes tree array
Expand Down
6 changes: 3 additions & 3 deletions src/RouteCollectorInterface.php
Expand Up @@ -9,11 +9,11 @@ interface RouteCollectorInterface
/**
* Add a route
*
* @param RouteInterface $route
* @param Route $route
*
* @return self
*/
public function addRoute(RouteInterface $route): self;
public function addRoute(Route $route): self;

/**
* Add a group of routes
Expand All @@ -35,7 +35,7 @@ public function addRoute(RouteInterface $route): self;
public function addGroup(Group $group): self;

/**
* @return Group[]|RouteParametersInterface[]
* @return Group[]|Route[]
*/
public function getItems(): array;

Expand Down
2 changes: 2 additions & 0 deletions src/RouteParametersInterface.php
Expand Up @@ -21,4 +21,6 @@ public function getHost(): ?string;
public function isOverride(): bool;

public function getDefaults(): array;

public function __toString(): string;
}

0 comments on commit 44b64c4

Please sign in to comment.