From 3b0b42a1f90d649534e58011a3663379b56780c7 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 13 Oct 2023 19:07:54 +1100 Subject: [PATCH] wip --- src/Concerns/Identification.php | 4 +- src/Concerns/Implementation.php | 2 +- src/Concerns/Relationships.php | 2 +- .../ResourceIdentificationException.php | 13 +++---- .../UnknownRelationshipException.php | 7 ++-- src/JsonApiResource.php | 38 ++----------------- src/JsonApiResourceCollection.php | 20 ++-------- src/Link.php | 6 ++- src/RelationshipObject.php | 7 ++-- src/Support/Includes.php | 1 - 10 files changed, 27 insertions(+), 73 deletions(-) diff --git a/src/Concerns/Identification.php b/src/Concerns/Identification.php index 0b578a1..76ba061 100644 --- a/src/Concerns/Identification.php +++ b/src/Concerns/Identification.php @@ -125,7 +125,7 @@ private function resolveType(Request $request) */ private static function idResolver() { - return self::$idResolver ?? function (mixed $resource, Request $request): string { + return self::$idResolver ??= function (mixed $resource, Request $request): string { if (! $resource instanceof Model) { throw ResourceIdentificationException::attemptingToDetermineIdFor($resource); } @@ -144,7 +144,7 @@ private static function idResolver() */ private static function typeResolver() { - return self::$typeResolver ?? function (mixed $resource, Request $request): string { + return self::$typeResolver ??= function (mixed $resource, Request $request): string { if (! $resource instanceof Model) { throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource); } diff --git a/src/Concerns/Implementation.php b/src/Concerns/Implementation.php index 0a557bd..6312473 100644 --- a/src/Concerns/Implementation.php +++ b/src/Concerns/Implementation.php @@ -44,6 +44,6 @@ public static function resolveServerImplementationNormally() */ public static function serverImplementationResolver() { - return self::$serverImplementationResolver ?? fn (Request $request): ServerImplementation => new ServerImplementation('1.0'); + return self::$serverImplementationResolver ??= fn (Request $request): ServerImplementation => new ServerImplementation('1.0'); } } diff --git a/src/Concerns/Relationships.php b/src/Concerns/Relationships.php index 4d5a9af..3c1b184 100644 --- a/src/Concerns/Relationships.php +++ b/src/Concerns/Relationships.php @@ -186,7 +186,7 @@ private function shouldBePresentInIncludes() */ private static function guessRelationshipResource(string $relationship, JsonApiResource $resource) { - return (self::$relationshipResourceGuesser ?? function (string $relationship, JsonApiResource $resource): string { + return (self::$relationshipResourceGuesser ??= function (string $relationship, JsonApiResource $resource): string { $relationship = Str::of($relationship); foreach ([ diff --git a/src/Exceptions/ResourceIdentificationException.php b/src/Exceptions/ResourceIdentificationException.php index 55af6ff..bd6bb2e 100644 --- a/src/Exceptions/ResourceIdentificationException.php +++ b/src/Exceptions/ResourceIdentificationException.php @@ -9,31 +9,28 @@ use function gettype; use function is_object; +/** + * @internal + */ final class ResourceIdentificationException extends RuntimeException { /** - * @internal - * * @return self */ public static function attemptingToDetermineIdFor(mixed $resource) { - return new static('Unable to resolve resource object id for ['.static::determineType($resource).'].'); + return new self('Unable to resolve resource object id for ['.static::determineType($resource).'].'); } /** - * @internal - * * @return self */ public static function attemptingToDetermineTypeFor(mixed $resource) { - return new static('Unable to resolve resource object type for ['.static::determineType($resource).'].'); + return new self('Unable to resolve resource object type for ['.static::determineType($resource).'].'); } /** - * @internal - * * @return string */ private static function determineType(mixed $resource) diff --git a/src/Exceptions/UnknownRelationshipException.php b/src/Exceptions/UnknownRelationshipException.php index 94978d5..b9376a0 100644 --- a/src/Exceptions/UnknownRelationshipException.php +++ b/src/Exceptions/UnknownRelationshipException.php @@ -11,11 +11,12 @@ use function gettype; use function is_object; +/** + * @internal + */ final class UnknownRelationshipException extends Exception { /** - * @internal - * * @return self */ public static function from(mixed $resource) @@ -24,8 +25,6 @@ public static function from(mixed $resource) } /** - * @internal - * * @return string */ private static function determineType(mixed $resource) diff --git a/src/JsonApiResource.php b/src/JsonApiResource.php index c3ce83a..19030df 100644 --- a/src/JsonApiResource.php +++ b/src/JsonApiResource.php @@ -10,7 +10,6 @@ use Illuminate\Http\Resources\PotentiallyMissing; use Illuminate\Support\Collection; use stdClass; - use function property_exists; abstract class JsonApiResource extends JsonResource @@ -25,8 +24,6 @@ abstract class JsonApiResource extends JsonResource use Concerns\Relationships; /** - * @api - * * @return array */ public function toAttributes(Request $request) @@ -37,8 +34,6 @@ public function toAttributes(Request $request) } /** - * @api - * * @return array */ public function toRelationships(Request $request) @@ -49,8 +44,6 @@ public function toRelationships(Request $request) } /** - * @api - * * @return array */ public function toLinks(Request $request) @@ -61,8 +54,6 @@ public function toLinks(Request $request) } /** - * @api - * * @return array */ public function toMeta(Request $request) @@ -73,8 +64,6 @@ public function toMeta(Request $request) } /** - * @api - * * @return string */ public function toId(Request $request) @@ -83,8 +72,6 @@ public function toId(Request $request) } /** - * @api - * * @return string */ public function toType(Request $request) @@ -93,8 +80,6 @@ public function toType(Request $request) } /** - * @api - * * @return RelationshipObject */ public function toResourceLink(Request $request) @@ -105,8 +90,6 @@ public function toResourceLink(Request $request) } /** - * @api - * * @return ResourceIdentifier */ public function toResourceIdentifier(Request $request) @@ -115,12 +98,9 @@ public function toResourceIdentifier(Request $request) } /** - * @api - * - * @param Request $request * @return array{id: string, type: string, attributes?: stdClass, relationships?: stdClass, meta?: stdClass, links?: stdClass} */ - public function toArray($request) + public function toArray(Request $request) { return [ 'id' => $this->resolveId($request), @@ -135,12 +115,9 @@ public function toArray($request) } /** - * @api - * - * @param Request $request - * @return array{included: Collection, jsonapi: JsonApiServerImplementation} + * @return array{included?: Collection, jsonapi: JsonApiServerImplementation} */ - public function with($request) + public function with(Request $request) { return [ ...($included = $this->included($request) @@ -152,12 +129,9 @@ public function with($request) } /** - * @api - * - * @param mixed $resource * @return JsonApiResourceCollection */ - public static function collection($resource) + public static function collection(mixed $resource) { return tap(static::newCollection($resource), function (JsonApiResourceCollection $collection): void { if (property_exists(static::class, 'preserveKeys')) { @@ -168,8 +142,6 @@ public static function collection($resource) } /** - * @api - * * @return JsonApiResourceCollection */ public static function newCollection(mixed $resource) @@ -178,8 +150,6 @@ public static function newCollection(mixed $resource) } /** - * @api - * * @param Request $request * @return JsonResponse */ diff --git a/src/JsonApiResourceCollection.php b/src/JsonApiResourceCollection.php index 39ddb98..eac3cdc 100644 --- a/src/JsonApiResourceCollection.php +++ b/src/JsonApiResourceCollection.php @@ -13,8 +13,6 @@ class JsonApiResourceCollection extends AnonymousResourceCollection use Concerns\RelationshipLinks; /** - * @api - * * @param (callable(JsonApiResource): JsonApiResource) $callback * @return $this */ @@ -26,8 +24,6 @@ public function map(callable $callback) } /** - * @api - * * @return RelationshipObject */ public function toResourceLink(Request $request) @@ -36,8 +32,6 @@ public function toResourceLink(Request $request) } /** - * @internal - * * @return Collection */ private function resolveResourceIdentifiers(Request $request) @@ -48,12 +42,9 @@ private function resolveResourceIdentifiers(Request $request) } /** - * @api - * - * @param Request $request - * @return array{included: Collection, jsonapi: JsonApiServerImplementation} + * @return array{included?: Collection, jsonapi: JsonApiServerImplementation} */ - public function with($request) + public function with(Request $request) { return [ ...($included = $this->collection @@ -67,8 +58,6 @@ public function with($request) } /** - * @api - * * @param Request $request * @return \Illuminate\Http\JsonResponse */ @@ -78,14 +67,11 @@ public function toResponse($request) } /** - * @api - * - * @param Request $request * @param array $paginated * @param array{links: array} $default * @return array{links: array} */ - public function paginationInformation($request, $paginated, $default) + public function paginationInformation(Request $request, array $paginated, array $default) { if (isset($default['links'])) { $default['links'] = array_filter($default['links'], fn (?string $link): bool => $link !== null); diff --git a/src/Link.php b/src/Link.php index 47a3009..091bb8e 100644 --- a/src/Link.php +++ b/src/Link.php @@ -20,16 +20,18 @@ final class Link implements JsonSerializable /** * @param array $meta + * @return self */ - public static function self(string $href, array $meta = []): self + public static function self(string $href, array $meta = []) { return new self('self', $href, $meta); } /** * @param array $meta + * @return self */ - public static function related(string $href, array $meta = []): self + public static function related(string $href, array $meta = []) { return new self('related', $href, $meta); } diff --git a/src/RelationshipObject.php b/src/RelationshipObject.php index d2ebcbd..a29b033 100644 --- a/src/RelationshipObject.php +++ b/src/RelationshipObject.php @@ -20,8 +20,9 @@ final class RelationshipObject implements JsonSerializable /** * @param array $links * @param array $meta + * @return self */ - public static function toOne(ResourceIdentifier|null $data, array $links = [], array $meta = []): self + public static function toOne(ResourceIdentifier|null $data, array $links = [], array $meta = []) { return new self($data, $links, $meta); } @@ -30,8 +31,9 @@ public static function toOne(ResourceIdentifier|null $data, array $links = [], a * @param array $data * @param array $links * @param array $meta + * @return self */ - public static function toMany(array $data, array $links = [], array $meta = []): self + public static function toMany(array $data, array $links = [], array $meta = []) { return new self($data, $links, $meta); } @@ -50,7 +52,6 @@ private function __construct(ResourceIdentifier|array|null $data, array $links = $this->meta = $meta; } - /** * @return array{data: ResourceIdentifier|null|array, meta?: stdClass, links?: stdClass} */ diff --git a/src/Support/Includes.php b/src/Support/Includes.php index 08dc63d..0ffdc3c 100644 --- a/src/Support/Includes.php +++ b/src/Support/Includes.php @@ -9,7 +9,6 @@ use Illuminate\Support\Str; use Symfony\Component\HttpKernel\Exception\HttpException; use WeakMap; - use function explode; use function is_array;