1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 07:27:39 +02:00

Apply fixes from StyleCI

This commit is contained in:
StyleCI Bot
2024-05-10 13:13:50 +00:00
committed by Sami Mazouz
parent 51e2ab8502
commit a442aad3be
28 changed files with 171 additions and 40 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -1,15 +1,20 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Endpoint\Concerns;
use Flarum\Api\Resource\AbstractDatabaseResource;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Laravel\EloquentResource;
/**
* This is directed at eager loading relationships apart from the request includes.
@@ -108,7 +113,7 @@ trait HasEagerLoading
foreach ($relations as $relation) {
if (isset($addedRelationWhere[$relation])) {
$whereRelations[$relation] = $addedRelationWhere[$relation];;
$whereRelations[$relation] = $addedRelationWhere[$relation];
} else {
$simpleRelations[] = $relation;
}
@@ -186,7 +191,7 @@ trait HasEagerLoading
}
/**
* From format of: 'relation' => [ ...nested ] to ['relation', 'relation.nested']
* From format of: 'relation' => [ ...nested ] to ['relation', 'relation.nested'].
*/
private function stringInclude(array $include): array
{

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Endpoint\Concerns;
use RuntimeException;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Endpoint\Concerns;
trait IncludesData

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Endpoint\Concerns;
use Illuminate\Contracts\Validation\Validator;
@@ -105,13 +112,13 @@ trait SavesAndValidatesData
{
$body = (array) $context->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',
]);

View File

@@ -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)));
}
}

View File

@@ -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),
);

View File

@@ -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();
}

View File

@@ -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',
]);

View File

@@ -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),
);

View File

@@ -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
{

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Attachable as AttachableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Context;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Creatable as CreatableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Deletable as DeletableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Findable as FindableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Context;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Paginatable as PaginatableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource\Contracts;
use Tobyz\JsonApiServer\Resource\Updatable as UpdatableContract;

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Resource;
use Illuminate\Database\Eloquent\Builder;
@@ -8,7 +15,6 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Laravel\EloquentResource;
use Tobyz\JsonApiServer\Laravel\Field\ToMany;
use Tobyz\JsonApiServer\Laravel\Field\ToOne;
use Tobyz\JsonApiServer\Schema\Field\Relationship;
@@ -42,13 +48,11 @@ abstract class EloquentBuffer
Context $context,
?array $aggregate = null,
): void {
if (!($models = static::getBuffer($model, $relationName, $aggregate))) {
if (! ($models = static::getBuffer($model, $relationName, $aggregate))) {
return;
}
$loader = function ($relation) use (
$model,
$relationName,
$relationship,
$context,
$aggregate,
@@ -74,8 +78,8 @@ abstract class EloquentBuffer
foreach ($resources as $resource) {
$modelClass = get_class($resource->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))

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Schema\Concerns;
use Flarum\Api\Context;
@@ -13,7 +20,7 @@ trait FlarumField
*/
public function writableOnCreate(): static
{
$this->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;
}

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Schema\Concerns;
trait FlarumRelationship

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Schema\Concerns;
use Closure;

View File

@@ -1,9 +1,15 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Schema\Concerns;
use Flarum\Api\Context;
use Tobyz\JsonApiServer\Endpoint\Update;
use Illuminate\Validation\Rule;
trait HasValidationRules
@@ -106,12 +112,12 @@ trait HasValidationRules
public function requiredWith(array $fields, bool|callable $condition): static
{
return $this->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

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Schema\Contracts;
interface RelationAggregator

View File

@@ -25,7 +25,7 @@ class ToOne extends BaseToOne
return ($this->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');
}

View File

@@ -1,5 +1,12 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Sort;
use Tobyz\JsonApiServer\Laravel\Sort\SortWithCount as BaseSortWithCount;