Skip to content

Commit

Permalink
Move type hints from annotations to methods signature + Cleanup + MSI…
Browse files Browse the repository at this point in the history
… 100% (#178)
  • Loading branch information
vjik committed Nov 12, 2022
1 parent ebffb51 commit f813e56
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 89 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Expand Up @@ -13,5 +13,9 @@ trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

[*.php]
ij_php_space_before_short_closure_left_parenthesis = true
ij_php_space_after_type_cast = true

[*.yml]
indent_size = 2
1 change: 1 addition & 0 deletions .github/workflows/mutation.yml
Expand Up @@ -23,6 +23,7 @@ jobs:
mutation:
uses: yiisoft/actions/.github/workflows/roave-infection.yml@master
with:
min-covered-msi: 100
os: >-
['ubuntu-latest']
php: >-
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- Enh #173: Raise minimum PHP version to 8.0 (@xepozz, @rustamwin)
- Enh #176: Add support for `yiisoft/middleware-dispatcher` version `^4.0` (@vjik)
- Enh #175: Add `$queryParameters` parameter to `UrlGeneratorInterface::generateFromCurrent()` method (@rustamwin)
- Chg #178: Move type hints from annotations to methods signature (@vjik)

## 1.2.0 September 07, 2022

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -39,7 +39,7 @@ with an adapter package. Currently, the only adapter available is [FastRoute](ht
The package could be installed with composer:

```shell
composer require yiisoft/router --prefer-dist
composer require yiisoft/router
```

Additionally, you will need an adapter such as [FastRoute](https://github.com/yiisoft/router-fastroute).
Expand Down
5 changes: 0 additions & 5 deletions dependency-checker.json

This file was deleted.

19 changes: 7 additions & 12 deletions src/CurrentRoute.php
Expand Up @@ -25,9 +25,7 @@ final class CurrentRoute
/**
* Current Route arguments.
*
* @var string[]
*
* @psalm-var array<string, string>
* @var array<string, string>
*/
private array $arguments = [];

Expand All @@ -38,7 +36,7 @@ final class CurrentRoute
*/
public function getName(): ?string
{
return $this->route !== null ? $this->route->getData('name') : null;
return $this->route?->getData('name');
}

/**
Expand All @@ -48,7 +46,7 @@ public function getName(): ?string
*/
public function getHost(): ?string
{
return $this->route !== null ? $this->route->getData('host') : null;
return $this->route?->getData('host');
}

/**
Expand All @@ -58,7 +56,7 @@ public function getHost(): ?string
*/
public function getPattern(): ?string
{
return $this->route !== null ? $this->route->getData('pattern') : null;
return $this->route?->getData('pattern');
}

/**
Expand All @@ -68,7 +66,7 @@ public function getPattern(): ?string
*/
public function getMethods(): ?array
{
return $this->route !== null ? $this->route->getData('methods') : null;
return $this->route?->getData('methods');
}

/**
Expand All @@ -82,9 +80,7 @@ public function getUri(): ?UriInterface
}

/**
* @param string[] $arguments
*
* @psalm-param array<string,string> $arguments
* @param array<string,string> $arguments
*
* @internal
*/
Expand All @@ -111,8 +107,7 @@ public function setUri(UriInterface $uri): void
}

/**
* @return string[] Arguments.
* @psalm-return array<string, string>
* @return array<string, string> Arguments.
*/
public function getArguments(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/Group.php
Expand Up @@ -30,6 +30,7 @@ final class Group
private bool $routesAdded = false;
private bool $middlewareAdded = false;
private array $disabledMiddlewareDefinitions = [];

/**
* @var array|callable|string|null Middleware definition for CORS requests.
*/
Expand Down Expand Up @@ -90,7 +91,7 @@ public function withDispatcher(MiddlewareDispatcher $dispatcher): self
*
* @param array|callable|string|null $middlewareDefinition Middleware definition for CORS requests.
*/
public function withCors($middlewareDefinition): self
public function withCors(array|callable|string|null $middlewareDefinition): self
{
$group = clone $this;
$group->corsMiddleware = $middlewareDefinition;
Expand Down
9 changes: 3 additions & 6 deletions src/MatchingResult.php
Expand Up @@ -15,8 +15,7 @@
final class MatchingResult implements MiddlewareInterface
{
/**
* @var string[]
* @psalm-var array<string,string>
* @var array<string,string>
*/
private array $arguments = [];

Expand All @@ -39,8 +38,7 @@ public function withDispatcher(MiddlewareDispatcher $dispatcher): self
}

/**
* @param string[] $arguments
* @psalm-param array<string,string> $arguments
* @param array<string,string> $arguments
*/
public static function fromSuccess(Route $route, array $arguments): self
{
Expand Down Expand Up @@ -73,8 +71,7 @@ public function isMethodFailure(): bool
}

/**
* @return string[]
* @psalm-return array<string,string>
* @return array<string,string>
*/
public function arguments(): array
{
Expand Down
26 changes: 12 additions & 14 deletions src/Route.php
Expand Up @@ -15,7 +15,7 @@
/**
* Route defines a mapping from URL to callback / name and vice versa.
*/
final class Route implements \Stringable
final class Route implements Stringable
{
private ?string $name = null;

Expand All @@ -34,16 +34,18 @@ final class Route implements \Stringable
private array $disabledMiddlewareDefinitions = [];

/**
* @var string[]
* @psalm-var array<string,string>
* @var array<string,string>
*/
private array $defaults = [];

/**
* @param string[] $methods
*/
private function __construct(private array $methods, private string $pattern, private ?MiddlewareDispatcher $dispatcher = null)
{
private function __construct(
private array $methods,
private string $pattern,
private ?MiddlewareDispatcher $dispatcher = null
) {
}

/**
Expand Down Expand Up @@ -225,8 +227,6 @@ public function disableMiddleware(mixed ...$middlewareDefinition): self
}

/**
* @return mixed
*
* @psalm-template T as string
* @psalm-param T $key
* @psalm-return (
Expand All @@ -244,7 +244,7 @@ public function disableMiddleware(mixed ...$middlewareDefinition): self
* )
* )
*/
public function getData(string $key)
public function getData(string $key): mixed
{
return match ($key) {
'name' => $this->name ??
Expand All @@ -264,18 +264,16 @@ public function getData(string $key)

public function __toString(): string
{
$result = '';

if ($this->name !== null) {
$result .= '[' . $this->name . '] ';
}
$result = $this->name === null
? ''
: '[' . $this->name . '] ';

if ($this->methods !== []) {
$result .= implode(',', $this->methods) . ' ';
}

if ($this->hosts) {
$quoted = array_map(static fn ($host) => preg_quote($host), $this->hosts);
$quoted = array_map(static fn ($host) => preg_quote($host, '/'), $this->hosts);

if (!preg_match('/' . implode('|', $quoted) . '/', $this->pattern)) {
$result .= implode('|', $this->hosts);
Expand Down
7 changes: 0 additions & 7 deletions src/RouteCollectionInterface.php
Expand Up @@ -11,17 +11,10 @@ interface RouteCollectionInterface
*/
public function getRoutes(): array;

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

/**
* Returns routes tree array.
*
* @return array
*/
public function getRouteTree(): array;
}
4 changes: 2 additions & 2 deletions src/RouteCollector.php
Expand Up @@ -34,7 +34,7 @@ public function addGroup(Group ...$group): RouteCollectorInterface
return $this;
}

public function middleware(...$middlewareDefinition): RouteCollectorInterface
public function middleware(array|callable|string ...$middlewareDefinition): RouteCollectorInterface
{
array_push(
$this->middlewareDefinitions,
Expand All @@ -43,7 +43,7 @@ public function middleware(...$middlewareDefinition): RouteCollectorInterface
return $this;
}

public function prependMiddleware(...$middlewareDefinition): RouteCollectorInterface
public function prependMiddleware(array|callable|string ...$middlewareDefinition): RouteCollectorInterface
{
array_unshift(
$this->middlewareDefinitions,
Expand Down
18 changes: 2 additions & 16 deletions src/RouteCollectorInterface.php
Expand Up @@ -8,10 +8,6 @@ interface RouteCollectorInterface
{
/**
* Add a route.
*
* @param Route ...$route
*
* @return self
*/
public function addRoute(Route ...$route): self;

Expand All @@ -29,30 +25,20 @@ public function addRoute(Route ...$route): self;
* ```
*
* @param Group ...$group A group to add.
*
* @return self
*/
public function addGroup(Group ...$group): self;

/**
* Appends a handler middleware definition that should be invoked for a matched route.
* First added handler will be executed first.
*
* @param array|callable|string ...$middlewareDefinition
*
* @return self
*/
public function middleware(...$middlewareDefinition): self;
public function middleware(array|callable|string ...$middlewareDefinition): self;

/**
* Prepends a handler middleware definition that should be invoked for a matched route.
* First added handler will be executed last.
*
* @param array|callable|string ...$middlewareDefinition
*
* @return self
*/
public function prependMiddleware(...$middlewareDefinition): self;
public function prependMiddleware(array|callable|string ...$middlewareDefinition): self;

/**
* @return Group[]|Route[]
Expand Down
27 changes: 12 additions & 15 deletions src/UrlGeneratorInterface.php
Expand Up @@ -7,39 +7,35 @@
use Stringable;

/**
* UrlGeneratorInterface allows generating URL given route name, arguments, and query parameters.
* `UrlGeneratorInterface` allows generating URL given route name, arguments, and query parameters.
*/
interface UrlGeneratorInterface
{
/**
* Generates URL from named route, arguments, and query parameters.
*
* @param string $name Name of the route.
* @param array $arguments Argument-value set.
* @param array<string,scalar|Stringable|null> $arguments Argument-value set.
* @param array $queryParameters Parameter-value set.
*
* @throws RouteNotFoundException In case there is no route with the name specified.
*
* @return string URL generated.
*
* @psalm-param array<string,null|Stringable|scalar> $arguments
*/
public function generate(string $name, array $arguments = [], array $queryParameters = []): string;

/**
* Generates absolute URL from named route, arguments, and query parameters.
*
* @param string $name Name of the route.
* @param array $arguments Argument-value set.
* @param array<string,scalar|Stringable|null> $arguments Argument-value set.
* @param array $queryParameters Parameter-value set.
* @param string|null $scheme Host scheme.
* @param string|null $host Host for manual setup.
*
* @throws RouteNotFoundException In case there is no route with the name specified.
*
* @return string URL generated.
*
* @psalm-param array<string,null|Stringable|scalar> $arguments
*/
public function generateAbsolute(
string $name,
Expand All @@ -52,14 +48,17 @@ public function generateAbsolute(
/**
* Generate URL from the current route replacing some of its arguments with values specified.
*
* @param array $replacedArguments New argument values indexed by replaced argument names.
* @param array<string,scalar|Stringable|null> $replacedArguments New argument values indexed by replaced argument
* names.
* @param array $queryParameters Parameter-value set.
* @param string|null $fallbackRouteName Name of a route that should be used if current route.
* can not be determined.
*
* @psalm-param array<string,null|Stringable|scalar> $replacedArguments
*/
public function generateFromCurrent(array $replacedArguments, array $queryParameters = [], ?string $fallbackRouteName = null): string;
public function generateFromCurrent(
array $replacedArguments,
array $queryParameters = [],
?string $fallbackRouteName = null
): string;

public function getUriPrefix(): string;

Expand All @@ -69,9 +68,7 @@ public function setUriPrefix(string $name): void;
* Set default argument value.
*
* @param string $name Name of argument to provide default value for.
* @param mixed $value Default value.
*
* @psalm-param null|Stringable|scalar $value
* @param bool|float|int|string|Stringable|null $value Default value.
*/
public function setDefaultArgument(string $name, mixed $value): void;
public function setDefaultArgument(string $name, bool|float|int|string|Stringable|null $value): void;
}
2 changes: 1 addition & 1 deletion src/UrlMatcherInterface.php
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ServerRequestInterface;

/**
* UrlMatcherInterface allows finding a matching route given a PSR-8 server request. It is preferred to type-hint
* `UrlMatcherInterface` allows finding a matching route given a PSR-8 server request. It is preferred to type-hint
* against it in case you need to match URL.
*/
interface UrlMatcherInterface
Expand Down

0 comments on commit f813e56

Please sign in to comment.