From a1b91ae9253ef3e82fa7a3fa627f8a403999f37e Mon Sep 17 00:00:00 2001 From: Michael Slowik Date: Sat, 23 Jun 2018 14:20:25 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/ElasticsearchEngine.php | 82 +++++++++++++++++-------------- src/ElasticsearchProvider.php | 4 +- tests/ElasticsearchEngineTest.php | 54 ++++++++++---------- 3 files changed, 75 insertions(+), 65 deletions(-) diff --git a/src/ElasticsearchEngine.php b/src/ElasticsearchEngine.php index 62bf8fb..be1dd14 100644 --- a/src/ElasticsearchEngine.php +++ b/src/ElasticsearchEngine.php @@ -2,11 +2,10 @@ namespace ScoutEngines\Elasticsearch; -use Laravel\Scout\Builder; -use Laravel\Scout\Engines\Engine; use Elasticsearch\Client as Elastic; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\Collection as BaseCollection; +use Laravel\Scout\Builder; +use Laravel\Scout\Engines\Engine; class ElasticsearchEngine extends Engine { @@ -20,7 +19,8 @@ class ElasticsearchEngine extends Engine /** * Create a new engine instance. * - * @param \Elasticsearch\Client $elastic + * @param \Elasticsearch\Client $elastic + * * @return void */ public function __construct(Elastic $elastic) @@ -31,7 +31,8 @@ public function __construct(Elastic $elastic) /** * Update the given model in the index. * - * @param Collection $models + * @param Collection $models + * * @return void */ public function update($models) @@ -41,14 +42,14 @@ public function update($models) $models->each(function ($model) use (&$params) { $params['body'][] = [ 'update' => [ - '_id' => $model->getKey(), + '_id' => $model->getKey(), '_index' => $model->searchableAs(), - '_type' => $model->searchableAs(), - ] + '_type' => $model->searchableAs(), + ], ]; $params['body'][] = [ - 'doc' => $model->toSearchableArray(), - 'doc_as_upsert' => true + 'doc' => $model->toSearchableArray(), + 'doc_as_upsert' => true, ]; }); @@ -58,7 +59,8 @@ public function update($models) /** * Remove the given model from the index. * - * @param Collection $models + * @param Collection $models + * * @return void */ public function delete($models) @@ -68,10 +70,10 @@ public function delete($models) $models->each(function ($model) use (&$params) { $params['body'][] = [ 'delete' => [ - '_id' => $model->getKey(), + '_id' => $model->getKey(), '_index' => $model->searchableAs(), - '_type' => $model->searchableAs(), - ] + '_type' => $model->searchableAs(), + ], ]; }); @@ -81,34 +83,36 @@ public function delete($models) /** * Perform the given search on the engine. * - * @param Builder $builder + * @param Builder $builder + * * @return mixed */ public function search(Builder $builder) { return $this->performSearch($builder, array_filter([ 'numericFilters' => $this->filters($builder), - 'size' => $builder->limit, + 'size' => $builder->limit, ])); } /** * Perform the given search on the engine. * - * @param Builder $builder - * @param int $perPage - * @param int $page + * @param Builder $builder + * @param int $perPage + * @param int $page + * * @return mixed */ public function paginate(Builder $builder, $perPage, $page) { $result = $this->performSearch($builder, [ 'numericFilters' => $this->filters($builder), - 'from' => (($page * $perPage) - $perPage), - 'size' => $perPage, + 'from' => (($page * $perPage) - $perPage), + 'size' => $perPage, ]); - $result['nbPages'] = $result['hits']['total']/$perPage; + $result['nbPages'] = $result['hits']['total'] / $perPage; return $result; } @@ -116,21 +120,22 @@ public function paginate(Builder $builder, $perPage, $page) /** * Perform the given search on the engine. * - * @param Builder $builder - * @param array $options + * @param Builder $builder + * @param array $options + * * @return mixed */ protected function performSearch(Builder $builder, array $options = []) { $params = [ 'index' => $builder->index ?: $builder->model->searchableAs(), - 'body' => [ + 'body' => [ 'query' => [ 'bool' => [ - 'must' => [['query_string' => [ 'query' => "*{$builder->query}*"]]] - ] - ] - ] + 'must' => [['query_string' => ['query' => "*{$builder->query}*"]]], + ], + ], + ], ]; if ($sort = $this->sort($builder)) { @@ -167,7 +172,8 @@ protected function performSearch(Builder $builder, array $options = []) /** * Get the filter array for the query. * - * @param Builder $builder + * @param Builder $builder + * * @return array */ protected function filters(Builder $builder) @@ -184,7 +190,8 @@ protected function filters(Builder $builder) /** * Pluck and return the primary keys of the given results. * - * @param mixed $results + * @param mixed $results + * * @return \Illuminate\Support\Collection */ public function mapIds($results) @@ -195,8 +202,9 @@ public function mapIds($results) /** * Map the given results to instances of the given model. * - * @param mixed $results - * @param \Illuminate\Database\Eloquent\Model $model + * @param mixed $results + * @param \Illuminate\Database\Eloquent\Model $model + * * @return Collection */ public function map($results, $model) @@ -221,7 +229,8 @@ public function map($results, $model) /** * Get the total count from a raw result returned by the engine. * - * @param mixed $results + * @param mixed $results + * * @return int */ public function getTotalCount($results) @@ -232,13 +241,14 @@ public function getTotalCount($results) /** * Generates the sort if theres any. * - * @param Builder $builder + * @param Builder $builder + * * @return array|null */ protected function sort($builder) { if (count($builder->orders) == 0) { - return null; + return; } return collect($builder->orders)->map(function ($order) { diff --git a/src/ElasticsearchProvider.php b/src/ElasticsearchProvider.php index fefd3b8..977ec91 100644 --- a/src/ElasticsearchProvider.php +++ b/src/ElasticsearchProvider.php @@ -2,9 +2,9 @@ namespace ScoutEngines\Elasticsearch; -use Laravel\Scout\EngineManager; -use Illuminate\Support\ServiceProvider; use Elasticsearch\ClientBuilder as ElasticBuilder; +use Illuminate\Support\ServiceProvider; +use Laravel\Scout\EngineManager; class ElasticsearchProvider extends ServiceProvider { diff --git a/tests/ElasticsearchEngineTest.php b/tests/ElasticsearchEngineTest.php index d3c8791..86938f3 100644 --- a/tests/ElasticsearchEngineTest.php +++ b/tests/ElasticsearchEngineTest.php @@ -17,20 +17,20 @@ public function test_update_adds_objects_to_index() 'body' => [ [ 'update' => [ - '_id' => 1, + '_id' => 1, '_index' => 'table', - '_type' => 'table', - ] + '_type' => 'table', + ], ], [ - 'doc' => ['id' => 1 ], - 'doc_as_upsert' => true - ] - ] + 'doc' => ['id' => 1], + 'doc_as_upsert' => true, + ], + ], ]); $engine = new ElasticsearchEngine($client); - $engine->update(Collection::make([new ElasticsearchEngineTestModel])); + $engine->update(Collection::make([new ElasticsearchEngineTestModel()])); } public function test_delete_removes_objects_to_index() @@ -40,16 +40,16 @@ public function test_delete_removes_objects_to_index() 'body' => [ [ 'delete' => [ - '_id' => 1, + '_id' => 1, '_index' => 'table', - '_type' => 'table', - ] + '_type' => 'table', + ], ], - ] + ], ]); $engine = new ElasticsearchEngine($client); - $engine->delete(Collection::make([new ElasticsearchEngineTestModel])); + $engine->delete(Collection::make([new ElasticsearchEngineTestModel()])); } public function test_search_sends_correct_parameters_to_elasticsearch() @@ -57,24 +57,24 @@ public function test_search_sends_correct_parameters_to_elasticsearch() $client = Mockery::mock('Elasticsearch\Client'); $client->shouldReceive('search')->with([ 'index' => 'table', - 'body' => [ + 'body' => [ 'query' => [ 'bool' => [ 'must' => [ ['query_string' => ['query' => '*zonda*']], ['match_phrase' => ['foo' => 1]], - ['terms' => ['bar' => [1, 3]]], - ] - ] + ['terms' => ['bar' => [1, 3]]], + ], + ], ], 'sort' => [ - ['id' => 'desc'] - ] - ] + ['id' => 'desc'], + ], + ], ]); $engine = new ElasticsearchEngine($client); - $builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda'); + $builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel(), 'zonda'); $builder->where('foo', 1); $builder->where('bar', [1, 3]); $builder->orderBy('id', 'desc'); @@ -111,17 +111,17 @@ public function test_map_correctly_maps_results_to_models() $model = Mockery::mock('Illuminate\Database\Eloquent\Model'); $model->shouldReceive('getKeyName')->andReturn('id'); $model->shouldReceive('whereIn')->once()->with('id', ['1'])->andReturn($model); - $model->shouldReceive('get')->once()->andReturn(Collection::make([new ElasticsearchEngineTestModel])); + $model->shouldReceive('get')->once()->andReturn(Collection::make([new ElasticsearchEngineTestModel()])); $results = $engine->map([ 'hits' => [ 'total' => '1', - 'hits' => [ + 'hits' => [ [ - '_id' => '1' - ] - ] - ] + '_id' => '1', + ], + ], + ], ], $model); $this->assertEquals(1, count($results));