From ff6bf5bbbcde677b0692916ab96b4c94d211fede Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Fri, 18 Sep 2020 18:53:38 +0200 Subject: [PATCH] Fixed type issues --- psalm.xml | 35 ++++++- src/Commands/ReindexCommand.php | 14 ++- src/Index.php | 5 +- src/Model.php | 2 +- src/Query.php | 158 +++++++++++++++++++------------- src/Request.php | 2 +- src/ScoutEngine.php | 5 +- 7 files changed, 147 insertions(+), 74 deletions(-) diff --git a/psalm.xml b/psalm.xml index afc326b..d0152ab 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Commands/ReindexCommand.php b/src/Commands/ReindexCommand.php index 8c06e35..00bae41 100755 --- a/src/Commands/ReindexCommand.php +++ b/src/Commands/ReindexCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use InvalidArgumentException; +use JsonException; use Matchory\Elasticsearch\Connection; use RuntimeException; @@ -83,12 +84,14 @@ public function __construct() * @return void * @throws InvalidArgumentException * @throws RuntimeException + * @throws JsonException + * @psalm-suppress PossiblyInvalidArgument */ public function handle(): void { $this->connection = $this->option("connection") ?: config('es.default'); $this->size = (int)$this->option("bulk-size"); - $this->scroll = $this->option("scroll"); + $this->scroll = (string)$this->option("scroll"); if ($this->size <= 0) { $this->warn("Invalid size value"); @@ -96,7 +99,7 @@ public function handle(): void return; } - $originalIndex = $this->argument('index'); + $originalIndex = (string)$this->argument('index'); $newIndex = $this->argument('new_index'); if ( ! array_key_exists($originalIndex, config('es.indices'))) { @@ -119,17 +122,18 @@ public function handle(): void * * @param string $originalIndex * @param string $newIndex - * @param string|null $scroll_id + * @param string|null $scrollId * @param int $errors * @param int $page * * @throws InvalidArgumentException * @throws RuntimeException + * @throws JsonException */ public function migrate( string $originalIndex, string $newIndex, - ?string $scroll_id = null, + ?string $scrollId = null, int $errors = 0, int $page = 1 ): void { @@ -155,7 +159,7 @@ public function migrate( ->index($originalIndex) ->type('') ->scroll($this->scroll) - ->scrollID($scroll_id) + ->scrollID($scrollId ?: '') ->response(); } diff --git a/src/Index.php b/src/Index.php index 40e1bef..aad9602 100755 --- a/src/Index.php +++ b/src/Index.php @@ -151,7 +151,10 @@ public function exists(): bool public function create(): array { $callback = $this->callback; - $callback($this); + + if ($callback) { + $callback($this); + } $params = [ 'index' => $this->name, diff --git a/src/Model.php b/src/Model.php index 5419409..fce7764 100755 --- a/src/Model.php +++ b/src/Model.php @@ -111,7 +111,7 @@ class Model * @param array $attributes * @param bool $exists */ - public function __construct($attributes = [], $exists = false) + final public function __construct($attributes = [], $exists = false) { $this->attributes = $attributes; diff --git a/src/Query.php b/src/Query.php index c7058c2..b9a5605 100755 --- a/src/Query.php +++ b/src/Query.php @@ -7,8 +7,10 @@ use DateTime; use Elasticsearch\Client; use Illuminate\Database\Query\Builder; +use JsonException; use Matchory\Elasticsearch\Classes\Bulk; use Matchory\Elasticsearch\Classes\Search; +use RuntimeException; use stdClass; use function app; @@ -142,7 +144,7 @@ class Query /** * Elastic model instance * - * @var Model + * @var Model|null */ public $model; @@ -249,16 +251,16 @@ class Query /** * The key that should be used when caching the query. * - * @var string + * @var string|null */ protected $cacheKey; /** - * The number of minutes to cache the query. + * The number of seconds to cache the query. * - * @var int + * @var DateTime|int|null */ - protected $cacheMinutes; + protected $cacheTtl; /** * The cache driver to be used. @@ -281,7 +283,9 @@ class Query */ public function __construct(?Client $client = null) { - $this->client = $client; + if ($client) { + $this->client = $client; + } } /** @@ -365,6 +369,8 @@ public function scrollID(string $scroll): self * * @param string $type * + * @psalm-param 'query_then_fetch'|'dfs_query_then_fetch' $type + * * @return $this */ public function searchType(string $type): self @@ -518,7 +524,7 @@ public function unselect(): self } $this->source[self::SOURCE_EXCLUDE] = array_unique(array_merge( - $this->source[self::SOURCE_EXCLUDE], + $this->source[self::SOURCE_EXCLUDE] ?? [], $fields )); @@ -526,7 +532,7 @@ public function unselect(): self $this->source[self::SOURCE_INCLUDE], function ($field) { return ! in_array( $field, - $this->source[self::SOURCE_EXCLUDE], + $this->source[self::SOURCE_EXCLUDE] ?? [], false ); })); @@ -620,7 +626,7 @@ public function where( break; case self::OPERATOR_EXISTS: - $this->whereExists($name, $value); + $this->whereExists($name); } return $this; @@ -686,23 +692,26 @@ public function whereNot( /** * Set the query where between clause * - * @param $name - * @param $first_value - * @param $last_value + * @param string $name + * @param mixed $firstValue + * @param mixed $lastValue * * @return $this */ - public function whereBetween($name, $first_value, $last_value = null): self - { - if (is_array($first_value) && count($first_value) === 2) { - [$first_value, $last_value] = $first_value; + public function whereBetween( + string $name, + $firstValue, + $lastValue = null + ): self { + if (is_array($firstValue) && count($firstValue) === 2) { + [$firstValue, $lastValue] = $firstValue; } $this->filter[] = [ 'range' => [ $name => [ - 'gte' => $first_value, - 'lte' => $last_value, + 'gte' => $firstValue, + 'lte' => $lastValue, ], ], ]; @@ -713,26 +722,26 @@ public function whereBetween($name, $first_value, $last_value = null): self /** * Set the query where not between clause * - * @param $name - * @param $first_value - * @param $last_value + * @param string $name + * @param mixed $firstValue + * @param mixed|null $lastValue * * @return $this */ public function whereNotBetween( - $name, - $first_value, - $last_value = null + string $name, + $firstValue, + $lastValue = null ): self { - if (is_array($first_value) && count($first_value) === 2) { - [$first_value, $last_value] = $first_value; + if (is_array($firstValue) && count($firstValue) === 2) { + [$firstValue, $lastValue] = $firstValue; } $this->must_not[] = [ 'range' => [ $name => [ - 'gte' => $first_value, - 'lte' => $last_value, + 'gte' => $firstValue, + 'lte' => $lastValue, ], ], ]; @@ -846,12 +855,13 @@ public function distance($name, $value, string $distance): self /** * Search the entire document fields * - * @param string|null $queryString - * @param callable|int|null $settings + * @param string|null $queryString + * @param callable|null $settings + * @param int|null $boost * * @return $this */ - public function search(?string $queryString = null, $settings = null): self + public function search(?string $queryString = null, $settings = null, ?int $boost = null): self { if ($queryString) { $search = new Search( @@ -860,10 +870,7 @@ public function search(?string $queryString = null, $settings = null): self $settings ); - if ( ! is_callable($settings)) { - $search->boost($settings ?: 1); - } - + $search->boost($boost ?? 1); $search->build(); } @@ -871,11 +878,11 @@ public function search(?string $queryString = null, $settings = null): self } /** - * @param $path + * @param string $path * * @return Query */ - public function nested($path): self + public function nested(string $path): self { $this->body = [ 'query' => [ @@ -1008,6 +1015,10 @@ public function get(?string $scrollId = null): Collection { $result = $this->getResult($scrollId); + if ( ! $result) { + return new Collection([]); + } + return $this->getAll($result); } @@ -1024,6 +1035,10 @@ public function first(?string $scroll_id = null): ?Model $result = $this->getResult($scroll_id); + if ( ! $result) { + return null; + } + return $this->getFirst($result); } @@ -1033,6 +1048,7 @@ public function first(?string $scroll_id = null): ?Model * @param string|null $scrollId * * @return array + * @throws JsonException */ public function response(?string $scrollId = null): array { @@ -1047,10 +1063,10 @@ public function response(?string $scrollId = null): array $result = $this->client->search($this->query()); } - if ( ! is_null($this->cacheMinutes)) { + if ( ! is_null($this->cacheTtl)) { app('cache') ->driver($this->cacheDriver) - ->put($this->getCacheKey(), $result, $this->cacheMinutes); + ->put($this->getCacheKey(), $result, $this->cacheTtl); } return $result; @@ -1358,9 +1374,14 @@ public function raw(): Client * Check existence of index * * @return bool + * @throws RuntimeException */ public function exists(): bool { + if ( ! $this->index) { + throw new RuntimeException('No index configured'); + } + $index = new Index($this->index); $index->setClient($this->client); @@ -1386,48 +1407,52 @@ public function createIndex(string $name, ?callable $callback = null): array } /** - * Drop index + * Create the configured index * - * @param $name + * @param callable|null $callback * * @return array + * @throws RuntimeException + * @see Query::createIndex() */ - public function dropIndex($name): array + public function create(?callable $callback = null): array { - $index = new Index($name); - - $index->client = $this->client; + if ( ! $this->index) { + throw new RuntimeException('No index name configured'); + } - return $index->drop(); + return $this->createIndex($this->index, $callback); } /** - * create a new index [alias to createIndex method] + * Drop index * - * @param callable|null $callback + * @param string $name * * @return array */ - public function create(?callable $callback = null): array + public function dropIndex(string $name): array { - $index = new Index($this->index, $callback); + $index = new Index($name); + $index->client = $this->client; - return $index->create(); + return $index->drop(); } /** - * Drop index [alias to dropIndex method] + * Drop the configured index * * @return array + * @throws RuntimeException */ public function drop(): array { - $index = new Index($this->index); - - $index->client = $this->client; + if ( ! $this->index) { + throw new RuntimeException('No index name configured'); + } - return $index->drop(); + return $this->dropIndex($this->index); } /* Caching Methods */ @@ -1464,6 +1489,7 @@ public function cachePrefix(string $prefix): self * Get a unique cache key for the complete query. * * @return string + * @throws JsonException */ public function getCacheKey(): string { @@ -1476,23 +1502,29 @@ public function getCacheKey(): string * Generate the unique cache key for the query. * * @return string + * @throws JsonException */ public function generateCacheKey(): string { - return md5(json_encode($this->query())); + return md5(json_encode( + $this->query(), + JSON_THROW_ON_ERROR + )); } /** * Indicate that the query results should be cached. * - * @param DateTime|int $minutes - * @param string|null $key + * @param DateTime|int $ttl Cache TTL in seconds. + * @param string|null $key Cache key to use. Will be generated + * automatically if omitted. * * @return $this */ - public function remember($minutes, ?string $key = null): self + public function remember($ttl, ?string $key = null): self { - [$this->cacheMinutes, $this->cacheKey] = [$minutes, $key]; + $this->cacheTtl = $ttl; + $this->cacheKey = $key; return $this; } @@ -1520,7 +1552,7 @@ public function __call(string $method, array $parameters): self // Check for model scopes $method = 'scope' . ucfirst($method); - if (method_exists($this->model, $method)) { + if ($this->model && method_exists($this->model, $method)) { $parameters = array_merge([$this], $parameters); $this->model->$method(...$parameters); @@ -1638,7 +1670,7 @@ protected function getBody(): array */ protected function getResult(?string $scrollId = null): ?array { - if (is_null($this->cacheMinutes)) { + if (is_null($this->cacheTtl)) { return $this->response($scrollId); } diff --git a/src/Request.php b/src/Request.php index 911d333..ec26126 100755 --- a/src/Request.php +++ b/src/Request.php @@ -24,7 +24,7 @@ public static function url(?string $host = null): string $server = $_SERVER; $ssl = ( ! empty($server['HTTPS']) && $server['HTTPS'] === 'on'); $sp = strtolower($server['SERVER_PROTOCOL']); - $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : ''); + $protocol = substr($sp, 0, strpos($sp, '/') ?: 0) . (($ssl) ? 's' : ''); $port = (int)$server['SERVER_PORT']; $port = (( ! $ssl && $port === 80) || ($ssl && $port === 443)) ? '' diff --git a/src/ScoutEngine.php b/src/ScoutEngine.php index 8a7dedb..4e2e2ce 100755 --- a/src/ScoutEngine.php +++ b/src/ScoutEngine.php @@ -48,7 +48,9 @@ public function __construct(Elastic $elastic, string $index) */ public function delete($models): void { - $params['body'] = []; + $params = [ + 'body' => [], + ]; $models->each(function (Model $model) use (&$params) { $params['body'][] = [ @@ -116,7 +118,6 @@ public function map(Builder $builder, $results, $model): Collection ->values() ->all(); - /** @noinspection PhpUndefinedMethodInspection */ $models = $model ->whereIn($model->getKeyName(), $keys) ->get()