From a442aad3be528c7c719e989c8b604d4ba930469c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 10 May 2024 13:13:50 +0000 Subject: [PATCH] Apply fixes from StyleCI --- .../sticky/src/Api/DiscussionResourceFields.php | 1 - framework/core/src/Api/Context.php | 7 +++++-- .../Api/Endpoint/Concerns/HasEagerLoading.php | 13 +++++++++---- .../core/src/Api/Endpoint/Concerns/HasHooks.php | 7 +++++++ .../src/Api/Endpoint/Concerns/IncludesData.php | 7 +++++++ .../Endpoint/Concerns/SavesAndValidatesData.php | 17 ++++++++++++----- framework/core/src/Api/Endpoint/Create.php | 5 +++-- framework/core/src/Api/Endpoint/Delete.php | 3 ++- framework/core/src/Api/Endpoint/Endpoint.php | 9 +++++---- framework/core/src/Api/Endpoint/Index.php | 13 +++++++------ framework/core/src/Api/Endpoint/Update.php | 2 +- framework/core/src/Api/JsonApi.php | 2 -- .../src/Api/Resource/Contracts/Attachable.php | 7 +++++++ .../src/Api/Resource/Contracts/Countable.php | 7 +++++++ .../src/Api/Resource/Contracts/Creatable.php | 7 +++++++ .../src/Api/Resource/Contracts/Deletable.php | 7 +++++++ .../src/Api/Resource/Contracts/Findable.php | 7 +++++++ .../src/Api/Resource/Contracts/Listable.php | 7 +++++++ .../src/Api/Resource/Contracts/Paginatable.php | 7 +++++++ .../src/Api/Resource/Contracts/Updatable.php | 7 +++++++ .../core/src/Api/Resource/EloquentBuffer.php | 16 ++++++++++------ .../src/Api/Schema/Concerns/FlarumField.php | 11 +++++++++-- .../Api/Schema/Concerns/FlarumRelationship.php | 7 +++++++ .../Schema/Concerns/GetsRelationAggregates.php | 7 +++++++ .../Api/Schema/Concerns/HasValidationRules.php | 12 +++++++++--- .../Api/Schema/Contracts/RelationAggregator.php | 7 +++++++ .../core/src/Api/Schema/Relationship/ToOne.php | 2 +- framework/core/src/Api/Sort/SortWithCount.php | 7 +++++++ 28 files changed, 171 insertions(+), 40 deletions(-) diff --git a/extensions/sticky/src/Api/DiscussionResourceFields.php b/extensions/sticky/src/Api/DiscussionResourceFields.php index 22730b514..437155a8d 100644 --- a/extensions/sticky/src/Api/DiscussionResourceFields.php +++ b/extensions/sticky/src/Api/DiscussionResourceFields.php @@ -10,7 +10,6 @@ namespace Flarum\Sticky\Api; use Flarum\Api\Context; -use Flarum\Api\Endpoint\Update; use Flarum\Api\Schema; use Flarum\Discussion\Discussion; use Flarum\Sticky\Event\DiscussionWasStickied; diff --git a/framework/core/src/Api/Context.php b/framework/core/src/Api/Context.php index 7eb4556e8..7bbfc78d7 100644 --- a/framework/core/src/Api/Context.php +++ b/framework/core/src/Api/Context.php @@ -136,6 +136,7 @@ class Context extends BaseContext { $new = parent::withRequest($request); $new->requestIncludes = null; + return $new; } @@ -143,6 +144,7 @@ class Context extends BaseContext { $new = clone $this; $new->modelId = $id; + return $new; } @@ -150,15 +152,16 @@ class Context extends BaseContext { $new = clone $this; $new->requestIncludes = $requestIncludes; + return $new; } public function extractIdFromPath(\Tobyz\JsonApiServer\Context $context): ?string { $currentPath = trim($context->path(), '/'); - $path = trim($context->collection->name() . $this->endpoint->path, '/'); + $path = trim($context->collection->name().$this->endpoint->path, '/'); - if (!str_contains($path, '{id}')) { + if (! str_contains($path, '{id}')) { return null; } diff --git a/framework/core/src/Api/Endpoint/Concerns/HasEagerLoading.php b/framework/core/src/Api/Endpoint/Concerns/HasEagerLoading.php index dffa64f2b..225dc7c3d 100644 --- a/framework/core/src/Api/Endpoint/Concerns/HasEagerLoading.php +++ b/framework/core/src/Api/Endpoint/Concerns/HasEagerLoading.php @@ -1,15 +1,20 @@ [ ...nested ] to ['relation', 'relation.nested'] + * From format of: 'relation' => [ ...nested ] to ['relation', 'relation.nested']. */ private function stringInclude(array $include): array { diff --git a/framework/core/src/Api/Endpoint/Concerns/HasHooks.php b/framework/core/src/Api/Endpoint/Concerns/HasHooks.php index 1af527583..a31cf4d89 100644 --- a/framework/core/src/Api/Endpoint/Concerns/HasHooks.php +++ b/framework/core/src/Api/Endpoint/Concerns/HasHooks.php @@ -1,5 +1,12 @@ body(); - if (!isset($body['data']) || !is_array($body['data'])) { + if (! isset($body['data']) || ! is_array($body['data'])) { throw (new BadRequestException('data must be an object'))->setSource([ 'pointer' => '/data', ]); } - if (!isset($body['data']['type'])) { + if (! isset($body['data']['type'])) { if (isset($context->collection->resources()[0])) { $body['data']['type'] = $context->collection->resources()[0]; } else { @@ -140,19 +147,19 @@ trait SavesAndValidatesData ]); } - if (!in_array($body['data']['type'], $context->collection->resources())) { + if (! in_array($body['data']['type'], $context->collection->resources())) { throw (new ConflictException( 'collection does not support this resource type', ))->setSource(['pointer' => '/data/type']); } - if (array_key_exists('attributes', $body['data']) && !is_array($body['data']['attributes'])) { + if (array_key_exists('attributes', $body['data']) && ! is_array($body['data']['attributes'])) { throw (new BadRequestException('data.attributes must be an object'))->setSource([ 'pointer' => '/data/attributes', ]); } - if (array_key_exists('relationships', $body['data']) && !is_array($body['data']['relationships'])) { + if (array_key_exists('relationships', $body['data']) && ! is_array($body['data']['relationships'])) { throw (new BadRequestException('data.relationships must be an object'))->setSource([ 'pointer' => '/data/relationships', ]); diff --git a/framework/core/src/Api/Endpoint/Create.php b/framework/core/src/Api/Endpoint/Create.php index 6d9ba9175..44ef17b01 100644 --- a/framework/core/src/Api/Endpoint/Create.php +++ b/framework/core/src/Api/Endpoint/Create.php @@ -18,6 +18,7 @@ use Flarum\Database\Eloquent\Collection; use RuntimeException; use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources; use Tobyz\JsonApiServer\Resource\Creatable; + use function Tobyz\JsonApiServer\has_value; use function Tobyz\JsonApiServer\json_api_response; use function Tobyz\JsonApiServer\set_value; @@ -45,7 +46,7 @@ class Create extends Endpoint $collection = $context->collection; - if (!$collection instanceof Creatable) { + if (! $collection instanceof Creatable) { throw new RuntimeException( sprintf('%s must implement %s', get_class($collection), Creatable::class), ); @@ -84,7 +85,7 @@ class Create extends Endpoint final protected function fillDefaultValues(Context $context, array &$data): void { foreach ($context->fields($context->resource) as $field) { - if (!has_value($data, $field) && ($default = $field->default)) { + if (! has_value($data, $field) && ($default = $field->default)) { set_value($data, $field, $default($context->withField($field))); } } diff --git a/framework/core/src/Api/Endpoint/Delete.php b/framework/core/src/Api/Endpoint/Delete.php index b31bb8c10..e2ad756c3 100644 --- a/framework/core/src/Api/Endpoint/Delete.php +++ b/framework/core/src/Api/Endpoint/Delete.php @@ -16,6 +16,7 @@ use Nyholm\Psr7\Response; use RuntimeException; use Tobyz\JsonApiServer\Resource\Deletable; use Tobyz\JsonApiServer\Schema\Concerns\HasMeta; + use function Tobyz\JsonApiServer\json_api_response; class Delete extends Endpoint @@ -39,7 +40,7 @@ class Delete extends Endpoint $resource = $context->resource($context->collection->resource($model, $context)), ); - if (!$resource instanceof Deletable) { + if (! $resource instanceof Deletable) { throw new RuntimeException( sprintf('%s must implement %s', get_class($resource), Deletable::class), ); diff --git a/framework/core/src/Api/Endpoint/Endpoint.php b/framework/core/src/Api/Endpoint/Endpoint.php index 55496998d..ba93e913e 100644 --- a/framework/core/src/Api/Endpoint/Endpoint.php +++ b/framework/core/src/Api/Endpoint/Endpoint.php @@ -21,6 +21,7 @@ use Tobyz\JsonApiServer\Endpoint\Concerns\FindsResources; use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources; use Tobyz\JsonApiServer\Exception\ForbiddenException; use Tobyz\JsonApiServer\Exception\MethodNotAllowedException; + use function Tobyz\JsonApiServer\json_api_response; class Endpoint implements EndpointInterface @@ -81,7 +82,7 @@ class Endpoint implements EndpointInterface public function route(string $method, string $path): static { $this->method = $method; - $this->path = '/' . ltrim(rtrim($path, '/'), '/'); + $this->path = '/'.ltrim(rtrim($path, '/'), '/'); return $this; } @@ -96,7 +97,7 @@ class Endpoint implements EndpointInterface public function process(Context $context): mixed { if (! $this->action) { - throw new RuntimeException("No action defined for endpoint [".static::class."]"); + throw new RuntimeException('No action defined for endpoint ['.static::class.']'); } return ($this->action)($context); @@ -108,7 +109,7 @@ class Endpoint implements EndpointInterface public function handle(\Tobyz\JsonApiServer\Context $context): ?Response { if (! isset($this->method, $this->path)) { - throw new RuntimeException("No route defined for endpoint [".static::class."]"); + throw new RuntimeException('No route defined for endpoint ['.static::class.']'); } if (strtolower($context->method()) !== strtolower($this->method)) { @@ -125,7 +126,7 @@ class Endpoint implements EndpointInterface ); } - if (!$this->isVisible($context)) { + if (! $this->isVisible($context)) { throw new ForbiddenException(); } diff --git a/framework/core/src/Api/Endpoint/Index.php b/framework/core/src/Api/Endpoint/Index.php index a73750535..3820d1ca6 100644 --- a/framework/core/src/Api/Endpoint/Index.php +++ b/framework/core/src/Api/Endpoint/Index.php @@ -29,6 +29,7 @@ use Tobyz\JsonApiServer\Pagination\OffsetPagination; use Tobyz\JsonApiServer\Pagination\Pagination; use Tobyz\JsonApiServer\Schema\Concerns\HasMeta; use Tobyz\JsonApiServer\Serializer; + use function Tobyz\JsonApiServer\apply_filters; use function Tobyz\JsonApiServer\json_api_response; use function Tobyz\JsonApiServer\parse_sort_string; @@ -49,7 +50,7 @@ class Index extends Endpoint { parent::__construct($name); - $this->paginationResolver = fn() => null; + $this->paginationResolver = fn () => null; } public static function make(?string $name = null): static @@ -111,7 +112,7 @@ class Index extends Endpoint $collection = $context->collection; - if (!$collection instanceof Listable) { + if (! $collection instanceof Listable) { throw new RuntimeException( sprintf('%s must implement %s', get_class($collection), Listable::class), ); @@ -144,7 +145,7 @@ class Index extends Endpoint if ( $collection instanceof Countable && - !is_null($total = $collection->count($query, $context)) + ! is_null($total = $collection->count($query, $context)) ) { $meta['page']['total'] = $total; } @@ -199,7 +200,7 @@ class Index extends Endpoint final protected function applySorts($query, Context $context): void { - if (!($sortString = $context->queryParam('sort', $this->defaultSort))) { + if (! ($sortString = $context->queryParam('sort', $this->defaultSort))) { return; } @@ -221,11 +222,11 @@ class Index extends Endpoint final protected function applyFilters($query, Context $context): void { - if (!($filters = $context->queryParam('filter'))) { + if (! ($filters = $context->queryParam('filter'))) { return; } - if (!is_array($filters)) { + if (! is_array($filters)) { throw (new BadRequestException('filter must be an array'))->setSource([ 'parameter' => 'filter', ]); diff --git a/framework/core/src/Api/Endpoint/Update.php b/framework/core/src/Api/Endpoint/Update.php index 18a0cc0fd..b37cf110e 100644 --- a/framework/core/src/Api/Endpoint/Update.php +++ b/framework/core/src/Api/Endpoint/Update.php @@ -42,7 +42,7 @@ class Update extends Endpoint $resource = $context->resource($context->collection->resource($model, $context)), ); - if (!$resource instanceof Updatable) { + if (! $resource instanceof Updatable) { throw new RuntimeException( sprintf('%s must implement %s', get_class($resource), Updatable::class), ); diff --git a/framework/core/src/Api/JsonApi.php b/framework/core/src/Api/JsonApi.php index 82b7d67df..f4c7e1fcc 100644 --- a/framework/core/src/Api/JsonApi.php +++ b/framework/core/src/Api/JsonApi.php @@ -17,13 +17,11 @@ use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\Uri; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Tobyz\JsonApiServer\Endpoint\Endpoint; use Tobyz\JsonApiServer\Exception\BadRequestException; use Tobyz\JsonApiServer\Exception\ResourceNotFoundException; use Tobyz\JsonApiServer\JsonApi as BaseJsonApi; use Tobyz\JsonApiServer\Resource\Collection; use Tobyz\JsonApiServer\Resource\Resource; -use Tobyz\JsonApiServer\Schema\Field\Field; class JsonApi extends BaseJsonApi { diff --git a/framework/core/src/Api/Resource/Contracts/Attachable.php b/framework/core/src/Api/Resource/Contracts/Attachable.php index 85f72257a..e2efb8f40 100644 --- a/framework/core/src/Api/Resource/Contracts/Attachable.php +++ b/framework/core/src/Api/Resource/Contracts/Attachable.php @@ -1,5 +1,12 @@ newModel($context)); - if ($resource instanceof AbstractDatabaseResource && !isset($constrain[$modelClass])) { - $constrain[$modelClass] = function (Builder $query) use ($resource, $context, $relationship, $relation, $aggregate) { + if ($resource instanceof AbstractDatabaseResource && ! isset($constrain[$modelClass])) { + $constrain[$modelClass] = function (Builder $query) use ($resource, $context, $relationship, $aggregate) { if (! $aggregate) { $query ->with($context->endpoint->getEagerLoadsFor($relationship->name, $context)) diff --git a/framework/core/src/Api/Schema/Concerns/FlarumField.php b/framework/core/src/Api/Schema/Concerns/FlarumField.php index af73b95f7..06f9656c6 100644 --- a/framework/core/src/Api/Schema/Concerns/FlarumField.php +++ b/framework/core/src/Api/Schema/Concerns/FlarumField.php @@ -1,5 +1,12 @@ writable = fn($model, Context $context) => $context->creating(); + $this->writable = fn ($model, Context $context) => $context->creating(); return $this; } @@ -23,7 +30,7 @@ trait FlarumField */ public function writableOnUpdate(): static { - $this->writable = fn($model, Context $context) => $context->updating(); + $this->writable = fn ($model, Context $context) => $context->updating(); return $this; } diff --git a/framework/core/src/Api/Schema/Concerns/FlarumRelationship.php b/framework/core/src/Api/Schema/Concerns/FlarumRelationship.php index 92c3c48d9..1962773f9 100644 --- a/framework/core/src/Api/Schema/Concerns/FlarumRelationship.php +++ b/framework/core/src/Api/Schema/Concerns/FlarumRelationship.php @@ -1,5 +1,12 @@ rule('required_with:' . implode(',', $fields), $condition); + return $this->rule('required_with:'.implode(',', $fields), $condition); } public function requiredWithout(array $fields, bool|callable $condition): static { - return $this->rule('required_without:' . implode(',', $fields), $condition); + return $this->rule('required_without:'.implode(',', $fields), $condition); } public function requiredOnCreateWith(array $fields): static diff --git a/framework/core/src/Api/Schema/Contracts/RelationAggregator.php b/framework/core/src/Api/Schema/Contracts/RelationAggregator.php index a1450f09e..e0dd1661c 100644 --- a/framework/core/src/Api/Schema/Contracts/RelationAggregator.php +++ b/framework/core/src/Api/Schema/Contracts/RelationAggregator.php @@ -1,5 +1,12 @@ deserializer)($value, $context); } - if (!is_array($value) || !array_key_exists('data', $value)) { + if (! is_array($value) || ! array_key_exists('data', $value)) { throw new BadRequestException('relationship does not include data key'); } diff --git a/framework/core/src/Api/Sort/SortWithCount.php b/framework/core/src/Api/Sort/SortWithCount.php index ebfce9801..71874eff8 100644 --- a/framework/core/src/Api/Sort/SortWithCount.php +++ b/framework/core/src/Api/Sort/SortWithCount.php @@ -1,5 +1,12 @@