Skip to content

Commit

Permalink
Fixed type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiergummi committed Sep 18, 2020
1 parent 5f686c6 commit efb3e4e
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 142 deletions.
4 changes: 3 additions & 1 deletion src/Classes/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Matchory\Elasticsearch\Query;

use function is_callable;

/**
* Class Search
*
Expand Down Expand Up @@ -60,7 +62,7 @@ public function __construct(
$this->query = $query;
$this->queryString = $queryString;

if (is_callback_function($settings)) {
if (is_callable($settings)) {
$settings($this);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Collection extends BaseCollection
protected $maxScore;

/**
* @var int|null
* @var float|null
*/
protected $duration;

Expand Down Expand Up @@ -98,7 +98,7 @@ public function getMaxScore(): ?int
return $this->maxScore;
}

public function getDuration(): ?int
public function getDuration(): ?float
{
return $this->duration;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/CreateIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Matchory\Elasticsearch\Connection;
use RuntimeException;

use function app;
use function array_keys;
use function config;
use function is_null;

class CreateIndexCommand extends Command
{
/**
Expand Down Expand Up @@ -47,11 +52,6 @@ public function handle(): void
{
$connectionName = $this->option("connection") ?: config('es.default');
$connection = $this->es->connection($connectionName);

if ( ! $connection) {
throw new RuntimeException('No connection');
}

$client = $connection->raw();
$indices = ! is_null($this->argument('index'))
? [$this->argument('index')]
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/DropIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Matchory\Elasticsearch\Connection;
use RuntimeException;

use function app;
use function array_keys;
use function config;
use function is_null;

class DropIndexCommand extends Command
{
/**
Expand Down Expand Up @@ -48,11 +53,6 @@ public function handle(): void
{
$connectionName = $this->option("connection") ?: config('es.default');
$connection = $this->es->connection($connectionName);

if ( ! $connection) {
throw new RuntimeException('No connection');
}

$force = $this->option("force") ?: 0;
$client = $connection->raw();
$indices = ! is_null($this->argument('index'))
Expand Down
13 changes: 8 additions & 5 deletions src/Commands/ListIndicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
use Matchory\Elasticsearch\Connection;
use RuntimeException;

use function app;
use function array_key_exists;
use function config;
use function count;
use function explode;
use function is_array;
use function trim;

/**
* Class ListIndicesCommand
*
Expand Down Expand Up @@ -71,11 +79,6 @@ public function handle(): void
{
$connectionName = $this->option("connection") ?: config('es.default');
$connection = $this->es->connection($connectionName);

if ( ! $connection) {
throw new RuntimeException('No connection');
}

$indices = $connection->raw()->cat()->indices();
$indices = is_array($indices)
? $this->getIndicesFromArrayResponse($indices)
Expand Down
13 changes: 8 additions & 5 deletions src/Commands/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
use Matchory\Elasticsearch\Connection;
use RuntimeException;

use function app;
use function array_key_exists;
use function ceil;
use function config;
use function count;
use function json_encode;

/**
* Class ReindexCommand
*
Expand Down Expand Up @@ -83,7 +90,7 @@ public function handle(): void
$this->size = (int)$this->option("bulk-size");
$this->scroll = $this->option("scroll");

if ($this->size <= 0 || ! is_numeric($this->size)) {
if ($this->size <= 0) {
$this->warn("Invalid size value");

return;
Expand Down Expand Up @@ -128,10 +135,6 @@ public function migrate(
): void {
$connection = $this->es->connection($this->connection);

if ( ! $connection) {
throw new RuntimeException('No connection');
}

if ($page === 1) {
$pages = (int)ceil(
$connection
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/UpdateIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Matchory\Elasticsearch\Connection;
use RuntimeException;

use function app;
use function array_keys;
use function config;
use function is_null;

class UpdateIndexCommand extends Command
{
/**
Expand Down Expand Up @@ -46,11 +51,6 @@ public function handle(): void
{
$connectionName = $this->option("connection") ?: config('es.default');
$connection = $this->es->connection($connectionName);

if ( ! $connection) {
throw new RuntimeException('No connection');
}

$client = $connection->raw();
$indices = ! is_null($this->argument('index'))
? [$this->argument('index')]
Expand Down
1 change: 1 addition & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Monolog\Logger;
use RuntimeException;

use function app;
use function array_key_exists;
use function call_user_func_array;
use function method_exists;
Expand Down
7 changes: 6 additions & 1 deletion src/ElasticsearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use RuntimeException;

use function class_exists;
use function config;
use function config_path;
use function method_exists;
use function str_starts_with;
use function version_compare;

Expand Down Expand Up @@ -46,7 +48,10 @@ public function boot(): void

// Auto configuration with lumen framework.

if (Str::contains($this->app->version(), 'Lumen')) {
if (
method_exists($this->app, 'configure') &&
Str::contains($this->app->version(), 'Lumen')
) {
$this->app->configure('es');
}

Expand Down
10 changes: 3 additions & 7 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function count;
use function func_get_args;
use function is_array;
use function is_callback_function;

/**
* Class Index
Expand Down Expand Up @@ -42,7 +41,7 @@ class Index
/**
* Index create callback
*
* @var null
* @var callable|null
*/
public $callback;

Expand All @@ -63,7 +62,7 @@ class Index
/**
* Index mapping
*
* @var int
* @var array
*/
public $mappings = [];

Expand Down Expand Up @@ -152,10 +151,7 @@ public function exists(): bool
public function create(): array
{
$callback = $this->callback;

if (is_callback_function($callback)) {
$callback($this);
}
$callback($this);

$params = [
'index' => $this->name,
Expand Down
62 changes: 39 additions & 23 deletions src/Model.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php

declare(strict_types=1);

namespace Matchory\Elasticsearch;

use Illuminate\Support\Str;
use InvalidArgumentException;
use JsonException;
use RuntimeException;

use function app;
use function array_diff;
use function array_key_exists;
use function config;
use function in_array;
use function json_encode;
use function method_exists;
Expand Down Expand Up @@ -118,7 +123,7 @@ public function __construct($attributes = [], $exists = false)
/**
* Get all model records
*
* @return mixed
* @return Collection
* @throws InvalidArgumentException
* @throws RuntimeException
*/
Expand Down Expand Up @@ -363,16 +368,22 @@ public function toArray(): array
* @param int $options
*
* @return string
* @throws JsonException
*/
public function toJson($options = 0): string
{
return json_encode($this->toArray(), $options);
return json_encode(
$this->toArray(),
JSON_THROW_ON_ERROR | $options
);
}

/**
* Delete model record
*
* @return $this
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function delete(): ?self
{
Expand All @@ -393,9 +404,11 @@ public function delete(): ?self
/**
* Save data to model
*
* @return string
* @return $this
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function save(): string
public function save(): self
{
$fields = array_diff($this->attributes, [
'_index',
Expand All @@ -411,29 +424,30 @@ public function save(): string
->newQuery()
->id($this->getID())
->update($fields);

return $this;
}

// Check if model key exists in items
if (array_key_exists('_id', $this->attributes)) {
$created = $this
->newQuery()
->id($this->attributes['_id'])
->insert($fields);
$this->_id = $this->attributes['_id'];
} else {
// Check if model key exists in items

if (array_key_exists('_id', $this->attributes)) {
$created = $this
->newQuery()
->id($this->attributes['_id'])
->insert($fields);
$this->_id = $this->attributes['_id'];
} else {
$created = $this->newQuery()->insert($fields);
$this->_id = $created->_id;
}
$created = $this->newQuery()->insert($fields);
$this->_id = $created->_id;
}

$this->setConnection($this->getConnection());
$this->setIndex($created->_index);
$this->setConnection($this->getConnection());
$this->setIndex($created->_index);

// Match earlier versions
$this->_index = $created->_index;
$this->_type = $this->type;
// Match earlier versions
$this->_index = $created->_index;
$this->_type = $this->type;

$this->exists = true;
}
$this->exists = true;

return $this;
}
Expand Down Expand Up @@ -465,6 +479,8 @@ public function getID()
* @param array $parameters
*
* @return mixed
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function __call(string $method, array $parameters)
{
Expand Down
Loading

0 comments on commit efb3e4e

Please sign in to comment.