diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 28487a4..04d63f1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -137,8 +137,8 @@ function ($args) { - function ($container) use ($originalContainer) { - function ($container) use ($originalContainer, $routeResult) { + static function ($container) use ($originalContainer) { + static function ($container) use ($originalContainer, $routeResult) { diff --git a/src/BodyParams/BodyParamsMiddleware.php b/src/BodyParams/BodyParamsMiddleware.php index 5ab809c..b15f51f 100644 --- a/src/BodyParams/BodyParamsMiddleware.php +++ b/src/BodyParams/BodyParamsMiddleware.php @@ -14,17 +14,15 @@ class BodyParamsMiddleware implements MiddlewareInterface { /** @var StrategyInterface[] */ - private $strategies = []; + private array $strategies = []; /** * List of request methods that do not have any defined body semantics, and thus * will not have the body parsed. * * @see https://tools.ietf.org/html/rfc7231 - * - * @var array */ - private $nonBodyRequests = [ + private array $nonBodyRequests = [ 'GET', 'HEAD', 'OPTIONS', diff --git a/src/ServerUrlHelper.php b/src/ServerUrlHelper.php index 237aea0..36fce3a 100644 --- a/src/ServerUrlHelper.php +++ b/src/ServerUrlHelper.php @@ -6,6 +6,7 @@ use Psr\Http\Message\UriInterface; +use function assert; use function preg_match; use function rtrim; @@ -14,8 +15,7 @@ */ class ServerUrlHelper { - /** @var UriInterface */ - private $uri; + private ?UriInterface $uri = null; /** * Return a path relative to the current request URI. @@ -33,7 +33,7 @@ class ServerUrlHelper */ public function __invoke(?string $path = null): string { - $path = $path ?? ''; + $path ??= ''; if ($this->uri instanceof UriInterface) { return $this->createUrlFromUri($path); @@ -76,6 +76,8 @@ private function createUrlFromUri(string $specification): string $query = $matches['query'] ?? ''; $fragment = $matches['fragment'] ?? ''; + assert($this->uri instanceof UriInterface); + $uri = $this->uri ->withQuery('') ->withFragment(''); diff --git a/src/ServerUrlMiddleware.php b/src/ServerUrlMiddleware.php index 0af6dbe..d3193ac 100644 --- a/src/ServerUrlMiddleware.php +++ b/src/ServerUrlMiddleware.php @@ -4,6 +4,7 @@ namespace Mezzio\Helper; +use Mezzio\Helper\ServerUrlHelper; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -11,8 +12,7 @@ class ServerUrlMiddleware implements MiddlewareInterface { - /** @var ServerUrlHelper */ - private $helper; + private ServerUrlHelper $helper; public function __construct(ServerUrlHelper $helper) { diff --git a/src/Template/TemplateVariableContainer.php b/src/Template/TemplateVariableContainer.php index 40ca75e..5c05ea4 100644 --- a/src/Template/TemplateVariableContainer.php +++ b/src/Template/TemplateVariableContainer.php @@ -72,7 +72,7 @@ class TemplateVariableContainer implements Countable { /** @var array */ - private $variables = []; + private array $variables = []; public function count(): int { diff --git a/src/UrlHelper.php b/src/UrlHelper.php index 420b82f..49626ce 100644 --- a/src/UrlHelper.php +++ b/src/UrlHelper.php @@ -25,17 +25,13 @@ class UrlHelper */ public const FRAGMENT_IDENTIFIER_REGEX = '/^([!$&\'()*+,;=._~:@\/?-]|%[0-9a-fA-F]{2}|[a-zA-Z0-9])+$/'; - /** @var string */ - private $basePath = '/'; + private string $basePath = '/'; - /** @var RouteResult */ - private $result; + private ?RouteResult $result = null; - /** @var ServerRequestInterface */ - private $request; + private ?ServerRequestInterface $request = null; - /** @var RouterInterface */ - private $router; + private RouterInterface $router; public function __construct(RouterInterface $router) { diff --git a/src/UrlHelperFactory.php b/src/UrlHelperFactory.php index da83011..5596514 100644 --- a/src/UrlHelperFactory.php +++ b/src/UrlHelperFactory.php @@ -12,10 +12,9 @@ class UrlHelperFactory { /** @var string Base path for the URL helper */ - private $basePath; + private string $basePath; - /** @var string $routerServiceName */ - private $routerServiceName; + private string $routerServiceName; /** * Allow serialization diff --git a/src/UrlHelperMiddleware.php b/src/UrlHelperMiddleware.php index 71bc8c9..0e67b59 100644 --- a/src/UrlHelperMiddleware.php +++ b/src/UrlHelperMiddleware.php @@ -4,6 +4,7 @@ namespace Mezzio\Helper; +use Mezzio\Helper\UrlHelper; use Mezzio\Router\RouteResult; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -15,8 +16,7 @@ */ class UrlHelperMiddleware implements MiddlewareInterface { - /** @var UrlHelper */ - private $helper; + private UrlHelper $helper; public function __construct(UrlHelper $helper) { diff --git a/src/UrlHelperMiddlewareFactory.php b/src/UrlHelperMiddlewareFactory.php index 560a03f..fde957a 100644 --- a/src/UrlHelperMiddlewareFactory.php +++ b/src/UrlHelperMiddlewareFactory.php @@ -10,8 +10,7 @@ class UrlHelperMiddlewareFactory { - /** @var string */ - private $urlHelperServiceName; + private string $urlHelperServiceName; /** * Allow serialization diff --git a/test/BodyParams/BodyParamsMiddlewareTest.php b/test/BodyParams/BodyParamsMiddlewareTest.php index 9d5ab86..0d910cd 100644 --- a/test/BodyParams/BodyParamsMiddlewareTest.php +++ b/test/BodyParams/BodyParamsMiddlewareTest.php @@ -24,11 +24,9 @@ class BodyParamsMiddlewareTest extends TestCase { use AttributeAssertionsTrait; - /** @var Stream */ - private $body; + private Stream $body; - /** @var BodyParamsMiddleware */ - private $bodyParams; + private BodyParamsMiddleware $bodyParams; public function setUp(): void { @@ -80,7 +78,7 @@ public function testParsesRawBodyAndPreservesRawBodyInRequestAttribute(string $c $this->bodyParams->process( $serverRequest, - $this->mockHandler(function (ServerRequestInterface $request) use (&$serverRequest) { + $this->mockHandler(static function (ServerRequestInterface $request) use (&$serverRequest): Response { $serverRequest = $request; return new Response(); }) @@ -117,7 +115,7 @@ public function testRequestIsUnchangedWhenBodyParamsMiddlewareIsNotApplicable( $this->bodyParams->process( $originalRequest, - $this->mockHandler(function (ServerRequestInterface $request) use (&$finalRequest) { + $this->mockHandler(static function (ServerRequestInterface $request) use (&$finalRequest): Response { $finalRequest = $request; return new Response(); }) @@ -158,10 +156,12 @@ public function testCustomStrategiesCanMatchRequests(): void $response = $middleware->process( $serverRequest, - $this->mockHandler(function (ServerRequestInterface $request) use ($expectedReturn, $expectedResponse) { - $this->assertSame($expectedReturn, $request); - return $expectedResponse; - }) + $this->mockHandler( + function (ServerRequestInterface $request) use ($expectedReturn, $expectedResponse): Response { + $this->assertSame($expectedReturn, $request); + return $expectedResponse; + } + ) ); $this->assertSame($expectedResponse, $response); @@ -176,10 +176,12 @@ public function testCallsNextWithOriginalRequestWhenNoStrategiesMatch(): void $response = $middleware->process( $serverRequest, - $this->mockHandler(function (ServerRequestInterface $request) use ($serverRequest, $expectedResponse) { - $this->assertSame($serverRequest, $request); - return $expectedResponse; - }) + $this->mockHandler( + function (ServerRequestInterface $request) use ($serverRequest, $expectedResponse): Response { + $this->assertSame($serverRequest, $request); + return $expectedResponse; + } + ) ); $this->assertSame($expectedResponse, $response); @@ -242,10 +244,8 @@ public function testParsesJsonBodyWhenExpected(string $method): void ); $handlerTriggered = false; - - $result = $this->bodyParams->process( - $serverRequest, - $this->mockHandler(function (ServerRequestInterface $request) use ($serverRequest, &$handlerTriggered) { + $callback = + function (ServerRequestInterface $request) use ($serverRequest, &$handlerTriggered): Response { $handlerTriggered = true; $this->assertNotSame( @@ -267,7 +267,10 @@ public function testParsesJsonBodyWhenExpected(string $method): void ); return new Response(); - }) + }; + $result = $this->bodyParams->process( + $serverRequest, + $this->mockHandler($callback) ); $this->assertInstanceOf(Response::class, $result); diff --git a/test/BodyParams/FormUrlEncodedStrategyTest.php b/test/BodyParams/FormUrlEncodedStrategyTest.php index fc6a898..4fff9c6 100644 --- a/test/BodyParams/FormUrlEncodedStrategyTest.php +++ b/test/BodyParams/FormUrlEncodedStrategyTest.php @@ -12,8 +12,7 @@ class FormUrlEncodedStrategyTest extends TestCase { - /** @var FormUrlEncodedStrategy */ - private $strategy; + private FormUrlEncodedStrategy $strategy; public function setUp(): void { diff --git a/test/BodyParams/JsonStrategyTest.php b/test/BodyParams/JsonStrategyTest.php index 259e33e..4dc38de 100644 --- a/test/BodyParams/JsonStrategyTest.php +++ b/test/BodyParams/JsonStrategyTest.php @@ -17,8 +17,7 @@ class JsonStrategyTest extends TestCase { - /** @var JsonStrategy */ - private $strategy; + private JsonStrategy $strategy; public function setUp(): void { diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index a8abbab..474046a 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -13,8 +13,7 @@ class ConfigProviderTest extends TestCase { - /** @var ConfigProvider */ - private $provider; + private ConfigProvider $provider; protected function setUp(): void { diff --git a/test/ContentLengthMiddlewareTest.php b/test/ContentLengthMiddlewareTest.php index 66db6a3..990d540 100644 --- a/test/ContentLengthMiddlewareTest.php +++ b/test/ContentLengthMiddlewareTest.php @@ -17,10 +17,8 @@ class ContentLengthMiddlewareTest extends TestCase { - /** @var ContentLengthMiddleware */ - private $middleware; - /** @var ServerRequest */ - private $serverRequest; + private ContentLengthMiddleware $middleware; + private ServerRequest $serverRequest; protected function setUp(): void { diff --git a/test/ServerUrlMiddlewareTest.php b/test/ServerUrlMiddlewareTest.php index fa7d7f2..3b0c62b 100644 --- a/test/ServerUrlMiddlewareTest.php +++ b/test/ServerUrlMiddlewareTest.php @@ -32,11 +32,12 @@ public function testMiddlewareInjectsHelperWithUri(): void $invoked = false; $handler = $this->prophesize(RequestHandlerInterface::class); - $handler->handle(Argument::type(RequestInterface::class))->will(function ($req) use (&$invoked) { - $invoked = true; - - return new Response(); - }); + $handler->handle(Argument::type(RequestInterface::class))->will( + function ($req) use (&$invoked): Response { + $invoked = true; + return new Response(); + } + ); $test = $middleware->process($request->reveal(), $handler->reveal()); //$this->assertSame($response->reveal(), $test, 'Unexpected return value from middleware'); diff --git a/test/Template/RouteTemplateVariableMiddlewareTest.php b/test/Template/RouteTemplateVariableMiddlewareTest.php index 984db63..7018b71 100644 --- a/test/Template/RouteTemplateVariableMiddlewareTest.php +++ b/test/Template/RouteTemplateVariableMiddlewareTest.php @@ -31,9 +31,7 @@ public function testMiddlewareInjectsVariableContainerWithNullRouteIfNoVariableC { $this->request ->getAttribute(TemplateVariableContainer::class, Argument::type(TemplateVariableContainer::class)) - ->will(function ($args) { - return $args[1]; - }) + ->will(fn($args) => $args[1]) ->shouldBeCalledTimes(1); $this->request @@ -80,7 +78,7 @@ public function testMiddlewareWillInjectNullValueForRouteIfNoRouteResultInReques $this->request ->withAttribute( TemplateVariableContainer::class, - Argument::that(function ($container) use ($originalContainer) { + Argument::that(static function ($container) use ($originalContainer) { TestCase::assertNotSame($container, $originalContainer); TestCase::assertTrue($container->has('route')); TestCase::assertNull($container->get('route')); @@ -118,7 +116,7 @@ public function testMiddlewareWillInjectRoutePulledFromRequestRouteResult(): voi $this->request ->withAttribute( TemplateVariableContainer::class, - Argument::that(function ($container) use ($originalContainer, $routeResult) { + Argument::that(static function ($container) use ($originalContainer, $routeResult) { TestCase::assertNotSame($container, $originalContainer); TestCase::assertTrue($container->has('route')); TestCase::assertSame($container->get('route'), $routeResult->reveal()); diff --git a/test/UrlHelperFactoryTest.php b/test/UrlHelperFactoryTest.php index 3af7d8e..8f75b1e 100644 --- a/test/UrlHelperFactoryTest.php +++ b/test/UrlHelperFactoryTest.php @@ -22,8 +22,7 @@ class UrlHelperFactoryTest extends TestCase /** @var ContainerInterface&MockObject */ private $container; - /** @var UrlHelperFactory */ - private $factory; + private UrlHelperFactory $factory; public function setUp(): void {