From ea76d55d27b89cd84955bc23f89c9e87a4b184fa Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 23 Oct 2024 08:12:26 +0900 Subject: [PATCH 1/2] Remove redundant PHP version check for enums This update simplifies the code by removing the PHP version check before calling the isEnum method. Since the project now requires a PHP version that supports enums, the conditional check is unnecessary. This change enhances readability and maintainability of the code. --- src/ClassParam.php | 4 +--- src/HttpRequestCurl.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ClassParam.php b/src/ClassParam.php index fbf55942..0da2fdbb 100644 --- a/src/ClassParam.php +++ b/src/ClassParam.php @@ -24,8 +24,6 @@ use function preg_replace; use function strtolower; -use const PHP_VERSION_ID; - final class ClassParam implements ParamInterface { private readonly string $type; @@ -64,7 +62,7 @@ public function __invoke(string $varName, array $query, InjectorInterface $injec assert(class_exists($this->type)); $refClass = (new ReflectionClass($this->type)); - if (PHP_VERSION_ID >= 80100 && $refClass->isEnum()) { + if ($refClass->isEnum()) { return $this->enum($this->type, $props, $varName); } diff --git a/src/HttpRequestCurl.php b/src/HttpRequestCurl.php index f4eabda2..90dbb9eb 100644 --- a/src/HttpRequestCurl.php +++ b/src/HttpRequestCurl.php @@ -44,7 +44,7 @@ public function __construct( ) { } - /** @inheritdoc */ + /** @inheritDoc */ public function request(string $method, string $uri, array $query): array { $body = http_build_query($query); From 48b846cf51f844ab15afb7ac985dcfb239e73e21 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 23 Oct 2024 08:33:54 +0900 Subject: [PATCH 2/2] Refactor body preparation into a separate method Extract body preparation logic from main code block into a new `prepareBody` method. This enhances code readability and maintainability by isolating the responsibility and making the `EmbedInterceptor` class easier to extend and test. --- src/EmbedInterceptor.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/EmbedInterceptor.php b/src/EmbedInterceptor.php index 138c5337..83587a21 100644 --- a/src/EmbedInterceptor.php +++ b/src/EmbedInterceptor.php @@ -66,13 +66,7 @@ private function embedResource(array $embeds, ResourceObject $ro, array $query): $uri = uri_template($templateUri, $query); /** @var Request $request */ // phpcs:ignore SlevomatCodingStandard.PHP.RequireExplicitAssertion.RequiredExplicitAssertion $request = $this->resource->get->uri($uri); - if ($ro->body === null) { - $ro->body = []; - } - - if (! is_array($ro->body)) { - throw new LinkException($embed->rel); // @codeCoverageIgnore - } + $this->prepareBody($ro, $embed); if ($embed->rel === self::SELF_LINK) { $this->linkSelf($request, $ro); @@ -80,6 +74,8 @@ private function embedResource(array $embeds, ResourceObject $ro, array $query): continue; } + assert(is_array($ro->body)); + $ro->body[$embed->rel] = clone $request; } catch (BadRequestException $e) { // wrap ResourceNotFound or Uri exception @@ -97,6 +93,17 @@ private function getFullUri(string $uri, ResourceObject $ro): string return $uri; } + public function prepareBody(ResourceObject $ro, Embed $embed): void + { + if ($ro->body === null) { + $ro->body = []; + } + + if (! is_array($ro->body)) { + throw new LinkException($embed->rel); // @codeCoverageIgnore + } + } + /** @return array */ private function getArgsByInvocation(MethodInvocation $invocation): array {