From 963b7e541d58108cc2924ac2d517226a8e320ea6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 12 Oct 2022 18:26:31 +0700 Subject: [PATCH] Apply PHP 8.0 Syntax and constructor promotion Signed-off-by: Abdul Malik Ikhsan --- src/ServerUrlMiddleware.php | 5 +---- src/Template/TemplateVariableContainer.php | 3 +-- src/UrlHelper.php | 6 +----- src/UrlHelperFactory.php | 15 ++++++--------- src/UrlHelperMiddleware.php | 5 +---- src/UrlHelperMiddlewareFactory.php | 5 +---- test/AttributeAssertionsTrait.php | 9 +++------ test/ContentLengthMiddlewareTest.php | 5 +---- test/UrlHelperTest.php | 3 +-- 9 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/ServerUrlMiddleware.php b/src/ServerUrlMiddleware.php index d3193ac..bf85867 100644 --- a/src/ServerUrlMiddleware.php +++ b/src/ServerUrlMiddleware.php @@ -12,11 +12,8 @@ class ServerUrlMiddleware implements MiddlewareInterface { - private ServerUrlHelper $helper; - - public function __construct(ServerUrlHelper $helper) + public function __construct(private ServerUrlHelper $helper) { - $this->helper = $helper; } /** diff --git a/src/Template/TemplateVariableContainer.php b/src/Template/TemplateVariableContainer.php index 5c05ea4..363f385 100644 --- a/src/Template/TemplateVariableContainer.php +++ b/src/Template/TemplateVariableContainer.php @@ -94,10 +94,9 @@ public function has(string $key): bool } /** - * @param mixed $value * @return self Returns a new instance that contains the given key/value pair */ - public function with(string $key, $value): self + public function with(string $key, mixed $value): self { $new = clone $this; $new->variables[$key] = $value; diff --git a/src/UrlHelper.php b/src/UrlHelper.php index 49626ce..5e307ca 100644 --- a/src/UrlHelper.php +++ b/src/UrlHelper.php @@ -31,11 +31,8 @@ class UrlHelper private ?ServerRequestInterface $request = null; - private RouterInterface $router; - - public function __construct(RouterInterface $router) + public function __construct(private RouterInterface $router) { - $this->router = $router; } /** @@ -226,7 +223,6 @@ private function mergeParams(string $route, RouteResult $result, array $params): * @param string $route Route name * @param RouteResult $result RouteResult instance * @param array $params Params to be merged with request params - * @return array */ private function mergeQueryParams(string $route, RouteResult $result, array $params): array { diff --git a/src/UrlHelperFactory.php b/src/UrlHelperFactory.php index 5596514..8d9ff55 100644 --- a/src/UrlHelperFactory.php +++ b/src/UrlHelperFactory.php @@ -11,11 +11,6 @@ class UrlHelperFactory { - /** @var string Base path for the URL helper */ - private string $basePath; - - private string $routerServiceName; - /** * Allow serialization */ @@ -31,11 +26,13 @@ public static function __set_state(array $data): self * Allows varying behavior per-instance. * * Defaults to '/' for the base path, and the FQCN of the RouterInterface. + * + * @param string $basePath Base path for the URL helper */ - public function __construct(string $basePath = '/', string $routerServiceName = RouterInterface::class) - { - $this->basePath = $basePath; - $this->routerServiceName = $routerServiceName; + public function __construct( + private string $basePath = '/', + private string $routerServiceName = RouterInterface::class + ) { } /** diff --git a/src/UrlHelperMiddleware.php b/src/UrlHelperMiddleware.php index 0e67b59..9a1ac4c 100644 --- a/src/UrlHelperMiddleware.php +++ b/src/UrlHelperMiddleware.php @@ -16,11 +16,8 @@ */ class UrlHelperMiddleware implements MiddlewareInterface { - private UrlHelper $helper; - - public function __construct(UrlHelper $helper) + public function __construct(private UrlHelper $helper) { - $this->helper = $helper; } /** diff --git a/src/UrlHelperMiddlewareFactory.php b/src/UrlHelperMiddlewareFactory.php index fde957a..2e96cb2 100644 --- a/src/UrlHelperMiddlewareFactory.php +++ b/src/UrlHelperMiddlewareFactory.php @@ -10,8 +10,6 @@ class UrlHelperMiddlewareFactory { - private string $urlHelperServiceName; - /** * Allow serialization */ @@ -25,9 +23,8 @@ public static function __set_state(array $data): self /** * Allow varying behavior based on URL helper service name. */ - public function __construct(string $urlHelperServiceName = UrlHelper::class) + public function __construct(private string $urlHelperServiceName = UrlHelper::class) { - $this->urlHelperServiceName = $urlHelperServiceName; } /** diff --git a/test/AttributeAssertionsTrait.php b/test/AttributeAssertionsTrait.php index ee559e3..613ad7e 100644 --- a/test/AttributeAssertionsTrait.php +++ b/test/AttributeAssertionsTrait.php @@ -8,8 +8,7 @@ trait AttributeAssertionsTrait { - /** @param mixed $expected */ - public static function assertAttributeSame($expected, string $attribute, object $object): void + public static function assertAttributeSame(mixed $expected, string $attribute, object $object): void { $r = new ReflectionProperty($object, $attribute); $r->setAccessible(true); @@ -17,8 +16,7 @@ public static function assertAttributeSame($expected, string $attribute, object self::assertSame($expected, $r->getValue($object)); } - /** @param mixed $expected */ - public static function assertAttributeEquals($expected, string $attribute, object $object): void + public static function assertAttributeEquals(mixed $expected, string $attribute, object $object): void { $r = new ReflectionProperty($object, $attribute); $r->setAccessible(true); @@ -26,8 +24,7 @@ public static function assertAttributeEquals($expected, string $attribute, objec self::assertEquals($expected, $r->getValue($object)); } - /** @param mixed $expected */ - public static function assertAttributeContains($expected, string $attribute, object $object): void + public static function assertAttributeContains(mixed $expected, string $attribute, object $object): void { $r = new ReflectionProperty($object, $attribute); $r->setAccessible(true); diff --git a/test/ContentLengthMiddlewareTest.php b/test/ContentLengthMiddlewareTest.php index 0d5e7d8..8e9a6d3 100644 --- a/test/ContentLengthMiddlewareTest.php +++ b/test/ContentLengthMiddlewareTest.php @@ -33,11 +33,8 @@ protected function setUp(): void private function handlerWillReturnResponse(ResponseInterface $response): RequestHandlerInterface { return new class ($response) implements RequestHandlerInterface { - public ResponseInterface $response; - - public function __construct(ResponseInterface $response) + public function __construct(public ResponseInterface $response) { - $this->response = $response; } public function handle(ServerRequestInterface $request): ResponseInterface diff --git a/test/UrlHelperTest.php b/test/UrlHelperTest.php index 18fe468..dbdbfd7 100644 --- a/test/UrlHelperTest.php +++ b/test/UrlHelperTest.php @@ -419,9 +419,8 @@ public function invalidBasePathProvider(): array /** * @dataProvider invalidBasePathProvider - * @param mixed $basePath */ - public function testThrowsExceptionWhenSettingInvalidBasePaths($basePath): void + public function testThrowsExceptionWhenSettingInvalidBasePaths(mixed $basePath): void { $this->expectException(TypeError::class);