From 69003373a9b1e939fbb0886caab31cd8d815ab5f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 18 Oct 2023 12:26:10 +0000 Subject: [PATCH] Apply fixes from StyleCI --- extend.php | 2 +- src/Api/Controllers/SearchController.php | 2 +- src/Commands/BuildCommand.php | 8 ++--- src/Discussion/DiscussionIndexer.php | 14 ++++++-- src/Discussion/DiscussionSearcher.php | 14 ++++++-- src/Discussion/FulltextFilter.php | 38 ++++++++++++-------- src/Discussion/PrivateFilterMutator.php | 12 ++++++- src/Elasticsearch/Builder.php | 10 ++++++ src/Post/CommentPostIndexer.php | 14 ++++++-- src/Search/Concerns/AppliesAccessControl.php | 10 ++++++ src/Search/ElasticIndex.php | 18 +++++++--- src/Search/ElasticSearchDriver.php | 10 ++++++ src/Search/ElasticSearchState.php | 12 ++++++- src/Search/Searcher.php | 22 ++++++++---- 14 files changed, 148 insertions(+), 38 deletions(-) diff --git a/extend.php b/extend.php index 74f2e18..b6b90d2 100644 --- a/extend.php +++ b/extend.php @@ -22,7 +22,7 @@ return [ (new Flarum\Frontend('admin')) ->js(__DIR__.'/js/dist/admin.js'), - (new Flarum\Locales(__DIR__.'/resources/locale')), + new Flarum\Locales(__DIR__.'/resources/locale'), (new Flarum\ServiceProvider()) ->register(Provider::class), diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index a443777..ca57500 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -12,6 +12,7 @@ namespace Blomstra\Search\Api\Controllers; +use Blomstra\Search\Elasticsearch\Builder; use Blomstra\Search\Elasticsearch\MatchPhraseQuery; use Blomstra\Search\Elasticsearch\MatchQuery; use Blomstra\Search\Elasticsearch\TermsQuery; @@ -32,7 +33,6 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Psr\Http\Message\ServerRequestInterface; -use Blomstra\Search\Elasticsearch\Builder; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery; diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index cc2b110..6e372cd 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -13,6 +13,7 @@ namespace Blomstra\Search\Commands; use Blomstra\Search\Discussion\DiscussionIndexer; +use Blomstra\Search\Elasticsearch\Builder; use Blomstra\Search\Post\CommentPostIndexer; use Elasticsearch\Client; use Flarum\Discussion\Discussion; @@ -24,7 +25,6 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Queue\Queue; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Arr; -use Blomstra\Search\Elasticsearch\Builder; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; use Spatie\ElasticsearchQueryBuilder\Queries\RangeQuery; @@ -45,11 +45,11 @@ class BuildCommand extends Command { $indexers = [ 'discussions' => DiscussionIndexer::class, - 'posts' => CommentPostIndexer::class, + 'posts' => CommentPostIndexer::class, ]; $models = [ 'discussions' => Discussion::class, - 'posts' => Post::class, + 'posts' => Post::class, ]; $only = $this->option('only'); @@ -72,7 +72,7 @@ class BuildCommand extends Command } // Create the index. - if (! $this->option('recreate') && $this->option('mapping')) { + if (!$this->option('recreate') && $this->option('mapping')) { $client->indices()->putMapping([ 'index' => $indexer::index(), 'body' => $properties, diff --git a/src/Discussion/DiscussionIndexer.php b/src/Discussion/DiscussionIndexer.php index a7db51a..4cc346a 100644 --- a/src/Discussion/DiscussionIndexer.php +++ b/src/Discussion/DiscussionIndexer.php @@ -1,5 +1,15 @@ elastic->delete(self::index(), $models); } - function build(): void + public function build(): void { $this->elastic->build(self::index(), $this->properties()); } - function flush(): void + public function flush(): void { $this->elastic->flush(self::index()); } diff --git a/src/Discussion/DiscussionSearcher.php b/src/Discussion/DiscussionSearcher.php index 5318ab3..6d036ec 100644 --- a/src/Discussion/DiscussionSearcher.php +++ b/src/Discussion/DiscussionSearcher.php @@ -1,5 +1,15 @@ select('discussions.*'); } diff --git a/src/Discussion/FulltextFilter.php b/src/Discussion/FulltextFilter.php index 68e63bd..28f8bad 100644 --- a/src/Discussion/FulltextFilter.php +++ b/src/Discussion/FulltextFilter.php @@ -1,7 +1,18 @@ retrieveDatabaseRecordsUsing(function (array $response, SearchCriteria $criteria): Collection { $buckets = Collection::make(Arr::get($response, 'aggregations.posts.per_discussion.buckets')) ->map(fn (array $hit) => [ - 'discussion_id' => $hit['key'], - 'most_relevant_post_id' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._id'), + 'discussion_id' => $hit['key'], + 'most_relevant_post_id' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._id'), 'most_relevant_post_score' => Arr::get($hit, 'most_relevant_post_id.most_relevant_post_id.hits.hits.0._score'), ])->keyBy('discussion_id'); $results = Collection::make(Arr::get($response, 'hits.hits')) ->map(fn (array $hit) => [ - 'discussion_id' => $hit['_source']['discussion_id'], - 'most_relevant_post_id' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_id'), + 'discussion_id' => $hit['_source']['discussion_id'], + 'most_relevant_post_id' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_id'), 'most_relevant_post_score' => Arr::get($buckets->get($hit['_source']['discussion_id']), 'most_relevant_post_score'), - 'title_score' => $hit['_score'], + 'title_score' => $hit['_score'], ])->keyBy('discussion_id'); // We have $hits and $buckets, both are sorted by score. @@ -122,17 +132,17 @@ class FulltextFilter extends AbstractFulltextFilter ->select('discussions.*') ->selectRaw( $connection->raw('COALESCE(('.$connection->getQueryGrammar()->compileSelect( - $postsQuery = Post::query() - ->select('posts.id') - ->whereIn('posts.id', $results->pluck('most_relevant_post_id')->filter()) - ->whereColumn('discussions.id', '=', 'posts.discussion_id') - ->limit(1) - ->toBase() - ).'), first_post_id) as most_relevant_post_id')->getValue($connection->getQueryGrammar()) + $postsQuery = Post::query() + ->select('posts.id') + ->whereIn('posts.id', $results->pluck('most_relevant_post_id')->filter()) + ->whereColumn('discussions.id', '=', 'posts.discussion_id') + ->limit(1) + ->toBase() + ).'), first_post_id) as most_relevant_post_id')->getValue($connection->getQueryGrammar()) ) ->mergeBindings($postsQuery) ->whereIn('discussions.id', $results->pluck('discussion_id')) - ->orderByRaw('FIELD(`discussions`.`id`, ' . implode(',', $results->pluck('discussion_id')->all()) . ')') + ->orderByRaw('FIELD(`discussions`.`id`, '.implode(',', $results->pluck('discussion_id')->all()).')') ->get(); }); } diff --git a/src/Discussion/PrivateFilterMutator.php b/src/Discussion/PrivateFilterMutator.php index b97b016..f447346 100644 --- a/src/Discussion/PrivateFilterMutator.php +++ b/src/Discussion/PrivateFilterMutator.php @@ -1,5 +1,15 @@ getActor(); - if (! $this->extensions->isEnabled('fof-byobu') || $actor->isGuest()) { + if (!$this->extensions->isEnabled('fof-byobu') || $actor->isGuest()) { return; } diff --git a/src/Elasticsearch/Builder.php b/src/Elasticsearch/Builder.php index 4988faf..bf15674 100644 --- a/src/Elasticsearch/Builder.php +++ b/src/Elasticsearch/Builder.php @@ -1,5 +1,15 @@ elastic->save( self::index(), @@ -34,7 +44,7 @@ class CommentPostIndexer implements IndexerInterface ); } - function delete(array $models): void + public function delete(array $models): void { $this->elastic->delete( self::index(), diff --git a/src/Search/Concerns/AppliesAccessControl.php b/src/Search/Concerns/AppliesAccessControl.php index c97104e..b29cdc5 100644 --- a/src/Search/Concerns/AppliesAccessControl.php +++ b/src/Search/Concerns/AppliesAccessControl.php @@ -1,5 +1,15 @@ [ '_index' => $index, - '_id' => $document->id - ] + '_id' => $document->id, + ], ], $document->toArray(), ]; @@ -55,8 +65,8 @@ class ElasticIndex [ 'delete' => [ '_index' => $index(), - '_id' => $model->id - ] + '_id' => $model->id, + ], ], ]; })->flatten(1); diff --git a/src/Search/ElasticSearchDriver.php b/src/Search/ElasticSearchDriver.php index 08bf188..7df31ae 100644 --- a/src/Search/ElasticSearchDriver.php +++ b/src/Search/ElasticSearchDriver.php @@ -1,5 +1,15 @@ $id, - 'score' => Arr::get($hit, '_score'), + 'id' => $id, + 'score' => Arr::get($hit, '_score'), 'weight' => Arr::get($hit, 'sort.0'), ]; })->sortByDesc('weight'); @@ -56,7 +66,7 @@ abstract class Searcher implements SearcherInterface return $this->getQuery($criteria->actor) ->whereIn('id', $ids) - ->orderByRaw('FIELD(id, ' . implode(',', $ids) . ')') + ->orderByRaw('FIELD(id, '.implode(',', $ids).')') ->get(); }); @@ -81,7 +91,7 @@ abstract class Searcher implements SearcherInterface $callback = $state->getRetrieveDatabaseRecordsUsing(); - if (! $callback) { + if (!$callback) { throw new \RuntimeException('No callback set to retrieve database records'); } @@ -94,7 +104,7 @@ abstract class Searcher implements SearcherInterface { $sort = $criteria->sort; - if ($criteria->sortIsDefault && ! empty($state->getDefaultSort())) { + if ($criteria->sortIsDefault && !empty($state->getDefaultSort())) { $sort = $state->getDefaultSort(); }