From dd8b67060045a297a1fbc7af2feff3a8638b8d9a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Jul 2022 05:18:15 +0700 Subject: [PATCH 1/4] Apply PHP 7.4 syntax and typed property Signed-off-by: Abdul Malik Ikhsan --- src/EmptyPipelineHandler.php | 3 +- src/Handler/NotFoundHandler.php | 4 +-- .../DoublePassMiddlewareDecorator.php | 7 ++--- src/Middleware/ErrorHandler.php | 9 ++---- src/Middleware/ErrorResponseGenerator.php | 3 +- src/Middleware/HostMiddlewareDecorator.php | 5 ++-- src/Middleware/NotFoundHandler.php | 3 +- src/Middleware/PathMiddlewareDecorator.php | 11 +++---- src/MiddlewarePipe.php | 3 +- src/Next.php | 6 ++-- test/Handler/NotFoundHandlerTest.php | 4 +-- .../CallableMiddlewareDecoratorTest.php | 12 ++------ ...lePassMiddlewareDecoratorExceptionTest.php | 7 ++--- .../DoublePassMiddlewareDecoratorTest.php | 20 ++++--------- test/Middleware/ErrorHandlerTest.php | 15 +++------- test/Middleware/NotFoundHandlerTest.php | 4 +-- ...PathMiddlewareDecoratorIntegrationTest.php | 29 ++++--------------- .../PathMiddlewareDecoratorTest.php | 29 +++++-------------- .../RequestHandlerMiddlewareTest.php | 3 +- test/MiddlewarePipeTest.php | 10 ++----- test/MiddlewareTrait.php | 4 +-- test/NextTest.php | 16 ++++------ 22 files changed, 57 insertions(+), 150 deletions(-) diff --git a/src/EmptyPipelineHandler.php b/src/EmptyPipelineHandler.php index 87ed621..7df0ced 100644 --- a/src/EmptyPipelineHandler.php +++ b/src/EmptyPipelineHandler.php @@ -10,8 +10,7 @@ final class EmptyPipelineHandler implements RequestHandlerInterface { - /** @var string */ - private $className; + private string $className; public function __construct(string $className) { diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php index efd1bb7..25d2b50 100644 --- a/src/Handler/NotFoundHandler.php +++ b/src/Handler/NotFoundHandler.php @@ -23,9 +23,7 @@ final class NotFoundHandler implements RequestHandlerInterface */ public function __construct(callable $responseFactory) { - $this->responseFactory = function () use ($responseFactory): ResponseInterface { - return $responseFactory(); - }; + $this->responseFactory = static fn(): ResponseInterface => $responseFactory(); } /** diff --git a/src/Middleware/DoublePassMiddlewareDecorator.php b/src/Middleware/DoublePassMiddlewareDecorator.php index 9a9fbf4..7976d16 100644 --- a/src/Middleware/DoublePassMiddlewareDecorator.php +++ b/src/Middleware/DoublePassMiddlewareDecorator.php @@ -36,8 +36,7 @@ final class DoublePassMiddlewareDecorator implements MiddlewareInterface /** @var callable */ private $middleware; - /** @var ResponseInterface */ - private $responsePrototype; + private ResponseInterface $responsePrototype; /** * @throws Exception\MissingResponsePrototypeException If no response @@ -77,8 +76,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface private function decorateHandler(RequestHandlerInterface $handler): callable { - return function ($request, $response) use ($handler) { - return $handler->handle($request); - }; + return static fn($request, $response) => $handler->handle($request); } } diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php index b9343e3..3de31da 100644 --- a/src/Middleware/ErrorHandler.php +++ b/src/Middleware/ErrorHandler.php @@ -65,7 +65,7 @@ class ErrorHandler implements MiddlewareInterface { /** @var callable[] */ - private $listeners = []; + private array $listeners = []; /** @var callable Routine that will generate the error response. */ private $responseGenerator; @@ -82,9 +82,7 @@ class ErrorHandler implements MiddlewareInterface */ public function __construct(callable $responseFactory, ?callable $responseGenerator = null) { - $this->responseFactory = function () use ($responseFactory): ResponseInterface { - return $responseFactory(); - }; + $this->responseFactory = static fn(): ResponseInterface => $responseFactory(); $this->responseGenerator = $responseGenerator ?: new ErrorResponseGenerator(); } @@ -163,12 +161,11 @@ private function createErrorHandler(): callable /** * @throws ErrorException if error is not within the error_reporting mask. */ - return function (int $errno, string $errstr, string $errfile, int $errline): void { + return static function (int $errno, string $errstr, string $errfile, int $errline): void { if (! (error_reporting() & $errno)) { // error_reporting does not include this error return; } - throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }; } diff --git a/src/Middleware/ErrorResponseGenerator.php b/src/Middleware/ErrorResponseGenerator.php index c761604..ebde089 100644 --- a/src/Middleware/ErrorResponseGenerator.php +++ b/src/Middleware/ErrorResponseGenerator.php @@ -12,8 +12,7 @@ final class ErrorResponseGenerator { - /** @var bool */ - private $isDevelopmentMode; + private bool $isDevelopmentMode; public function __construct(bool $isDevelopmentMode = false) { diff --git a/src/Middleware/HostMiddlewareDecorator.php b/src/Middleware/HostMiddlewareDecorator.php index 2e8202b..67609ba 100644 --- a/src/Middleware/HostMiddlewareDecorator.php +++ b/src/Middleware/HostMiddlewareDecorator.php @@ -13,11 +13,10 @@ final class HostMiddlewareDecorator implements MiddlewareInterface { - /** @var MiddlewareInterface */ - private $middleware; + private MiddlewareInterface $middleware; /** @var string Host name under which the middleware is segregated. */ - private $host; + private string $host; public function __construct(string $host, MiddlewareInterface $middleware) { diff --git a/src/Middleware/NotFoundHandler.php b/src/Middleware/NotFoundHandler.php index d0dcdc5..26efb2f 100644 --- a/src/Middleware/NotFoundHandler.php +++ b/src/Middleware/NotFoundHandler.php @@ -15,8 +15,7 @@ */ final class NotFoundHandler implements MiddlewareInterface { - /** @var NotFoundRequestHandler */ - private $notFoundHandler; + private NotFoundRequestHandler $notFoundHandler; /** * @param callable $responseFactory A factory capable of returning an diff --git a/src/Middleware/PathMiddlewareDecorator.php b/src/Middleware/PathMiddlewareDecorator.php index beac054..ae83d29 100644 --- a/src/Middleware/PathMiddlewareDecorator.php +++ b/src/Middleware/PathMiddlewareDecorator.php @@ -17,11 +17,10 @@ final class PathMiddlewareDecorator implements MiddlewareInterface { - /** @var MiddlewareInterface */ - private $middleware; + private MiddlewareInterface $middleware; /** @var string Path prefix under which the middleware is segregated. */ - private $prefix; + private string $prefix; public function __construct(string $prefix, MiddlewareInterface $middleware) { @@ -96,11 +95,9 @@ private function getTruncatedPath(string $segment, string $path): string private function prepareHandlerForOriginalRequest(RequestHandlerInterface $handler): RequestHandlerInterface { return new class ($handler, $this->prefix) implements RequestHandlerInterface { - /** @var RequestHandlerInterface */ - private $handler; + private RequestHandlerInterface $handler; - /** @var string */ - private $prefix; + private string $prefix; public function __construct(RequestHandlerInterface $handler, string $prefix) { diff --git a/src/MiddlewarePipe.php b/src/MiddlewarePipe.php index e6223e8..2616fa2 100644 --- a/src/MiddlewarePipe.php +++ b/src/MiddlewarePipe.php @@ -26,8 +26,7 @@ */ final class MiddlewarePipe implements MiddlewarePipeInterface { - /** @var SplQueue */ - private $pipeline; + private SplQueue $pipeline; /** * Initializes the queue. diff --git a/src/Next.php b/src/Next.php index 4c66063..c598417 100644 --- a/src/Next.php +++ b/src/Next.php @@ -15,11 +15,9 @@ */ final class Next implements RequestHandlerInterface { - /** @var RequestHandlerInterface */ - private $fallbackHandler; + private RequestHandlerInterface $fallbackHandler; - /** @var null|SplQueue */ - private $queue; + private ?SplQueue $queue; /** * Clones the queue provided to allow re-use. diff --git a/test/Handler/NotFoundHandlerTest.php b/test/Handler/NotFoundHandlerTest.php index 582c529..d944275 100644 --- a/test/Handler/NotFoundHandlerTest.php +++ b/test/Handler/NotFoundHandlerTest.php @@ -42,9 +42,7 @@ public function testReturnsResponseWith404StatusAndErrorMessageInBody(): void ->method('getUri') ->willReturn('https://example.com/foo'); - $responseFactory = static function () use ($response): ResponseInterface { - return $response; - }; + $responseFactory = static fn(): ResponseInterface => $response; $middleware = new NotFoundHandler($responseFactory); diff --git a/test/Middleware/CallableMiddlewareDecoratorTest.php b/test/Middleware/CallableMiddlewareDecoratorTest.php index a924116..546f198 100644 --- a/test/Middleware/CallableMiddlewareDecoratorTest.php +++ b/test/Middleware/CallableMiddlewareDecoratorTest.php @@ -20,9 +20,7 @@ public function testCallableMiddlewareThatDoesNotProduceAResponseRaisesAnExcepti $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = function (): string { - return 'foo'; - }; + $middleware = static fn(): string => 'foo'; $decorator = new CallableMiddlewareDecorator($middleware); @@ -37,9 +35,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v $handler = $this->createMock(RequestHandlerInterface::class); $response = $this->createMock(ResponseInterface::class); - $middleware = function ($request, $handler) use ($response): ResponseInterface { - return $response; - }; + $middleware = static fn($request, $handler): ResponseInterface => $response; $decorator = new CallableMiddlewareDecorator($middleware); @@ -48,9 +44,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v public function testMiddlewareFunction(): void { - $toDecorate = static function (): string { - return 'foo'; - }; + $toDecorate = static fn(): string => 'foo'; $middleware = middleware($toDecorate); self::assertInstanceOf(CallableMiddlewareDecorator::class, $middleware); diff --git a/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php b/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php index 98d05fb..53bc063 100644 --- a/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php +++ b/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php @@ -15,8 +15,7 @@ class DoublePassMiddlewareDecoratorExceptionTest extends TestCase { - /** @var array */ - private $autoloadFunctions = []; + private array $autoloadFunctions = []; protected function setUp(): void { @@ -38,9 +37,7 @@ private function reloadAutoloaders(): void public function testDiactorosIsNotAvailableAndResponsePrototypeIsNotSet(): void { - $middleware = function ($request, $response, $next) { - return $response; - }; + $middleware = static fn($request, $response, $next) => $response; $this->expectException(MissingResponsePrototypeException::class); $this->expectExceptionMessage( diff --git a/test/Middleware/DoublePassMiddlewareDecoratorTest.php b/test/Middleware/DoublePassMiddlewareDecoratorTest.php index 44160b0..f7fe4ee 100644 --- a/test/Middleware/DoublePassMiddlewareDecoratorTest.php +++ b/test/Middleware/DoublePassMiddlewareDecoratorTest.php @@ -22,9 +22,7 @@ public function testCallableMiddlewareThatDoesNotProduceAResponseRaisesAnExcepti $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = function ($request, $response, $next): string { - return 'foo'; - }; + $middleware = static fn($request, $response, $next): string => 'foo'; $decorator = new DoublePassMiddlewareDecorator($middleware, $response); @@ -39,9 +37,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = function ($request, $response, $next) { - return $response; - }; + $middleware = static fn($request, $response, $next) => $response; $decorator = new DoublePassMiddlewareDecorator($middleware, $response); @@ -61,9 +57,7 @@ public function testCallableMiddlewareCanDelegateViaHandler(): void ->willReturn($response); $middleware = /** @psalm-param callable(ServerRequestInterface,ResponseInterface):ResponseInterface $next */ - function (ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface { - return $next($request, $response); - }; + static fn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface => $next($request, $response); $decorator = new DoublePassMiddlewareDecorator($middleware, $response); @@ -78,9 +72,7 @@ public function testDecoratorCreatesAResponsePrototypeIfNoneIsProvided(): void $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = function ($request, $response, $next) { - return $response; - }; + $middleware = static fn($request, $response, $next) => $response; $decorator = new DoublePassMiddlewareDecorator($middleware); @@ -91,9 +83,7 @@ public function testDecoratorCreatesAResponsePrototypeIfNoneIsProvided(): void public function testDoublePassMiddlewareFunction(): void { - $toDecorate = function ($request, $response, $next): string { - return 'foo'; - }; + $toDecorate = static fn($request, $response, $next): string => 'foo'; $response = $this->createMock(ResponseInterface::class); diff --git a/test/Middleware/ErrorHandlerTest.php b/test/Middleware/ErrorHandlerTest.php index 00e5d5d..1e81fbc 100644 --- a/test/Middleware/ErrorHandlerTest.php +++ b/test/Middleware/ErrorHandlerTest.php @@ -39,16 +39,13 @@ class ErrorHandlerTest extends TestCase /** @var callable():ResponseInterface */ private $responseFactory; - /** @var int */ - private $errorReporting; + private int $errorReporting; protected function setUp(): void { $this->response = $this->createMock(ResponseInterface::class); $response = $this->response; - $this->responseFactory = static function () use ($response): ResponseInterface { - return $response; - }; + $this->responseFactory = static fn(): ResponseInterface => $response; $this->request = $this->createMock(ServerRequestInterface::class); $this->body = $this->createMock(StreamInterface::class); @@ -93,7 +90,7 @@ public function testReturnsErrorResponseIfHandlerRaisesAnErrorInTheErrorMask(): $this->handler ->method('handle') ->with($this->request) - ->will(self::returnCallback(function () { + ->will(self::returnCallback(static function () { trigger_error('Deprecated', E_USER_DEPRECATED); })); @@ -278,11 +275,7 @@ public function testErrorHandlingTriggersListeners(): void public function testCanProvideAlternateErrorResponseGenerator(): void { - $generator = function ( - Throwable $e, - ServerRequestInterface $request, - ResponseInterface $response - ): ResponseInterface { + $generator = static function (Throwable $e, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { $response = $response->withStatus(400); $response->getBody()->write('The client messed up'); return $response; diff --git a/test/Middleware/NotFoundHandlerTest.php b/test/Middleware/NotFoundHandlerTest.php index a42d690..797e992 100644 --- a/test/Middleware/NotFoundHandlerTest.php +++ b/test/Middleware/NotFoundHandlerTest.php @@ -39,9 +39,7 @@ public function testReturnsResponseWith404StatusAndErrorMessageInBody(): void ->method('getUri') ->willReturn('https://example.com/foo'); - $responseFactory = static function () use ($response): ResponseInterface { - return $response; - }; + $responseFactory = static fn(): ResponseInterface => $response; $middleware = new NotFoundHandler($responseFactory); diff --git a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php index 483336e..9e15349 100644 --- a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php +++ b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php @@ -27,7 +27,7 @@ public function testPipelineComposingPathDecoratedMiddlewareExecutesAsExpected() $pipeline = new MiddlewarePipe(); - $first = $this->createPassThroughMiddleware(function ($received) use ($request) { + $first = $this->createPassThroughMiddleware(static function ($received) use ($request) { self::assertSame( $request, $received, @@ -36,28 +36,24 @@ public function testPipelineComposingPathDecoratedMiddlewareExecutesAsExpected() return $request; }); $second = new PathMiddlewareDecorator('/foo', $this->createNestedPipeline($request)); - $last = $this->createPassThroughMiddleware(function ($received) use ($request) { + $last = $this->createPassThroughMiddleware(static function ($received) use ($request) { self::assertNotSame( $request, $received, 'Last middleware received original request, but should not have' ); - $originalUri = $request->getUri(); $receivedUri = $received->getUri(); - Assert::assertNotSame( $originalUri, $receivedUri, 'Last middleware received original URI instance, but should not have' ); - Assert::assertSame( $originalUri->getPath(), $receivedUri->getPath(), 'Last middleware did not receive original URI path, but should have' ); - return $request; }); @@ -103,12 +99,7 @@ static function ($argument2): bool { ) ->will( self::returnCallback( - static function ( - ServerRequestInterface $request, - RequestHandlerInterface $next - ): ResponseInterface { - return $next->handle($request); - } + static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) ) ); @@ -140,12 +131,7 @@ static function ($request) use ($originalRequest) { } ) ) - ->willReturnCallback(static function ( - ServerRequestInterface $request, - RequestHandlerInterface $next - ): ResponseInterface { - return $next->handle($request); - }); + ->willReturnCallback(static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request)); $decorated = new PathMiddlewareDecorator('/bar', $barMiddleware); @@ -169,12 +155,7 @@ static function ($request) use ($originalRequest) { }) ) ->willReturnCallback( - static function ( - ServerRequestInterface $request, - RequestHandlerInterface $next - ): ResponseInterface { - return $next->handle($request); - } + static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) ); $pipeline->pipe($decorated); diff --git a/test/Middleware/PathMiddlewareDecoratorTest.php b/test/Middleware/PathMiddlewareDecoratorTest.php index 0b00408..14a33d4 100644 --- a/test/Middleware/PathMiddlewareDecoratorTest.php +++ b/test/Middleware/PathMiddlewareDecoratorTest.php @@ -262,8 +262,7 @@ public function process( }); $topLevel = new PathMiddlewareDecorator($prefix, new class ($nested) implements MiddlewareInterface { - /** @var MiddlewareInterface */ - private $middleware; + private MiddlewareInterface $middleware; public function __construct(MiddlewareInterface $middleware) { @@ -346,9 +345,8 @@ public function testRequestPathPassedToDecoratedMiddlewareTrimsPathPrefix(): voi ->expects(self::once()) ->method('process') ->with( - self::callback(function (ServerRequestInterface $req): bool { + self::callback(static function (ServerRequestInterface $req): bool { self::assertSame('/bar', $req->getUri()->getPath()); - return true; }) ) @@ -367,19 +365,17 @@ public function testInvocationOfHandlerByDecoratedMiddlewareWillInvokeWithOrigin $finalHandler ->method('handle') ->with( - self::callback(function ($received) use ($request) { + self::callback(static function ($received) use ($request) { self::assertNotSame( $request, $received, 'Final handler received same request, and should not have' ); - self::assertSame( $request->getUri()->getPath(), $received->getUri()->getPath(), 'Final handler received request with different path' ); - return true; }) ) @@ -389,7 +385,7 @@ public function testInvocationOfHandlerByDecoratedMiddlewareWillInvokeWithOrigin $segregatedMiddleware ->method('process') ->with( - self::callback(function ($received) use ($request) { + self::callback(static function ($received) use ($request) { Assert::assertNotSame( $request, $received, @@ -399,9 +395,7 @@ public function testInvocationOfHandlerByDecoratedMiddlewareWillInvokeWithOrigin }) ) ->willReturnCallback( - static function (ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface { - return $next->handle($request); - } + static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) ); $decoratedMiddleware = new PathMiddlewareDecorator('/test', $segregatedMiddleware); @@ -422,21 +416,15 @@ public function testPathFunction(): void public function testUpdatesInPathInsideNestedMiddlewareAreRespected(): void { $request = new ServerRequest([], [], 'http://local.example.com/foo/bar', 'GET', 'php://memory'); - $decoratedMiddleware = middleware(function ( - ServerRequestInterface $request, - RequestHandlerInterface $handler - ) { - return $handler->handle($request->withUri(new Uri('/changed/path'))); - }); + $decoratedMiddleware = middleware(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) => $handler->handle($request->withUri(new Uri('/changed/path')))); $middleware = new PathMiddlewareDecorator('/foo', $decoratedMiddleware); $handler = $this->createMock(RequestHandlerInterface::class); $handler ->method('handle') ->with( - self::callback(function (ServerRequestInterface $received): bool { + self::callback(static function (ServerRequestInterface $received): bool { self::assertEquals('/foo/changed/path', $received->getUri()->getPath()); - return true; }) ); @@ -459,9 +447,8 @@ public function testProcessesMatchedPathsWithoutCaseSensitivity(): void ->expects(self::once()) ->method('process') ->with( - self::callback(function (ServerRequestInterface $req) { + self::callback(static function (ServerRequestInterface $req) { Assert::assertSame('', $req->getUri()->getPath()); - return true; }) ) diff --git a/test/Middleware/RequestHandlerMiddlewareTest.php b/test/Middleware/RequestHandlerMiddlewareTest.php index 07d63a9..fa548db 100644 --- a/test/Middleware/RequestHandlerMiddlewareTest.php +++ b/test/Middleware/RequestHandlerMiddlewareTest.php @@ -22,8 +22,7 @@ class RequestHandlerMiddlewareTest extends TestCase /** @var MockObject&RequestHandlerInterface */ private $handler; - /** @var RequestHandlerMiddleware */ - private $middleware; + private RequestHandlerMiddleware $middleware; protected function setUp(): void { diff --git a/test/MiddlewarePipeTest.php b/test/MiddlewarePipeTest.php index cadc736..44dbe0c 100644 --- a/test/MiddlewarePipeTest.php +++ b/test/MiddlewarePipeTest.php @@ -28,11 +28,9 @@ class MiddlewarePipeTest extends TestCase { use MiddlewareTrait; - /** @var Request */ - private $request; + private Request $request; - /** @var MiddlewarePipe */ - private $pipeline; + private MiddlewarePipe $pipeline; protected function setUp(): void { @@ -179,9 +177,7 @@ public function testHandleProcessesEnqueuedMiddleware(): void $this->request ) ->willReturnCallback( - static function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - return $handler->handle($request); - } + static fn(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface => $handler->handle($request) ); $middleware2 = $this->createMock(MiddlewareInterface::class); $middleware2 diff --git a/test/MiddlewareTrait.php b/test/MiddlewareTrait.php index 793ad9a..a93cb57 100644 --- a/test/MiddlewareTrait.php +++ b/test/MiddlewareTrait.php @@ -27,9 +27,7 @@ private function getPassToHandlerMiddleware(): MiddlewareInterface $middleware ->expects(self::once()) ->method('process') - ->willReturnCallback(static function (ServerRequestInterface $request, RequestHandlerInterface $handler) { - return $handler->handle($request); - }); + ->willReturnCallback(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) => $handler->handle($request)); return $middleware; } diff --git a/test/NextTest.php b/test/NextTest.php index b6cc15c..e4cf235 100644 --- a/test/NextTest.php +++ b/test/NextTest.php @@ -24,8 +24,7 @@ class NextTest extends TestCase { use MiddlewareTrait; - /** @var SplQueue */ - private $queue; + private SplQueue $queue; private Request $request; @@ -43,8 +42,7 @@ public function createFallbackHandler(?ResponseInterface $response = null): Requ { $response = $response ?: $this->createDefaultResponse(); return new class ($response) implements RequestHandlerInterface { - /** @var ResponseInterface */ - private $response; + private ResponseInterface $response; public function __construct(ResponseInterface $response) { @@ -82,8 +80,7 @@ public function testMiddlewareCallingNextWithRequestPassesRequestToNextMiddlewar $middleware1 = new class ($cannedRequest) implements MiddlewareInterface { - /** @var ServerRequestInterface */ - private $cannedRequest; + private ServerRequestInterface $cannedRequest; public function __construct(ServerRequestInterface $cannedRequest) { @@ -98,8 +95,7 @@ public function process(ServerRequestInterface $req, RequestHandlerInterface $ha $middleware2 = new class ($cannedRequest) implements MiddlewareInterface { - /** @var ServerRequestInterface */ - private $cannedRequest; + private ServerRequestInterface $cannedRequest; public function __construct(ServerRequestInterface $cannedRequest) { @@ -241,9 +237,7 @@ public function testSecondInvocationAttemptDoesNotInvokeMiddleware(): void ->expects(self::once()) ->method('process') ->willReturnCallback( - static function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - return $handler->handle($request); - } + static fn(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface => $handler->handle($request) ); $this->queue->push($middleware); From 829d8b38873e63a95447dbd4da345f75eb528a68 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Jul 2022 05:35:24 +0700 Subject: [PATCH 2/4] psalm fix Signed-off-by: Abdul Malik Ikhsan --- psalm-baseline.xml | 7 ------- .../DoublePassMiddlewareDecoratorExceptionTest.php | 3 ++- test/Middleware/DoublePassMiddlewareDecoratorTest.php | 4 ++-- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 60c10fd..05fe999 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -134,9 +134,6 @@ $request $response - - function ($request, $response, $next) { - $autoloader @@ -163,10 +160,6 @@ $response $response - - function ($request, $response, $next) { - function ($request, $response, $next) { - $next $next diff --git a/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php b/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php index 53bc063..fdf3e9a 100644 --- a/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php +++ b/test/Middleware/DoublePassMiddlewareDecoratorExceptionTest.php @@ -7,6 +7,7 @@ use Laminas\Stratigility\Exception\MissingResponsePrototypeException; use Laminas\Stratigility\Middleware\DoublePassMiddlewareDecorator; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; use function class_exists; use function spl_autoload_functions; @@ -37,7 +38,7 @@ private function reloadAutoloaders(): void public function testDiactorosIsNotAvailableAndResponsePrototypeIsNotSet(): void { - $middleware = static fn($request, $response, $next) => $response; + $middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response; $this->expectException(MissingResponsePrototypeException::class); $this->expectExceptionMessage( diff --git a/test/Middleware/DoublePassMiddlewareDecoratorTest.php b/test/Middleware/DoublePassMiddlewareDecoratorTest.php index f7fe4ee..62124ff 100644 --- a/test/Middleware/DoublePassMiddlewareDecoratorTest.php +++ b/test/Middleware/DoublePassMiddlewareDecoratorTest.php @@ -37,7 +37,7 @@ public function testCallableMiddlewareReturningAResponseSucceedsProcessCall(): v $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = static fn($request, $response, $next) => $response; + $middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response; $decorator = new DoublePassMiddlewareDecorator($middleware, $response); @@ -72,7 +72,7 @@ public function testDecoratorCreatesAResponsePrototypeIfNoneIsProvided(): void $request = $this->createMock(ServerRequestInterface::class); $handler = $this->createMock(RequestHandlerInterface::class); - $middleware = static fn($request, $response, $next) => $response; + $middleware = static fn($request, ResponseInterface $response, $next): ResponseInterface => $response; $decorator = new DoublePassMiddlewareDecorator($middleware); From 9b64e20c0e0b10cf1529e3467c6055d0a7970284 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Jul 2022 05:42:56 +0700 Subject: [PATCH 3/4] cs fix Signed-off-by: Abdul Malik Ikhsan --- src/Middleware/ErrorHandler.php | 1 + test/Middleware/DoublePassMiddlewareDecoratorTest.php | 5 ++++- test/Middleware/ErrorHandlerTest.php | 6 +++++- test/Middleware/PathMiddlewareDecoratorIntegrationTest.php | 7 +++++-- test/Middleware/PathMiddlewareDecoratorTest.php | 6 ++++-- test/MiddlewareTrait.php | 3 ++- test/NextTest.php | 5 ++++- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php index 3de31da..2cd241a 100644 --- a/src/Middleware/ErrorHandler.php +++ b/src/Middleware/ErrorHandler.php @@ -166,6 +166,7 @@ private function createErrorHandler(): callable // error_reporting does not include this error return; } + throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }; } diff --git a/test/Middleware/DoublePassMiddlewareDecoratorTest.php b/test/Middleware/DoublePassMiddlewareDecoratorTest.php index 62124ff..a12602d 100644 --- a/test/Middleware/DoublePassMiddlewareDecoratorTest.php +++ b/test/Middleware/DoublePassMiddlewareDecoratorTest.php @@ -57,7 +57,10 @@ public function testCallableMiddlewareCanDelegateViaHandler(): void ->willReturn($response); $middleware = /** @psalm-param callable(ServerRequestInterface,ResponseInterface):ResponseInterface $next */ - static fn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface => $next($request, $response); + static fn( + ServerRequestInterface $request, + ResponseInterface $response, + callable $next): ResponseInterface => $next($request, $response); $decorator = new DoublePassMiddlewareDecorator($middleware, $response); diff --git a/test/Middleware/ErrorHandlerTest.php b/test/Middleware/ErrorHandlerTest.php index 1e81fbc..b341690 100644 --- a/test/Middleware/ErrorHandlerTest.php +++ b/test/Middleware/ErrorHandlerTest.php @@ -275,7 +275,11 @@ public function testErrorHandlingTriggersListeners(): void public function testCanProvideAlternateErrorResponseGenerator(): void { - $generator = static function (Throwable $e, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { + $generator = static function ( + Throwable $e, + ServerRequestInterface $request, + ResponseInterface $response + ): ResponseInterface { $response = $response->withStatus(400); $response->getBody()->write('The client messed up'); return $response; diff --git a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php index 9e15349..7c965d9 100644 --- a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php +++ b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php @@ -99,7 +99,8 @@ static function ($argument2): bool { ) ->will( self::returnCallback( - static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) + static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface + => $next->handle($request) ) ); @@ -131,7 +132,9 @@ static function ($request) use ($originalRequest) { } ) ) - ->willReturnCallback(static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request)); + ->willReturnCallback(static fn( + ServerRequestInterface $request, + RequestHandlerInterface $next): ResponseInterface => $next->handle($request)); $decorated = new PathMiddlewareDecorator('/bar', $barMiddleware); diff --git a/test/Middleware/PathMiddlewareDecoratorTest.php b/test/Middleware/PathMiddlewareDecoratorTest.php index 14a33d4..38a2d2c 100644 --- a/test/Middleware/PathMiddlewareDecoratorTest.php +++ b/test/Middleware/PathMiddlewareDecoratorTest.php @@ -395,7 +395,8 @@ public function testInvocationOfHandlerByDecoratedMiddlewareWillInvokeWithOrigin }) ) ->willReturnCallback( - static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) + static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => + $next->handle($request) ); $decoratedMiddleware = new PathMiddlewareDecorator('/test', $segregatedMiddleware); @@ -416,7 +417,8 @@ public function testPathFunction(): void public function testUpdatesInPathInsideNestedMiddlewareAreRespected(): void { $request = new ServerRequest([], [], 'http://local.example.com/foo/bar', 'GET', 'php://memory'); - $decoratedMiddleware = middleware(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) => $handler->handle($request->withUri(new Uri('/changed/path')))); + $decoratedMiddleware = middleware(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) + => $handler->handle($request->withUri(new Uri('/changed/path')))); $middleware = new PathMiddlewareDecorator('/foo', $decoratedMiddleware); $handler = $this->createMock(RequestHandlerInterface::class); diff --git a/test/MiddlewareTrait.php b/test/MiddlewareTrait.php index a93cb57..4aa39dd 100644 --- a/test/MiddlewareTrait.php +++ b/test/MiddlewareTrait.php @@ -27,7 +27,8 @@ private function getPassToHandlerMiddleware(): MiddlewareInterface $middleware ->expects(self::once()) ->method('process') - ->willReturnCallback(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) => $handler->handle($request)); + ->willReturnCallback(static fn(ServerRequestInterface $request, RequestHandlerInterface $handler) + => $handler->handle($request)); return $middleware; } diff --git a/test/NextTest.php b/test/NextTest.php index e4cf235..1a94361 100644 --- a/test/NextTest.php +++ b/test/NextTest.php @@ -237,7 +237,10 @@ public function testSecondInvocationAttemptDoesNotInvokeMiddleware(): void ->expects(self::once()) ->method('process') ->willReturnCallback( - static fn(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface => $handler->handle($request) + static fn( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface => $handler->handle($request) ); $this->queue->push($middleware); From a1a054667c8d227688cf245440cc7217fa80fd4e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Jul 2022 05:46:02 +0700 Subject: [PATCH 4/4] cs fix Signed-off-by: Abdul Malik Ikhsan --- test/Middleware/PathMiddlewareDecoratorIntegrationTest.php | 4 +++- test/MiddlewarePipeTest.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php index 7c965d9..1b97df4 100644 --- a/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php +++ b/test/Middleware/PathMiddlewareDecoratorIntegrationTest.php @@ -158,7 +158,9 @@ static function ($request) use ($originalRequest) { }) ) ->willReturnCallback( - static fn(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface => $next->handle($request) + static fn( + ServerRequestInterface $request, + RequestHandlerInterface $next): ResponseInterface => $next->handle($request) ); $pipeline->pipe($decorated); diff --git a/test/MiddlewarePipeTest.php b/test/MiddlewarePipeTest.php index 44dbe0c..518f60e 100644 --- a/test/MiddlewarePipeTest.php +++ b/test/MiddlewarePipeTest.php @@ -177,7 +177,9 @@ public function testHandleProcessesEnqueuedMiddleware(): void $this->request ) ->willReturnCallback( - static fn(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface => $handler->handle($request) + static fn( + ServerRequestInterface $request, + RequestHandlerInterface $handler): ResponseInterface => $handler->handle($request) ); $middleware2 = $this->createMock(MiddlewareInterface::class); $middleware2