From 455f6b65a2a18e02b5baf6da6ce741ee99ac8e07 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Wed, 17 Jul 2024 15:50:37 +0200 Subject: [PATCH] add types --- phpstan-baseline.neon | 118 ++++++++++++++++++++++++++++++++++++++---- src/RouteResolver.php | 15 ++++-- 2 files changed, 120 insertions(+), 13 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 9ea9585..22787ac 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,21 +1,121 @@ parameters: ignoreErrors: - - message: "#^Call to an undefined method Pest\\\\PendingCalls\\\\TestCall\\|Pest\\\\Support\\\\HigherOrderTapProxy\\:\\:get\\(\\)\\.$#" - count: 2 - path: src/RouteTesting.php + message: "#^Function Spatie\\\\RouteTesting\\\\routeTesting\\(\\) has no return type specified\\.$#" + count: 1 + path: src/Autoload.php + + - + message: "#^Parameter \\#1 \\$testCall of class Spatie\\\\RouteTesting\\\\RouteTestingTestCall constructor expects Pest\\\\PendingCalls\\\\TestCall, Pest\\\\PendingCalls\\\\TestCall\\|Pest\\\\Support\\\\HigherOrderTapProxy given\\.$#" + count: 1 + path: src/Autoload.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:bindingNames\\(\\) has parameter \\$bindingNames with no value type specified in iterable type array\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:exceptPaths\\(\\) has parameter \\$paths with no value type specified in iterable type array\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:getBindingsFromUrl\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:getFilteredRouteList\\(\\) should return array\\ but returns array\\\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:paths\\(\\) has parameter \\$paths with no value type specified in iterable type array\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteResolver\\:\\:resolveFullRouteList\\(\\) should return Illuminate\\\\Support\\\\Collection\\ but returns Illuminate\\\\Support\\\\Collection\\\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),mixed\\>\\:\\:flatMap\\(\\) expects callable\\(mixed, int\\|string\\)\\: \\(array\\\\|Illuminate\\\\Support\\\\Collection\\\\), Closure\\(array\\)\\: Illuminate\\\\Support\\\\Collection\\ given\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Parameter \\#1 \\$value of function collect expects Illuminate\\\\Contracts\\\\Support\\\\Arrayable\\<\\(int\\|string\\), mixed\\>\\|iterable\\<\\(int\\|string\\), mixed\\>\\|null, mixed given\\.$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Unable to resolve the template type TKey in call to function collect$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Unable to resolve the template type TValue in call to function collect$#" + count: 1 + path: src/RouteResolver.php + + - + message: "#^Call to an undefined method Pest\\\\PendingCalls\\\\TestCall\\:\\:defer\\(\\)\\.$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Cannot call method toRoute\\(\\) on Illuminate\\\\Contracts\\\\Routing\\\\UrlGenerator\\|string\\.$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Method Spatie\\\\RouteTesting\\\\RouteTest\\:\\:test\\(\\) has parameter \\$assertions with no value type specified in iterable type array\\.$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),mixed\\>\\:\\:first\\(\\) expects \\(callable\\(mixed, int\\|string\\)\\: bool\\)\\|null, Closure\\(Illuminate\\\\Routing\\\\Route\\)\\: bool given\\.$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Unable to resolve the template type TKey in call to function collect$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Unable to resolve the template type TValue in call to function collect$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Undefined variable\\: \\$this$#" + count: 6 + path: src/RouteTest.php + + - + message: "#^Variable \\$this on left side of \\?\\? is never defined\\.$#" + count: 1 + path: src/RouteTest.php + + - + message: "#^Call to an undefined method Pest\\\\PendingCalls\\\\TestCall\\:\\:defer\\(\\)\\.$#" + count: 1 + path: src/RouteTestingTestCall.php - - message: "#^Method Spatie\\\\RouteTesting\\\\RouteTesting\\:\\:routesToAssert\\(\\) should return array\\ but returns array\\\\.$#" + message: "#^Method Spatie\\\\RouteTesting\\\\RouteTestingTestCall\\:\\:__call\\(\\) has parameter \\$method with no type specified\\.$#" count: 1 - path: src/RouteTesting.php + path: src/RouteTestingTestCall.php - - message: "#^Parameter \\#1 \\$haystack of function str_starts_with expects string, string\\|null given\\.$#" + message: "#^Method Spatie\\\\RouteTesting\\\\RouteTestingTestCall\\:\\:__call\\(\\) has parameter \\$parameters with no type specified\\.$#" count: 1 - path: src/RouteTesting.php + path: src/RouteTestingTestCall.php - - message: "#^Property Spatie\\\\RouteTesting\\\\RouteTesting\\:\\:\\$assertedRoutes \\(array\\\\) does not accept array\\.$#" + message: "#^Method Spatie\\\\RouteTesting\\\\RouteTestingTestCall\\:\\:with\\(\\) has parameter \\$routes with no value type specified in iterable type array\\.$#" count: 1 - path: src/RouteTesting.php + path: src/RouteTestingTestCall.php diff --git a/src/RouteResolver.php b/src/RouteResolver.php index a86f771..bfa89ed 100644 --- a/src/RouteResolver.php +++ b/src/RouteResolver.php @@ -8,14 +8,18 @@ class RouteResolver { + /** @var array|null */ protected ?array $paths = null; + /** @var array|null */ protected ?array $exceptPaths = null; + /** @var array */ protected array $methods = ['GET']; protected bool $exceptRoutesWithMissingBindings = false; + /** @var array */ protected array $bindingNames = []; /** @var Collection */ @@ -34,7 +38,7 @@ protected function resolveFullRouteList(): Collection $routes = json_decode($result->output(), true); return collect($routes)->flatMap( - fn ($route) => Str::of($route['method']) + fn (array $route) => Str::of($route['method']) ->explode('|') ->intersect($this->methods) ->map(fn ($method) => ['method' => $method, 'uri' => $route['uri']]) @@ -69,17 +73,20 @@ public function exceptRoutesWithMissingBindings(): self return $this; } + /** + * @return array + */ public function getFilteredRouteList(): array { return $this->fullRouteList - ->filter(function ($route) { + ->filter(function (array $route) { if ($this->paths) { return collect($this->paths)->contains(fn ($path) => Str::is($path, $route['uri'])); } return true; }) - ->filter(function ($route) { + ->filter(function (array $route) { if ($this->exceptPaths) { return ! collect($this->exceptPaths)->contains(fn ($path) => Str::is($path, $route['uri'])); } @@ -87,7 +94,7 @@ public function getFilteredRouteList(): array return true; }) ->when($this->exceptRoutesWithMissingBindings, function (Collection $routes) { - return $routes->filter(function ($route) { + return $routes->filter(function (array $route) { $uriBindings = $this->getBindingsFromUrl($route['uri']); if (count($uriBindings) === 0) {