diff --git a/CHANGELOG.md b/CHANGELOG.md index 6078223..170cb85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `Innminf\Framework\Application::notFoundRequestHandler()` callable must now return an `Innmind\Immutable\Attempt` - `Innminf\Framework\Application::notFoundRequestHandler()` has been renamed `::routeNotFound()` - `Innmind\Framework\Application::mapCommand()` callable now longer has access to `OperatingSystem` and `Environment` (use services instead) +- `Innmind\Framework\Application::run()` always return an `Innmind\Immutable\Attempt` ### Removed diff --git a/src/Application.php b/src/Application.php index 96a8be7..2e30aab 100644 --- a/src/Application.php +++ b/src/Application.php @@ -27,7 +27,7 @@ /** * @template I of ServerRequest|CliEnv - * @template O of Response|Attempt + * @template O of Response|CliEnv */ final class Application { @@ -55,7 +55,7 @@ public static function http(OperatingSystem $os, Environment $env): self /** * @psalm-pure * - * @return self> + * @return self */ #[\NoDiscard] public static function cli(OperatingSystem $os, Environment $env): self @@ -67,7 +67,7 @@ public static function cli(OperatingSystem $os, Environment $env): self * @psalm-pure * @experimental * - * @return self> + * @return self */ #[\NoDiscard] public static function asyncHttp(OperatingSystem $os): self @@ -230,10 +230,10 @@ public function recoverRouteError(callable $recover): self /** * @param I $input * - * @return O + * @return Attempt */ #[\NoDiscard] - public function run(CliEnv|ServerRequest $input): Attempt|Response + public function run(CliEnv|ServerRequest $input): Attempt { return $this->app->run($input); } diff --git a/src/Application/Async/Http.php b/src/Application/Async/Http.php index 9114d0f..6e9075f 100644 --- a/src/Application/Async/Http.php +++ b/src/Application/Async/Http.php @@ -37,7 +37,7 @@ /** * @experimental * @internal - * @implements Implementation> + * @implements Implementation */ final class Http implements Implementation { @@ -247,7 +247,7 @@ public function recoverRouteError(callable $recover): self } #[\Override] - public function run($input) + public function run($input): Attempt { $map = $this->map; $container = $this->container; @@ -284,7 +284,7 @@ static function(ServerRequest $request, OperatingSystem $os) use ( static fn($request, $e) => $recover($request, $e, $container), ); - return $router($request); + return $router($request)->unwrap(); }, )); diff --git a/src/Application/Cli.php b/src/Application/Cli.php index 6dd050b..d5bd9b1 100644 --- a/src/Application/Cli.php +++ b/src/Application/Cli.php @@ -26,7 +26,7 @@ /** * @internal - * @implements Implementation> + * @implements Implementation */ final class Cli implements Implementation { @@ -197,7 +197,7 @@ public function recoverRouteError(callable $recover): self } #[\Override] - public function run($input) + public function run($input): Attempt { $container = ($this->container)($this->os, $this->env)->build(); $mapCommand = $this->mapCommand; diff --git a/src/Application/Http.php b/src/Application/Http.php index 1338c2f..120aeb0 100644 --- a/src/Application/Http.php +++ b/src/Application/Http.php @@ -227,7 +227,7 @@ public function recoverRouteError(callable $recover): self } #[\Override] - public function run($input) + public function run($input): Attempt { $container = ($this->container)($this->os, $this->env)->build(); $mapRoute = $this->mapRoute; diff --git a/src/Application/Implementation.php b/src/Application/Implementation.php index 5b25031..9cccfd4 100644 --- a/src/Application/Implementation.php +++ b/src/Application/Implementation.php @@ -29,7 +29,7 @@ /** * @internal * @template I of ServerRequest|CliEnv - * @template O of Response|Attempt + * @template O of Response|CliEnv */ interface Implementation { @@ -117,7 +117,7 @@ public function recoverRouteError(callable $recover): self; /** * @param I $input * - * @return O + * @return Attempt */ - public function run($input); + public function run($input): Attempt; } diff --git a/src/Http/Router.php b/src/Http/Router.php index d1c3ba9..6deed48 100644 --- a/src/Http/Router.php +++ b/src/Http/Router.php @@ -39,7 +39,10 @@ public function __construct( ) { } - public function __invoke(ServerRequest $request): Response + /** + * @return Attempt + */ + public function __invoke(ServerRequest $request): Attempt { $recover = $this->recover; @@ -63,6 +66,6 @@ public function __invoke(ServerRequest $request): Response )), ); - return $route($request)->unwrap(); + return $route($request); } } diff --git a/src/Main/Async/Http.php b/src/Main/Async/Http.php index d55c40b..bab1619 100644 --- a/src/Main/Async/Http.php +++ b/src/Main/Async/Http.php @@ -19,17 +19,13 @@ abstract class Http extends Main #[\Override] protected function main(Environment $env, OperatingSystem $os): Attempt { - /** - * @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse - * @var Attempt - */ return static::configure(Application::asyncHttp($os))->run($env); } /** - * @param Application> $app + * @param Application $app * - * @return Application> + * @return Application */ abstract protected function configure(Application $app): Application; } diff --git a/src/Main/Cli.php b/src/Main/Cli.php index f41e7cd..a8eba07 100644 --- a/src/Main/Cli.php +++ b/src/Main/Cli.php @@ -19,17 +19,13 @@ abstract class Cli extends Main #[\Override] protected function main(Environment $env, OperatingSystem $os): Attempt { - /** - * @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse - * @var Attempt - */ return static::configure(Application::cli($os, AppEnv::of($env->variables())))->run($env); } /** - * @param Application> $app + * @param Application $app * - * @return Application> + * @return Application */ abstract protected function configure(Application $app): Application; } diff --git a/src/Main/Http.php b/src/Main/Http.php index c1c5e98..3dfa43d 100644 --- a/src/Main/Http.php +++ b/src/Main/Http.php @@ -17,6 +17,7 @@ abstract class Http extends Main { + /** @var Application */ private Application $app; #[\Override] @@ -28,11 +29,7 @@ protected function preload(OperatingSystem $os, Environment $env): void #[\Override] protected function main(ServerRequest $request): Response { - /** - * @psalm-suppress InvalidReturnStatement Let the app crash in case of a misuse - * @var Response - */ - return $this->app->run($request); + return $this->app->run($request)->unwrap(); } /** diff --git a/src/Middleware.php b/src/Middleware.php index de06db0..2098a51 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -8,13 +8,12 @@ Response, }; use Innmind\CLI\Environment as CliEnv; -use Innmind\Immutable\Attempt; interface Middleware { /** * @template I of ServerRequest|CliEnv - * @template O of Response|Attempt + * @template O of Response|CliEnv * * @param Application $app * diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index b07c450..2ec7d4b 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -912,7 +912,7 @@ public function testHttpApplicationReturnsNotFoundByDefault(): BlackBox\Proof $url, $method, $protocol, - )); + ))->unwrap(); $this->assertSame(StatusCode::notFound, $response->statusCode()); $this->assertSame($protocol, $response->protocolVersion()); @@ -962,7 +962,7 @@ public function testMatchRoutes(): BlackBox\Proof Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($responseA, $response); @@ -970,7 +970,7 @@ public function testMatchRoutes(): BlackBox\Proof Url::of('/bar'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($responseB, $response); }); @@ -1019,7 +1019,7 @@ public function testRouteShortDeclaration(): BlackBox\Proof Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($responseA, $response); @@ -1027,7 +1027,7 @@ public function testRouteShortDeclaration(): BlackBox\Proof Url::of('/bar'), Method::post, $protocol, - )); + ))->unwrap(); $this->assertSame($responseB, $response); }); @@ -1071,7 +1071,7 @@ public function __invoke() Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($expected, $response); }); @@ -1114,7 +1114,7 @@ public function __invoke() Url::of('/foo'), $method, $protocol, - )); + ))->unwrap(); $this->assertSame($expected, $response); }); @@ -1149,7 +1149,7 @@ public function testAllowToSpecifyHttpNotFoundRequestHandler(): BlackBox\Proof $url, $method, $protocol, - )); + ))->unwrap(); $this->assertSame($expected, $response); }); @@ -1189,7 +1189,7 @@ public function testMatchMethodAllowed(): BlackBox\Proof Url::of('/foo'), Method::head, $protocol, - )); + ))->unwrap(); $this->assertSame(405, $response->statusCode()->toInt()); $this->assertSame($protocol, $response->protocolVersion()); @@ -1240,7 +1240,7 @@ public function __invoke() Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($expected, $response); }); @@ -1272,7 +1272,7 @@ public function testRoutesAsEnumCases(): BlackBox\Proof Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($responseA, $response); @@ -1280,7 +1280,7 @@ public function testRoutesAsEnumCases(): BlackBox\Proof Url::of('/bar'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($responseB, $response); }); @@ -1311,7 +1311,7 @@ public function testRecoverRouteError(): BlackBox\Proof Url::of('/foo'), Method::get, $protocol, - )); + ))->unwrap(); $this->assertSame($expected, $response); });