From a26f88a9b686000ef20d3aa471a66be41254f9e8 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 18 Nov 2022 09:29:46 +0000 Subject: [PATCH] Apply fixes from StyleCI --- extend.php | 22 ++++++-- src/Api/Client.php | 14 ++++- src/Api/Controllers/IndexController.php | 13 ++++- src/Api/Controllers/SearchController.php | 34 ++++++++---- src/Commands/BuildCommand.php | 71 ++++++++++++++---------- src/Elasticsearch/MatchPhraseQuery.php | 12 +++- src/Elasticsearch/MatchQuery.php | 10 ++++ src/Elasticsearch/SimpleSearchQuery.php | 20 +++++-- src/Elasticsearch/TermsQuery.php | 10 ++++ src/Elasticsearch/WildcardQuery.php | 13 ++++- src/Exceptions/SeedingException.php | 13 ++++- src/Jobs/DeletingJob.php | 22 ++++++-- src/Jobs/Job.php | 14 ++++- src/Jobs/SavingJob.php | 26 +++++++-- src/Provider.php | 18 ++++-- src/Save/Document.php | 26 ++++++--- src/Seeders/CommentSeeder.php | 31 +++++++---- src/Seeders/DiscussionSeeder.php | 32 +++++++---- src/Seeders/Seeder.php | 13 ++++- 19 files changed, 308 insertions(+), 106 deletions(-) diff --git a/extend.php b/extend.php index 012f9e6..50c0c60 100644 --- a/extend.php +++ b/extend.php @@ -1,23 +1,33 @@ register(Provider::class), + (new Flarum\ServiceProvider())->register(Provider::class), (new Flarum\Frontend('forum')) - ->js(__DIR__ . '/js/dist/forum.js'), + ->js(__DIR__.'/js/dist/forum.js'), (new Flarum\Frontend('admin')) - ->js(__DIR__ . '/js/dist/admin.js'), + ->js(__DIR__.'/js/dist/admin.js'), - (new Flarum\Locales(__DIR__ . '/resources/locale')), + (new Flarum\Locales(__DIR__.'/resources/locale')), (new Flarum\Routes('api')) ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class) ->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class), - (new Flarum\Console) - ->command(Commands\BuildCommand::class) + (new Flarum\Console()) + ->command(Commands\BuildCommand::class), ]; diff --git a/src/Api/Client.php b/src/Api/Client.php index 3bd431d..372a975 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -1,5 +1,15 @@ queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions"); + if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) { + return parent::get('/blomstra/search/discussions'); + } return parent::get($path); } diff --git a/src/Api/Controllers/IndexController.php b/src/Api/Controllers/IndexController.php index 573ce8c..87c2eb5 100644 --- a/src/Api/Controllers/IndexController.php +++ b/src/Api/Controllers/IndexController.php @@ -1,5 +1,15 @@ run( new ArrayInput([]), - new ConsoleOutput); + new ConsoleOutput() + ); return new EmptyResponse(); } diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 644e66d..767dcca 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -1,11 +1,21 @@ 'updated_at', - 'createdAt' => 'created_at', - 'commentCount' => 'comment_count' + 'createdAt' => 'created_at', + 'commentCount' => 'comment_count', ]; public function __construct(protected Client $elastic, protected UrlGenerator $uri) - {} + { + } protected function data(ServerRequestInterface $request, Document $document) { @@ -58,14 +69,14 @@ class SearchController extends ListDiscussionsController $filterQuery = BoolQuery::create(); - if (! empty($search)) { + if (!empty($search)) { $filterQuery // @todo commented out to use only partial matching for now ->add($this->sentenceMatch($search)) ->add($this->wordMatch($search, 'and')) ->add($this->wordMatch($search, 'or')) // ->add($this->partialMatch($search)) - ; +; } $builder = (new Builder($this->elastic)) @@ -91,7 +102,7 @@ class SearchController extends ListDiscussionsController // If the first level of the relationship wasn't explicitly included, // add it so the code below can look for it - if (! in_array('mostRelevantPost', $include)) { + if (!in_array('mostRelevantPost', $include)) { $include[] = 'mostRelevantPost'; } } @@ -106,19 +117,19 @@ class SearchController extends ListDiscussionsController if ($type === 'posts') { return [ 'most_relevant_post_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } else { return [ 'discussion_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } }); $document->addPaginationLinks( $this->uri->to('api')->route('blomstra.search', [ - 'type' => 'discussions' + 'type' => 'discussions', ]), $request->getQueryParams(), $offset, @@ -132,7 +143,7 @@ class SearchController extends ListDiscussionsController ->select('discussions.*') ->join('posts', 'posts.discussion_id', 'discussions.id') // Extra safety to prevent leaking hidden discussion (titles) towards search results. - ->when($actor->isGuest() || ! $actor->hasPermission('discussion.hide'), fn($query) => $query->whereNull('discussions.hidden_at')) + ->when($actor->isGuest() || !$actor->hasPermission('discussion.hide'), fn ($query) => $query->whereNull('discussions.hidden_at')) ->where(function ($query) use ($results) { $query ->whereIn('discussions.id', $results->pluck('discussion_id')->filter()) @@ -287,7 +298,6 @@ class SearchController extends ListDiscussionsController ); } - protected function getGroups(User $actor): Collection { /** @var Collection $groups */ diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 3494c94..f8a320d 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -1,11 +1,20 @@ [ - 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], - 'created_at' => ['type' => 'date'], - 'updated_at' => ['type' => 'date'], - 'is_private' => ['type' => 'boolean'], - 'is_sticky' => ['type' => 'boolean'], - 'groups' => ['type' => 'integer'], + 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], + 'created_at' => ['type' => 'date'], + 'updated_at' => ['type' => 'date'], + 'is_private' => ['type' => 'boolean'], + 'is_sticky' => ['type' => 'boolean'], + 'groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'], - 'recipient_users' => ['type' => 'integer'], - 'comment_count' => ['type' => 'integer'], - ] + 'recipient_users' => ['type' => 'integer'], + 'comment_count' => ['type' => 'integer'], + ], ]; if ($this->option('recreate')) { // Flush the index. $client->indices()->delete([ 'index' => $index, - 'ignore_unavailable' => true + 'ignore_unavailable' => true, ]); // Create a new index. @@ -68,38 +77,38 @@ class BuildCommand extends Command 'body' => [ 'settings' => [ 'index.max_ngram_diff' => 10, - 'analysis' => [ + 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ - 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english' + 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english', ], 'flarum_analyzer_partial' => [ - 'type' => 'custom', + 'type' => 'custom', 'tokenizer' => 'standard', - 'filter' => [ + 'filter' => [ 'lowercase', - 'partial_search_filter' - ] - ] + 'partial_search_filter', + ], + ], ], 'filter' => [ 'partial_search_filter' => [ - 'type' => 'ngram', - 'min_gram' => 1, - 'max_gram' => 10, - 'token_chars' => ['letter', 'digit', 'symbol'] - ] - ] - ] - ] - ] + 'type' => 'ngram', + 'min_gram' => 1, + 'max_gram' => 10, + 'token_chars' => ['letter', 'digit', 'symbol'], + ], + ], + ], + ], + ], ]); } if ($this->option('recreate') || $this->option('mapping')) { $client->indices()->putMapping([ 'index' => $index, - 'body' => $properties + 'body' => $properties, ]); } @@ -107,7 +116,9 @@ class BuildCommand extends Command /** @var Seeder $seeder */ foreach ($seeders as $seeder) { - if ($only && $seeder->type() !== $only) continue; + if ($only && $seeder->type() !== $only) { + continue; + } $total = 0; @@ -115,7 +126,7 @@ class BuildCommand extends Command ? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id')) : $seeder->query()->max('id'); - while($continueAt !== null) { + while ($continueAt !== null) { /** @var Collection $collection */ $collection = $seeder->query() ->latest('id') diff --git a/src/Elasticsearch/MatchPhraseQuery.php b/src/Elasticsearch/MatchPhraseQuery.php index c82ac3a..4a64540 100644 --- a/src/Elasticsearch/MatchPhraseQuery.php +++ b/src/Elasticsearch/MatchPhraseQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; return [ - 'match_phrase' => $query + 'match_phrase' => $query, ]; } } diff --git a/src/Elasticsearch/MatchQuery.php b/src/Elasticsearch/MatchQuery.php index 21cc07b..2703fc9 100644 --- a/src/Elasticsearch/MatchQuery.php +++ b/src/Elasticsearch/MatchQuery.php @@ -1,5 +1,15 @@ [ - 'query' => $this->value, - 'fields' => $this->fields, - 'analyzer' => $this->analyzer, + 'query' => $this->value, + 'fields' => $this->fields, + 'analyzer' => $this->analyzer, 'default_operator' => 'AND', - 'boost' => $this->boost - ] + 'boost' => $this->boost, + ], ]; } } diff --git a/src/Elasticsearch/TermsQuery.php b/src/Elasticsearch/TermsQuery.php index c312822..c35cc84 100644 --- a/src/Elasticsearch/TermsQuery.php +++ b/src/Elasticsearch/TermsQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; - $query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity; + $query['wildcard'][$this->field]['case_insensitive'] = !$this->sensitivity; if ($this->rewrite) { $query['wildcard'][$this->field]['rewrite'] = $this->rewrite; - } return $query; diff --git a/src/Exceptions/SeedingException.php b/src/Exceptions/SeedingException.php index 549fc6f..81b78e3 100644 --- a/src/Exceptions/SeedingException.php +++ b/src/Exceptions/SeedingException.php @@ -1,13 +1,22 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); return [ - ['delete' => ['_index' => $this->index, '_id' => $document->id]] + ['delete' => ['_index' => $this->index, '_id' => $document->id]], ]; })->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); } } diff --git a/src/Jobs/Job.php b/src/Jobs/Job.php index 288d10e..60fa0ec 100644 --- a/src/Jobs/Job.php +++ b/src/Jobs/Job.php @@ -1,5 +1,15 @@ index = resolve('blomstra.search.elastic_index'); - if (static::$onQueue) $this->onQueue(static::$onQueue); + if (static::$onQueue) { + $this->onQueue(static::$onQueue); + } } } diff --git a/src/Jobs/SavingJob.php b/src/Jobs/SavingJob.php index da80c1f..fec9a05 100644 --- a/src/Jobs/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -1,5 +1,15 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { @@ -19,18 +31,20 @@ class SavingJob extends Job return [ ['index' => ['_index' => $this->index, '_id' => $document->id]], - $document->toArray() + $document->toArray(), ]; }) ->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); - if (Arr::get($response, 'errors') !== true) return true; + if (Arr::get($response, 'errors') !== true) { + return true; + } $items = Arr::get($response, 'items'); diff --git a/src/Provider.php b/src/Provider.php index 4bd7994..8590fae 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -1,11 +1,20 @@ build(); }); - $this->container->instance( 'blomstra.search.elastic_index', $settings->get('blomstra-search.elastic-index', 'flarum') @@ -62,19 +70,19 @@ class Provider extends AbstractServiceProvider $this->container->extend( Client::class, function () { - $pipe = new MiddlewarePipe; + $pipe = new MiddlewarePipe(); $exclude = resolve('flarum.api_client.exclude_middleware'); $middlewareStack = array_filter(resolve('flarum.api.middleware'), function ($middlewareClass) use ($exclude) { - return ! in_array($middlewareClass, $exclude); + return !in_array($middlewareClass, $exclude); }); foreach ($middlewareStack as $middleware) { $pipe->pipe(resolve($middleware)); } - $pipe->pipe(new ExecuteRoute); + $pipe->pipe(new ExecuteRoute()); return new Api\Client($pipe); } diff --git a/src/Save/Document.php b/src/Save/Document.php index ba4d7af..87112db 100644 --- a/src/Save/Document.php +++ b/src/Save/Document.php @@ -1,19 +1,29 @@ getType(new CommentPost); + return resolve(PostSerializer::class)->getType(new CommentPost()); } public function query(): Builder @@ -53,21 +63,22 @@ class CommentSeeder extends Seeder /** * @param CommentPost $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->content, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->content, 'content_partial' => $model->content, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->edited_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model->discussion), - 'comment_count' => $model->discussion->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->edited_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model->discussion), + 'comment_count' => $model->discussion->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/DiscussionSeeder.php b/src/Seeders/DiscussionSeeder.php index 2ebf29b..40af780 100644 --- a/src/Seeders/DiscussionSeeder.php +++ b/src/Seeders/DiscussionSeeder.php @@ -1,5 +1,15 @@ getType(new Discussion); + return resolve(DiscussionSerializer::class)->getType(new Discussion()); } public function query(): Builder @@ -32,6 +42,7 @@ class DiscussionSeeder extends Seeder $includes[] = 'recipientUsers'; $includes[] = 'recipientGroups'; } + return Discussion::query() ->whereNull('hidden_at') ->with($includes); @@ -53,21 +64,22 @@ class DiscussionSeeder extends Seeder /** * @param Discussion $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->title, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->title, 'content_partial' => $model->title, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->last_posted_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model), - 'comment_count' => $model->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->last_posted_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model), + 'comment_count' => $model->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/Seeder.php b/src/Seeders/Seeder.php index cdbc84b..ff4997a 100644 --- a/src/Seeders/Seeder.php +++ b/src/Seeders/Seeder.php @@ -1,5 +1,15 @@ flatten(); } - if (! $discussion->is_private && $permissions->isEmpty()) { + if (!$discussion->is_private && $permissions->isEmpty()) { $permissions = $globalPermission; }