From 0ff8392889b661c106a951c29d4a59d6b3d243de Mon Sep 17 00:00:00 2001 From: Alwin Garside Date: Wed, 14 Nov 2018 18:43:17 +0100 Subject: [PATCH 1/2] Add and clean up PHPDocs Also made a few minor fixes. --- composer.json | 22 ++++++++++++---------- src/Connection.php | 30 ++++++++++++++---------------- src/Console/Index/Populate.php | 4 +++- src/Console/Mapping/Make.php | 4 ++++ src/DSL/SearchBuilder.php | 11 ++++++----- src/DSL/SuggestionBuilder.php | 14 +++++++------- src/Facades/Map.php | 5 +++++ src/Facades/Plastic.php | 25 ++++++++++++++++++++++++- src/Fillers/EloquentFiller.php | 7 +++++-- src/Map/Blueprint.php | 12 ++++++------ src/Map/Builder.php | 6 ++---- src/Map/Grammar.php | 2 +- src/Mappings/Creator.php | 4 ++-- src/Mappings/Mapper.php | 2 +- src/Mappings/Mapping.php | 3 +-- src/Mappings/Mappings.php | 5 +++-- src/PlasticManager.php | 2 +- src/Searchable.php | 2 +- tests/DSL/SearchBuilderTest.php | 2 ++ 19 files changed, 100 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index 617666d..83b9348 100644 --- a/composer.json +++ b/composer.json @@ -13,19 +13,21 @@ ], "require": { "php": ">=7.0", - "elasticsearch/elasticsearch": "~5.3.0", - "ongr/elasticsearch-dsl": "5.*", - "illuminate/container": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*", - "illuminate/contracts": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*", - "illuminate/console": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*", - "illuminate/pagination": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*", - "illuminate/support": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*", - "illuminate/database": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*" + "elasticsearch/elasticsearch": "^5.3.0", + "illuminate/container": "^5.2.0", + "illuminate/contracts": "^5.2.0", + "illuminate/console": "^5.2.0", + "illuminate/database": "^5.2.0", + "illuminate/filesystem": "^5.2.0", + "illuminate/http": "^5.2.0", + "illuminate/pagination": "^5.2.0", + "illuminate/support": "^5.2.0", + "ongr/elasticsearch-dsl": "^5.0.0" }, "require-dev": { - "phpunit/phpunit": "~4.0", + "phpunit/phpunit": "^4.0.0", "mockery/mockery": "^0.9.4", - "phpunit/php-code-coverage": "^2.1" + "phpunit/php-code-coverage": "^2.1.0" }, "autoload": { "psr-4": { diff --git a/src/Connection.php b/src/Connection.php index d9e1a52..434bc68 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -62,7 +62,7 @@ public function getMapBuilder() /** * Get map grammar instance for this connection. * - * @return MapBuilder + * @return MapGrammar */ public function getMapGrammar() { @@ -70,9 +70,9 @@ public function getMapGrammar() } /** - * Get DSL grammar instance for this connection. + * Get DSL query instance for this connection. * - * @return DSLGrammar + * @return DSLQuery */ public function getDSLQuery() { @@ -80,7 +80,7 @@ public function getDSLQuery() } /** - * Get the elastic search client instance. + * Get the Elasticsearch client instance. * * @return Client */ @@ -102,9 +102,7 @@ public function setClient(Client $client) /** * Set the default index. * - * @param $index - * - * @return Connection + * @param string $index */ public function setDefaultIndex($index) { @@ -112,7 +110,7 @@ public function setDefaultIndex($index) } /** - * Execute a map statement on index;. + * Execute a map statement on index. * * @param array $mappings * @@ -124,7 +122,7 @@ public function mapStatement(array $mappings) } /** - * Execute a map statement on index;. + * Execute a search statement on index. * * @param array $search * @@ -136,7 +134,7 @@ public function searchStatement(array $search) } /** - * Execute a map statement on index;. + * Execute a suggest statement on index. * * @param array $suggestions * @@ -148,7 +146,7 @@ public function suggestStatement(array $suggestions) } /** - * Execute a insert statement on index;. + * Execute an insert statement on index. * * @param $params * @@ -160,7 +158,7 @@ public function indexStatement(array $params) } /** - * Execute a update statement on index;. + * Execute an update statement on index. * * @param $params * @@ -172,7 +170,7 @@ public function updateStatement(array $params) } /** - * Execute a update statement on index;. + * Execute a delete statement on index. * * @param $params * @@ -184,7 +182,7 @@ public function deleteStatement(array $params) } /** - * Execute a exists statement on index. + * Execute an exists statement on index. * * @param array $params * @@ -196,7 +194,7 @@ public function existsStatement(array $params) } /** - * Execute a bulk statement on index;. + * Execute a bulk statement on index. * * @param $params * @@ -238,7 +236,7 @@ public function persist() } /** - * Create an elastic search instance. + * Create an Elasticsearch instance. * * @param array $config * diff --git a/src/Console/Index/Populate.php b/src/Console/Index/Populate.php index 3bff07a..cf09862 100644 --- a/src/Console/Index/Populate.php +++ b/src/Console/Index/Populate.php @@ -38,6 +38,8 @@ public function client() /** * Execute the console command. + * + * @throws \Exception */ public function handle() { @@ -94,7 +96,7 @@ protected function populateIndex($index) foreach ($models as $model) { $this->line('Indexing documents of model « '.$model.' » ...'); $model::chunk($chunkSize, function ($items) { - $this->line('Indexing chunk of '.$items->count().' documents ...'); + $this->line('Indexing chunk of '.count($items).' documents ...'); Plastic::persist()->bulkSave($items); }); } diff --git a/src/Console/Mapping/Make.php b/src/Console/Mapping/Make.php index bce4213..05c773f 100644 --- a/src/Console/Mapping/Make.php +++ b/src/Console/Mapping/Make.php @@ -47,6 +47,8 @@ public function __construct(Creator $creator, Composer $composer) /** * Execute the console command. + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function handle() { @@ -61,6 +63,8 @@ public function handle() * Create the mapping file. * * @param $model + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ private function writeMapping($model) { diff --git a/src/DSL/SearchBuilder.php b/src/DSL/SearchBuilder.php index 702cdf7..e6ee4e5 100644 --- a/src/DSL/SearchBuilder.php +++ b/src/DSL/SearchBuilder.php @@ -63,7 +63,7 @@ class SearchBuilder public $index; /** - * The model to use when querying elastic search. + * The model to use when querying Elasticsearch. * * @var Model */ @@ -99,7 +99,7 @@ class SearchBuilder public function __construct(Connection $connection, Query $grammar = null) { $this->connection = $connection; - $this->query = $grammar ?: $connection->getDSLGrammar(); + $this->query = $grammar ?: $connection->getDSLQuery(); } /** @@ -131,7 +131,7 @@ public function index($index) } /** - * Set the eloquent model to use when querying elastic search. + * Set the eloquent model to use when querying Elasticsearch. * * @param Model|Searchable $model * @@ -212,7 +212,7 @@ public function sortBy($fields, $order = null, array $parameters = []) /** * Set the query min score value. * - * @param $score + * @param int $score * * @return $this */ @@ -500,7 +500,7 @@ public function geoShape($field, $type, array $coordinates = [], array $attribut { $query = new GeoShapeQuery(); - $query->addShape($field, $type, $coordinates, $attributes); + $query->addShape($field, $type, $coordinates, GeoShapeQuery::INTERSECTS, $attributes); $this->append($query); @@ -610,6 +610,7 @@ public function range($field, array $attributes = []) * Add a regexp query. * * @param string $field + * @param string $regex * @param array $attributes * * @return $this diff --git a/src/DSL/SuggestionBuilder.php b/src/DSL/SuggestionBuilder.php index d7991aa..59f38ed 100644 --- a/src/DSL/SuggestionBuilder.php +++ b/src/DSL/SuggestionBuilder.php @@ -69,10 +69,10 @@ public function getIndex() /** * Add a completion suggestion. * - * @param $name - * @param $text - * @param $field - * @param array $parameters + * @param string $name + * @param string $text + * @param string $field + * @param array $parameters * * @return $this * @@ -92,8 +92,8 @@ public function completion($name, $text, $field = 'suggest', $parameters = []) * * @param string $name * @param string $text - * @param $field - * @param array $parameters + * @param string $field + * @param array $parameters * * @return $this */ @@ -144,7 +144,7 @@ public function getConnection() /** * Append a suggestion to query. * - * @param $suggestion + * @param \ONGR\ElasticsearchDSL\BuilderInterface $suggestion */ public function append($suggestion) { diff --git a/src/Facades/Map.php b/src/Facades/Map.php index 3ac87d2..b1e3b88 100644 --- a/src/Facades/Map.php +++ b/src/Facades/Map.php @@ -4,6 +4,10 @@ use Illuminate\Support\Facades\Facade; +/** + * @method static create(string $type, \Closure $callback, string $index = null) Create a map on your Elasticsearch index. + * @method static blueprintResolver(\Closure $resolver) Set the Schema Blueprint resolver callback. + */ class Map extends Facade { /** @@ -13,6 +17,7 @@ class Map extends Facade */ protected static function getFacadeAccessor() { + /** @noinspection PhpUndefinedMethodInspection */ return static::$app['plastic']->connection()->getMapBuilder(); } } diff --git a/src/Facades/Plastic.php b/src/Facades/Plastic.php index 7ecf502..406907d 100644 --- a/src/Facades/Plastic.php +++ b/src/Facades/Plastic.php @@ -4,12 +4,35 @@ use Illuminate\Support\Facades\Facade; +/** + * @method static \Sleimanx2\Plastic\Connection connection() Get an Elasticsearch connection instance. + * @method static string getDefaultIndex() Get the default elastic index. + * @method static \Sleimanx2\Plastic\Map\Builder getMapBuilder() Get map builder instance for this connection. + * @method static \Sleimanx2\Plastic\Map\Grammar getMapGrammar() Get map grammar instance for this connection. + * @method static \ONGR\ElasticsearchDSL\Search getDSLQuery() Get DSL grammar instance for this connection. + * @method static \Elasticsearch\Client getClient() Get the Elasticsearch client instance. + * + * @method static setClient(\Elasticsearch\Client $client) Set a custom elastic client. + * @method static setDefaultIndex(string $index) Set the default index. + * @method static array mapStatement(array $mappings) Execute a map statement on index. + * @method static array searchStatement(array $search) Execute a search statement on index. + * @method static array suggestStatement(array $suggestions) Execute a suggest statement on index. + * @method static array indexStatement(array $params) Execute an insert statement on index. + * @method static array updateStatement(array $params) Execute an update statement on index. + * @method static array deleteStatement(array $params) Execute a delete statement on index. + * @method static array|bool existsStatement(array $params) Execute an exists statement on index. + * @method static array bulkStatement(array $params) Execute a bulk statement on index. + * + * @method static \Sleimanx2\Plastic\DSL\SearchBuilder search() Begin a fluent search query builder. + * @method static \Sleimanx2\Plastic\DSL\SuggestionBuilder suggest() Begin a fluent suggest query builder. + * @method static \Sleimanx2\Plastic\Persistence\EloquentPersistence persist() Create a new elastic persistence handler. + */ class Plastic extends Facade { /** * Get a plastic manager instance for the default connection. * - * @return \Sleimanx2\Plastic\DSL\SearchBuilder + * @return \Sleimanx2\Plastic\PlasticManager */ protected static function getFacadeAccessor() { diff --git a/src/Fillers/EloquentFiller.php b/src/Fillers/EloquentFiller.php index 714ace2..8c36888 100644 --- a/src/Fillers/EloquentFiller.php +++ b/src/Fillers/EloquentFiller.php @@ -36,6 +36,7 @@ public function fill(Model $model, Result $result) * @param array $hit * * @return static + * @throws \ReflectionException */ public function fillModel(Model $model, $hit = []) { @@ -78,11 +79,12 @@ public function fillModel(Model $model, $hit = []) /** * Fill a model with form an elastic hit. * - * @param Model $model - * @param array $attributes + * @param Model $model + * @param array $attributes * @param Relation $parentRelation * * @return mixed + * @throws \ReflectionException */ public function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null) { @@ -105,6 +107,7 @@ public function newFromBuilderRecursive(Model $model, array $attributes = [], Re * Get the relations attributes from a model. * * @param \Illuminate\Database\Eloquent\Model $model + * @throws \ReflectionException */ protected function loadRelationsAttributesRecursive(Model $model) { diff --git a/src/Map/Blueprint.php b/src/Map/Blueprint.php index b577850..893a0d8 100644 --- a/src/Map/Blueprint.php +++ b/src/Map/Blueprint.php @@ -18,7 +18,7 @@ class Blueprint /** * The fields that should be mapped. * - * @var array + * @var Fluent[] */ protected $fields = []; @@ -30,7 +30,7 @@ class Blueprint protected $commands = []; /** - * @var elastic search index + * @var string Elasticsearch index */ private $index; @@ -285,7 +285,7 @@ public function tokenCount($field, $attributes = []) /** * Add a nested map. * - * @param $field + * @param string $field * @param Closure $callback * * @return Fluent @@ -298,7 +298,7 @@ public function nested($field, Closure $callback) /** * Add a object map. * - * @param $field + * @param string $field * @param Closure $callback * * @return Fluent @@ -329,7 +329,7 @@ public function addField($type, $name, array $attributes = []) /** * Get the registered fields. * - * @return array + * @return Fluent[] */ public function getFields() { @@ -339,7 +339,7 @@ public function getFields() /** * Get the command fields. * - * @return array + * @return Fluent[] */ public function getCommands() { diff --git a/src/Map/Builder.php b/src/Map/Builder.php index 25c6691..742172c 100644 --- a/src/Map/Builder.php +++ b/src/Map/Builder.php @@ -41,11 +41,11 @@ public function __construct(Connection $connection) } /** - * Create a map on your elasticsearch index. + * Create a map on your Elasticsearch index. * * @param string $type - * @param string $index * @param Closure $callback + * @param string $index */ public function create($type, Closure $callback, $index = null) { @@ -98,8 +98,6 @@ protected function createBlueprint($type, Closure $callback = null, $index = nul * Set the Schema Blueprint resolver callback. * * @param \Closure $resolver - * - * @return void */ public function blueprintResolver(Closure $resolver) { diff --git a/src/Map/Grammar.php b/src/Map/Grammar.php index 76ecfe3..6820274 100644 --- a/src/Map/Grammar.php +++ b/src/Map/Grammar.php @@ -390,7 +390,7 @@ protected function formatMap(array $map) /** * Compile an array of fluent fields. * - * @param $fields + * @param Fluent[] $fields * * @return array */ diff --git a/src/Mappings/Creator.php b/src/Mappings/Creator.php index 76c4732..4c3c141 100644 --- a/src/Mappings/Creator.php +++ b/src/Mappings/Creator.php @@ -39,6 +39,7 @@ public function __construct(Filesystem $filesystem) * @param $path * * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function create($model, $path) { @@ -66,9 +67,8 @@ public function getStubPath() /** * Get the mapping stub template. * - * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException - * * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function getStub() { diff --git a/src/Mappings/Mapper.php b/src/Mappings/Mapper.php index 39464ec..58aa91f 100644 --- a/src/Mappings/Mapper.php +++ b/src/Mappings/Mapper.php @@ -184,7 +184,7 @@ public function getNotes() /** * Return a filesystem instance. * - * @return Filesystem + * @return Mappings */ public function getRepository() { diff --git a/src/Mappings/Mapping.php b/src/Mappings/Mapping.php index 5ad1e48..c33c449 100644 --- a/src/Mappings/Mapping.php +++ b/src/Mappings/Mapping.php @@ -2,7 +2,6 @@ namespace Sleimanx2\Plastic\Mappings; -use Illuminate\Database\Eloquent\Model; use Sleimanx2\Plastic\Exception\InvalidArgumentException; use Sleimanx2\Plastic\Exception\MissingArgumentException; use Sleimanx2\Plastic\Searchable; @@ -12,7 +11,7 @@ abstract class Mapping /** * Eloquent instance. * - * @var Model + * @var \Sleimanx2\Plastic\Searchable */ protected $model; diff --git a/src/Mappings/Mappings.php b/src/Mappings/Mappings.php index 2245dae..722f3bc 100644 --- a/src/Mappings/Mappings.php +++ b/src/Mappings/Mappings.php @@ -3,6 +3,7 @@ namespace Sleimanx2\Plastic\Mappings; use Illuminate\Database\ConnectionResolverInterface as Resolver; +use Illuminate\Database\Schema\Blueprint; /** * Mapping log repository. @@ -136,7 +137,7 @@ public function getLastBatchNumber() */ public function createRepository() { - $this->schema()->create($this->table, function ($table) { + $this->schema()->create($this->table, function (Blueprint $table) { // The mappings table is responsible for keeping track of which of the // mappings have actually run for the application. We'll create the @@ -182,7 +183,7 @@ protected function table() /** * Resolve the database connection instance. * - * @return \Illuminate\Database\ConnectionInterface + * @return \Illuminate\Database\ConnectionInterface|\Illuminate\Database\Connection */ public function getConnection() { diff --git a/src/PlasticManager.php b/src/PlasticManager.php index 1998090..c9882b3 100644 --- a/src/PlasticManager.php +++ b/src/PlasticManager.php @@ -27,7 +27,7 @@ public function __construct(Application $app) } /** - * Get an elastic search connection instance. + * Get an Elasticsearch connection instance. */ public function connection() { diff --git a/src/Searchable.php b/src/Searchable.php index 4161e76..6362959 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -14,7 +14,7 @@ trait Searchable { /** - * Is indexed in elastic search. + * Is indexed in Elasticsearch. * * @var bool */ diff --git a/tests/DSL/SearchBuilderTest.php b/tests/DSL/SearchBuilderTest.php index 3671e39..b8f7c88 100644 --- a/tests/DSL/SearchBuilderTest.php +++ b/tests/DSL/SearchBuilderTest.php @@ -286,6 +286,7 @@ public function it_set_a_geoPolygon_query() */ public function it_set_a_geoShape_query() { + /** @var \Sleimanx2\Plastic\DSL\SearchBuilder $builder */ $builder = $this->getBuilder(); $builder->geoShape('area', 'point', [3.3, 33.3]); @@ -298,6 +299,7 @@ public function it_set_a_geoShape_query() 'type' => 'point', 'coordinates' => [3.3, 33.3], ], + 'relation' => 'intersects', ], ], ], From 7de88b90ddb6d1375f058748555c1f9de64341ba Mon Sep 17 00:00:00 2001 From: Alwin Garside Date: Wed, 14 Nov 2018 18:48:26 +0100 Subject: [PATCH 2/2] Fix code style --- src/Facades/Map.php | 2 +- src/Facades/Plastic.php | 2 -- src/Fillers/EloquentFiller.php | 11 +++++++---- src/Mappings/Creator.php | 6 ++++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Facades/Map.php b/src/Facades/Map.php index b1e3b88..68f2b63 100644 --- a/src/Facades/Map.php +++ b/src/Facades/Map.php @@ -17,7 +17,7 @@ class Map extends Facade */ protected static function getFacadeAccessor() { - /** @noinspection PhpUndefinedMethodInspection */ + /* @noinspection PhpUndefinedMethodInspection */ return static::$app['plastic']->connection()->getMapBuilder(); } } diff --git a/src/Facades/Plastic.php b/src/Facades/Plastic.php index 406907d..5d8b3cf 100644 --- a/src/Facades/Plastic.php +++ b/src/Facades/Plastic.php @@ -11,7 +11,6 @@ * @method static \Sleimanx2\Plastic\Map\Grammar getMapGrammar() Get map grammar instance for this connection. * @method static \ONGR\ElasticsearchDSL\Search getDSLQuery() Get DSL grammar instance for this connection. * @method static \Elasticsearch\Client getClient() Get the Elasticsearch client instance. - * * @method static setClient(\Elasticsearch\Client $client) Set a custom elastic client. * @method static setDefaultIndex(string $index) Set the default index. * @method static array mapStatement(array $mappings) Execute a map statement on index. @@ -22,7 +21,6 @@ * @method static array deleteStatement(array $params) Execute a delete statement on index. * @method static array|bool existsStatement(array $params) Execute an exists statement on index. * @method static array bulkStatement(array $params) Execute a bulk statement on index. - * * @method static \Sleimanx2\Plastic\DSL\SearchBuilder search() Begin a fluent search query builder. * @method static \Sleimanx2\Plastic\DSL\SuggestionBuilder suggest() Begin a fluent suggest query builder. * @method static \Sleimanx2\Plastic\Persistence\EloquentPersistence persist() Create a new elastic persistence handler. diff --git a/src/Fillers/EloquentFiller.php b/src/Fillers/EloquentFiller.php index 8c36888..a220f47 100644 --- a/src/Fillers/EloquentFiller.php +++ b/src/Fillers/EloquentFiller.php @@ -35,8 +35,9 @@ public function fill(Model $model, Result $result) * @param $model * @param array $hit * - * @return static * @throws \ReflectionException + * + * @return static */ public function fillModel(Model $model, $hit = []) { @@ -79,12 +80,13 @@ public function fillModel(Model $model, $hit = []) /** * Fill a model with form an elastic hit. * - * @param Model $model - * @param array $attributes + * @param Model $model + * @param array $attributes * @param Relation $parentRelation * - * @return mixed * @throws \ReflectionException + * + * @return mixed */ public function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null) { @@ -107,6 +109,7 @@ public function newFromBuilderRecursive(Model $model, array $attributes = [], Re * Get the relations attributes from a model. * * @param \Illuminate\Database\Eloquent\Model $model + * * @throws \ReflectionException */ protected function loadRelationsAttributesRecursive(Model $model) diff --git a/src/Mappings/Creator.php b/src/Mappings/Creator.php index 4c3c141..a146209 100644 --- a/src/Mappings/Creator.php +++ b/src/Mappings/Creator.php @@ -38,8 +38,9 @@ public function __construct(Filesystem $filesystem) * @param $model * @param $path * - * @return string * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + * + * @return string */ public function create($model, $path) { @@ -67,8 +68,9 @@ public function getStubPath() /** * Get the mapping stub template. * - * @return string * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + * + * @return string */ protected function getStub() {