From 596378a8ff245cc5e23119a5af796024470ec2ea Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Thu, 24 Sep 2020 19:57:45 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/ClickHouseServiceProvider.php | 7 +- src/Database/Connection.php | 2 +- src/Database/Eloquent/Builder.php | 153 +++++++++++------- src/Database/Eloquent/Collection.php | 17 +- src/Database/Eloquent/Concerns/Common.php | 3 +- src/Database/Eloquent/Model.php | 141 +++++++++------- src/Database/Query/Builder.php | 13 +- src/Database/Query/Pdo.php | 1 + tests/EloquentModelCastingTest.php | 20 +-- tests/EloquentModelWithTest.php | 2 +- tests/Helpers.php | 2 +- tests/Unit/Database/ConnectionTest.php | 16 +- tests/Unit/Database/Eloquent/BuilderTest.php | 38 ++--- .../Unit/Database/Eloquent/CollectionTest.php | 18 ++- tests/Unit/Database/Eloquent/ModelTest.php | 10 +- tests/Unit/Database/Query/BuilderTest.php | 15 +- 16 files changed, 266 insertions(+), 192 deletions(-) diff --git a/src/ClickHouseServiceProvider.php b/src/ClickHouseServiceProvider.php index 12eadbc..25bdbac 100644 --- a/src/ClickHouseServiceProvider.php +++ b/src/ClickHouseServiceProvider.php @@ -4,16 +4,17 @@ namespace Bavix\LaravelClickHouse; -use Illuminate\Database\DatabaseManager; -use Illuminate\Support\ServiceProvider; use Bavix\LaravelClickHouse\Database\Connection; use Bavix\LaravelClickHouse\Database\Eloquent\Model; +use Illuminate\Database\DatabaseManager; +use Illuminate\Support\ServiceProvider; class ClickHouseServiceProvider extends ServiceProvider { /** - * @return void * @throws + * + * @return void */ public function boot(): void { diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 9002aac..91c793b 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -4,9 +4,9 @@ namespace Bavix\LaravelClickHouse\Database; +use Bavix\LaravelClickHouse\Database\Query\Builder; use Bavix\LaravelClickHouse\Database\Query\Pdo; use Tinderbox\ClickhouseBuilder\Query\Grammar; -use Bavix\LaravelClickHouse\Database\Query\Builder; class Connection extends \Tinderbox\ClickhouseBuilder\Integrations\Laravel\Connection { diff --git a/src/Database/Eloquent/Builder.php b/src/Database/Eloquent/Builder.php index a38774a..a0d7620 100755 --- a/src/Database/Eloquent/Builder.php +++ b/src/Database/Eloquent/Builder.php @@ -4,19 +4,19 @@ namespace Bavix\LaravelClickHouse\Database\Eloquent; -use Closure; use BadMethodCallException; +use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; +use Closure; use Illuminate\Contracts\Pagination\LengthAwarePaginator; -use Illuminate\Database\Eloquent\Scope; -use Illuminate\Support\Arr; -use Illuminate\Support\Str; -use Illuminate\Pagination\Paginator; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Concerns\BuildsQueries; -use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\RelationNotFoundException; -use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; +use Illuminate\Database\Eloquent\Relations\Relation; +use Illuminate\Database\Eloquent\Scope; +use Illuminate\Pagination\Paginator; +use Illuminate\Support\Arr; +use Illuminate\Support\Str; use Tinderbox\Clickhouse\Exceptions\ClientException; /** @@ -95,12 +95,14 @@ public function __construct(QueryBuilder $query) * Add a where clause on the primary key to the query. * * @param mixed $id + * * @return $this */ public function whereKey($id): self { if (is_array($id) || $id instanceof Arrayable) { $this->query->whereIn($this->model->getQualifiedKeyName(), $id); + return $this; } @@ -111,12 +113,14 @@ public function whereKey($id): self * Add a where clause on the primary key to the query. * * @param mixed $id + * * @return $this */ public function whereKeyNot($id): self { if (is_array($id) || $id instanceof Arrayable) { $this->query->whereNotIn($this->model->getQualifiedKeyName(), $id); + return $this; } @@ -127,9 +131,10 @@ public function whereKeyNot($id): self * Add a basic where clause to the query. * * @param string|array|\Closure $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $operator + * @param mixed $value + * @param string $boolean + * * @return $this */ public function where($column, $operator = null, $value = null, string $boolean = 'AND'): self @@ -150,8 +155,9 @@ public function where($column, $operator = null, $value = null, string $boolean * Add an "or where" clause to the query. * * @param \Closure|array|string $column - * @param string $operator - * @param mixed $value + * @param string $operator + * @param mixed $value + * * @return $this */ public function orWhere($column, $operator = null, $value = null): self @@ -163,6 +169,7 @@ public function orWhere($column, $operator = null, $value = null): self * Create a collection of models from plain arrays. * * @param iterable $items + * * @return Collection */ public function hydrate(iterable $items): Collection @@ -180,7 +187,8 @@ public function hydrate(iterable $items): Collection * Create a collection of models from a raw query. * * @param string $query - * @param array $bindings + * @param array $bindings + * * @return Collection */ public function fromQuery($query, $bindings = []): Collection @@ -194,6 +202,7 @@ public function fromQuery($query, $bindings = []): Collection * Find a model by its primary key. * * @param mixed $id + * * @return Model|Collection|static[]|static|null */ public function find($id) @@ -209,6 +218,7 @@ public function find($id) * Find multiple models by their primary keys. * * @param \Illuminate\Contracts\Support\Arrayable|array $ids + * * @return Collection */ public function findMany($ids): Collection @@ -224,9 +234,10 @@ public function findMany($ids): Collection * Find a model by its primary key or throw an exception. * * @param mixed $id - * @return Model|Collection * * @throws ModelNotFoundException + * + * @return Model|Collection */ public function findOrFail($id) { @@ -241,17 +252,18 @@ public function findOrFail($id) return $result; } - throw (new ModelNotFoundException)->setModel( - get_class($this->model), $id + throw (new ModelNotFoundException())->setModel( + get_class($this->model), + $id ); } /** * Execute the query and get the first result or throw an exception. * - * @return Model|null - * * @throws ModelNotFoundException + * + * @return Model|null */ public function firstOrFail(): ?Model { @@ -260,9 +272,8 @@ public function firstOrFail(): ?Model */ $model = $this->first(); if ($model === null) { - throw (new ModelNotFoundException) - ->setModel(get_class($this->model)) - ; + throw (new ModelNotFoundException()) + ->setModel(get_class($this->model)); } return $model; @@ -271,8 +282,9 @@ public function firstOrFail(): ?Model /** * Execute the query as a "select" statement. * - * @return Collection * @throws ClientException + * + * @return Collection */ public function get(): Collection { @@ -291,8 +303,9 @@ public function get(): Collection /** * Get the hydrated models without eager loading. * - * @return Model[] * @throws ClientException + * + * @return Model[] */ public function getModels(): array { @@ -305,6 +318,7 @@ public function getModels(): array * Eager load the relationships for the models. * * @param array $models + * * @return array */ public function eagerLoadRelations(array $models): array @@ -324,9 +338,10 @@ public function eagerLoadRelations(array $models): array /** * Eagerly load the relationship on a set of models. * - * @param array $models - * @param string $name + * @param array $models + * @param string $name * @param \Closure $constraints + * * @return array */ protected function eagerLoadRelation(array $models, string $name, Closure $constraints): array @@ -354,6 +369,7 @@ protected function eagerLoadRelation(array $models, string $name, Closure $const * Get the relation instance for the given relation name. * * @param string $name + * * @return Relation */ public function getRelation(string $name): Relation @@ -385,6 +401,7 @@ public function getRelation(string $name): Relation * Get the deeply nested relations for a given top-level relation. * * @param string $relation + * * @return array */ protected function relationsNestedUnder(string $relation): array @@ -396,7 +413,7 @@ protected function relationsNestedUnder(string $relation): array // that start with the given top relations and adds them to our arrays. foreach ($this->eagerLoad as $name => $constraints) { if ($this->isNestedUnder($relation, $name)) { - $nested[substr($name, strlen($relation . '.'))] = $constraints; + $nested[substr($name, strlen($relation.'.'))] = $constraints; } } @@ -408,20 +425,22 @@ protected function relationsNestedUnder(string $relation): array * * @param string $relation * @param string $name + * * @return bool */ protected function isNestedUnder(string $relation, string $name): bool { - return Str::contains($name, '.') && Str::startsWith($name, $relation . '.'); + return Str::contains($name, '.') && Str::startsWith($name, $relation.'.'); } /** * Chunk the results of a query by comparing numeric IDs. * - * @param int $count - * @param callable $callback - * @param null $column + * @param int $count + * @param callable $callback + * @param null $column * @param string|null $alias + * * @return bool */ public function chunkById(int $count, callable $callback, $column = null, $alias = null): bool @@ -438,7 +457,7 @@ public function chunkById(int $count, callable $callback, $column = null, $alias // we will call the callback with the current chunk of these results here. $results = $clone->forPageAfterId($count, $lastId, $column)->get(); - $countResults = (int)$results->count(); + $countResults = (int) $results->count(); if (!$countResults) { break; @@ -462,8 +481,9 @@ public function chunkById(int $count, callable $callback, $column = null, $alias /** * Get an array with the values of a given column. * - * @param string $column + * @param string $column * @param string|null $key + * * @return \Illuminate\Support\Collection */ public function pluck(string $column, $key = null): \Illuminate\Support\Collection @@ -488,11 +508,11 @@ public function pluck(string $column, $key = null): \Illuminate\Support\Collecti * Paginate the given query. * * @param int|null $perPage - * @param array $columns - * @param string $pageName + * @param array $columns + * @param string $pageName * @param int|null $page - * @return LengthAwarePaginator * + * @return LengthAwarePaginator */ public function paginate(?int $perPage = null, $columns = ['*'], $pageName = 'page', $page = null): LengthAwarePaginator { @@ -503,7 +523,7 @@ public function paginate(?int $perPage = null, $columns = ['*'], $pageName = 'pa : $this->model->newCollection(); return $this->paginator($results, $total, $perPage, $page, [ - 'path' => Paginator::resolveCurrentPath(), + 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } @@ -512,11 +532,13 @@ public function paginate(?int $perPage = null, $columns = ['*'], $pageName = 'pa * Paginate the given query into a simple paginator. * * @param int|null $perPage - * @param array $columns - * @param string $pageName + * @param array $columns + * @param string $pageName * @param int|null $page - * @return \Illuminate\Contracts\Pagination\Paginator + * * @throws ClientException + * + * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate(?int $perPage = null, $columns = ['*'], $pageName = 'page', $page = null): \Illuminate\Contracts\Pagination\Paginator { @@ -530,7 +552,7 @@ public function simplePaginate(?int $perPage = null, $columns = ['*'], $pageName $this->skip(($page - 1) * $perPage)->take($perPage + 1); return $this->simplePaginator($this->get($columns), $perPage, $page, [ - 'path' => Paginator::resolveCurrentPath(), + 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); } @@ -539,6 +561,7 @@ public function simplePaginate(?int $perPage = null, $columns = ['*'], $pageName * Call the given local model scopes. * * @param array $scopes + * * @return mixed */ public function scopes(array $scopes) @@ -557,8 +580,8 @@ public function scopes(array $scopes) // care of grouping the "wheres" properly so the logical order doesn't get // messed up when adding scopes. Then we'll return back out the builder. $builder = $builder->callScope( - [$this->model, 'scope' . ucfirst($scope)], - (array)$parameters + [$this->model, 'scope'.ucfirst($scope)], + (array) $parameters ); } @@ -607,7 +630,8 @@ public function applyScopes() * Apply the given scope on the current builder instance. * * @param callable $scope - * @param array $parameters + * @param array $parameters + * * @return mixed */ protected function callScope(callable $scope, $parameters = []) @@ -624,7 +648,7 @@ protected function callScope(callable $scope, $parameters = []) $result = $scope(...array_values($parameters)) ?? $this; - if (count((array)$query->getWheres()) > $originalWhereCount) { + if (count((array) $query->getWheres()) > $originalWhereCount) { $this->addNewWheresWithinGroup($query, $originalWhereCount); } @@ -635,7 +659,8 @@ protected function callScope(callable $scope, $parameters = []) * Nest where conditions by slicing them at the given where count. * * @param \Illuminate\Database\Query\Builder $query - * @param int $originalWhereCount + * @param int $originalWhereCount + * * @return void */ protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCount) @@ -648,11 +673,13 @@ protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCo $query->wheres = []; $this->groupWhereSliceForScope( - $query, array_slice($allWheres, 0, $originalWhereCount) + $query, + array_slice($allWheres, 0, $originalWhereCount) ); $this->groupWhereSliceForScope( - $query, array_slice($allWheres, $originalWhereCount) + $query, + array_slice($allWheres, $originalWhereCount) ); } @@ -660,7 +687,8 @@ protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCo * Slice where conditions at the given offset and add them to the query as a nested condition. * * @param \Illuminate\Database\Query\Builder $query - * @param array $whereSlice + * @param array $whereSlice + * * @return void */ protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice) @@ -672,7 +700,8 @@ protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice) // we don't add any unnecessary nesting thus keeping the query clean. if ($whereBooleans->contains('or')) { $query->wheres[] = $this->createNestedWhere( - $whereSlice, $whereBooleans->first() + $whereSlice, + $whereBooleans->first() ); } else { $query->wheres = array_merge($query->wheres, $whereSlice); @@ -682,8 +711,9 @@ protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice) /** * Create a where array with nested where conditions. * - * @param array $whereSlice + * @param array $whereSlice * @param string $boolean + * * @return array */ protected function createNestedWhere($whereSlice, $boolean = 'and') @@ -699,6 +729,7 @@ protected function createNestedWhere($whereSlice, $boolean = 'and') * Set the relationships that should be eager loaded. * * @param mixed $relations + * * @return $this */ public function with($relations) @@ -714,6 +745,7 @@ public function with($relations) * Prevent the specified relations from being eager loaded. * * @param mixed $relations + * * @return $this */ public function without($relations) @@ -729,6 +761,7 @@ public function without($relations) * Create a new instance of the model being queried. * * @param array $attributes + * * @return Model */ public function newModelInstance($attributes = []) @@ -742,6 +775,7 @@ public function newModelInstance($attributes = []) * Parse a list of relations into individuals. * * @param array $relations + * * @return array */ protected function parseWithRelations(array $relations) @@ -780,6 +814,7 @@ function () { * Create a constraint to select the given columns for the relation. * * @param string $name + * * @return array */ protected function createSelectWithConstraint($name) @@ -796,7 +831,8 @@ function ($query) use ($name) { * Parse the nested relationships in a relation. * * @param string $name - * @param array $results + * @param array $results + * * @return array */ protected function addNestedWiths($name, $results) @@ -855,6 +891,7 @@ public function getEagerLoads() * Set the relationships being eagerly loaded. * * @param array $eagerLoad + * * @return $this */ public function setEagerLoads(array $eagerLoad) @@ -882,6 +919,7 @@ public function setModel(Model $model) * Get the given macro by name. * * @param string $name + * * @return \Closure */ public function getMacro($name) @@ -893,18 +931,21 @@ public function getMacro($name) * Dynamically handle calls into the query instance. * * @param string $method - * @param array $parameters + * @param array $parameters + * * @return mixed */ public function __call($method, $parameters) { if ($method === 'macro') { $this->localMacros[$parameters[0]] = $parameters[1]; + return null; } if (isset($this->localMacros[$method])) { array_unshift($parameters, $this); + return $this->localMacros[$method](...$parameters); } @@ -916,7 +957,7 @@ public function __call($method, $parameters) return call_user_func_array(static::$macros[$method], $parameters); } - if (method_exists($this->model, $scope = 'scope' . ucfirst($method))) { + if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) { return $this->callScope([$this->model, $scope], $parameters); } @@ -933,15 +974,17 @@ public function __call($method, $parameters) * Dynamically handle calls into the query instance. * * @param string $method - * @param array $parameters - * @return mixed + * @param array $parameters * * @throws \BadMethodCallException + * + * @return mixed */ public static function __callStatic($method, $parameters) { if ($method === 'macro') { static::$macros[$parameters[0]] = $parameters[1]; + return null; } diff --git a/src/Database/Eloquent/Collection.php b/src/Database/Eloquent/Collection.php index 49a3e89..96a303d 100644 --- a/src/Database/Eloquent/Collection.php +++ b/src/Database/Eloquent/Collection.php @@ -12,8 +12,9 @@ class Collection extends \Illuminate\Database\Eloquent\Collection /** * Find a model in the collection by key. * - * @param mixed $key - * @param mixed $default + * @param mixed $key + * @param mixed $default + * * @return Model|static */ public function find($key, $default = null) @@ -24,7 +25,7 @@ public function find($key, $default = null) if (is_array($key)) { if ($this->isEmpty()) { - return new static; + return new static(); } return $this->whereIn($this->first()->getKeyName(), $key); @@ -38,9 +39,10 @@ public function find($key, $default = null) /** * Determine if a key exists in the collection. * - * @param mixed $key - * @param mixed $operator - * @param mixed $value + * @param mixed $key + * @param mixed $operator + * @param mixed $value + * * @return bool */ public function contains($key, $operator = null, $value = null): bool @@ -57,7 +59,8 @@ public function contains($key, $operator = null, $value = null): bool /** * Run a map over each of the items. * - * @param callable $callback + * @param callable $callback + * * @return SupportCollection|static */ public function map(callable $callback) diff --git a/src/Database/Eloquent/Concerns/Common.php b/src/Database/Eloquent/Concerns/Common.php index 70d93d6..20a2dd8 100644 --- a/src/Database/Eloquent/Concerns/Common.php +++ b/src/Database/Eloquent/Concerns/Common.php @@ -6,16 +6,15 @@ trait Common { - /** * Save the model to the database. * * @param array $options + * * @return bool */ public function save(array $options = []): bool { return static::insert($this->toArray()); } - } diff --git a/src/Database/Eloquent/Model.php b/src/Database/Eloquent/Model.php index 2ab2855..afe22b3 100644 --- a/src/Database/Eloquent/Model.php +++ b/src/Database/Eloquent/Model.php @@ -5,34 +5,33 @@ namespace Bavix\LaravelClickHouse\Database\Eloquent; use ArrayAccess; -use JsonSerializable; -use Illuminate\Support\Str; -use Illuminate\Contracts\Support\Jsonable; -use Illuminate\Contracts\Support\Arrayable; -use Tinderbox\ClickhouseBuilder\Query\Grammar; use Bavix\LaravelClickHouse\Database\Connection; +use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\ConnectionResolverInterface; +use Illuminate\Database\ConnectionResolverInterface as Resolver; +use Illuminate\Database\Eloquent\Concerns\GuardsAttributes; use Illuminate\Database\Eloquent\Concerns\HasEvents; +use Illuminate\Database\Eloquent\Concerns\HasRelationships; +use Illuminate\Database\Eloquent\Concerns\HidesAttributes; use Illuminate\Database\Eloquent\JsonEncodingException; use Illuminate\Database\Eloquent\MassAssignmentException; -use Illuminate\Database\Eloquent\Concerns\HidesAttributes; -use Illuminate\Database\Eloquent\Concerns\GuardsAttributes; -use Illuminate\Database\Eloquent\Concerns\HasRelationships; -use Illuminate\Database\ConnectionResolverInterface as Resolver; -use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; +use Illuminate\Support\Str; +use JsonSerializable; +use Tinderbox\ClickhouseBuilder\Query\Grammar; /** - * Class Model - * @package Bavix\LaravelClickHouse\Database\Eloquent + * Class Model. */ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable { - use Concerns\HasAttributes, - Concerns\Common, - HasEvents, - HasRelationships, - HidesAttributes, - GuardsAttributes; + use Concerns\HasAttributes; + use Concerns\Common; + use HasEvents; + use HasRelationships; + use HidesAttributes; + use GuardsAttributes; /** * The connection name for the model. @@ -114,7 +113,8 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab /** * Create a new Eloquent model instance. * - * @param array $attributes + * @param array $attributes + * * @return void */ public function __construct(array $attributes = []) @@ -183,10 +183,11 @@ public static function clearBootedModels() /** * Fill the model with an array of attributes. * - * @param array $attributes - * @return $this + * @param array $attributes * * @throws \Illuminate\Database\Eloquent\MassAssignmentException + * + * @return $this */ public function fill(array $attributes) { @@ -211,7 +212,8 @@ public function fill(array $attributes) /** * Fill the model with an array of attributes. Force mass assignment. * - * @param array $attributes + * @param array $attributes + * * @return $this */ public function forceFill(array $attributes) @@ -224,7 +226,8 @@ public function forceFill(array $attributes) /** * Remove the table name from a given key. * - * @param string $key + * @param string $key + * * @return string */ protected function removeTableFromKey($key) @@ -235,8 +238,9 @@ protected function removeTableFromKey($key) /** * Create a new instance of the given model. * - * @param array $attributes - * @param bool $exists + * @param array $attributes + * @param bool $exists + * * @return static */ public function newInstance($attributes = [], $exists = false) @@ -258,8 +262,9 @@ public function newInstance($attributes = [], $exists = false) /** * Create a new model instance that is existing. * - * @param array $attributes - * @param string|null $connection + * @param array $attributes + * @param string|null $connection + * * @return static */ public function newFromBuilder(array $attributes = [], string $connection = null) @@ -282,25 +287,26 @@ public function newFromBuilder(array $attributes = [], string $connection = null */ public static function all() { - return (new static)->newQuery()->get(); + return (new static())->newQuery()->get(); } /** * Begin querying a model with eager loading. * - * @param array|string $relations + * @param array|string $relations + * * @return Builder|static */ public static function with($relations) { - return (new static)->newQuery()->with( + return (new static())->newQuery()->with( is_string($relations) ? func_get_args() : $relations ); } public static function query(): Builder { - return (new static)->newQuery(); + return (new static())->newQuery(); } public function newQuery(): Builder @@ -340,7 +346,8 @@ protected function newBaseQueryBuilder(): QueryBuilder /** * Create a new Eloquent Collection instance. * - * @param array $models + * @param array $models + * * @return Collection */ public function newCollection(array $models = []): Collection @@ -361,10 +368,11 @@ public function toArray(): array /** * Convert the model instance to JSON. * - * @param int $options - * @return string + * @param int $options * * @throws \Illuminate\Database\Eloquent\JsonEncodingException + * + * @return string */ public function toJson($options = 0): string { @@ -410,7 +418,8 @@ public function getConnectionName(): string /** * Set the connection associated with the model. * - * @param string $name + * @param string $name + * * @return $this */ public function setConnection(string $name) @@ -423,7 +432,8 @@ public function setConnection(string $name) /** * Resolve a connection instance. * - * @param string|null $connection + * @param string|null $connection + * * @return Connection|\Illuminate\Database\ConnectionInterface */ public static function resolveConnection(string $connection = null) @@ -444,7 +454,8 @@ public static function getConnectionResolver(): ConnectionResolverInterface /** * Set the connection resolver instance. * - * @param \Illuminate\Database\ConnectionResolverInterface $resolver + * @param \Illuminate\Database\ConnectionResolverInterface $resolver + * * @return void */ public static function setConnectionResolver(Resolver $resolver): void @@ -461,7 +472,9 @@ public function getTable(): string { if (!isset($this->table)) { $this->setTable(str_replace( - '\\', '', Str::snake(Str::plural(class_basename($this))) + '\\', + '', + Str::snake(Str::plural(class_basename($this))) )); } @@ -471,7 +484,8 @@ public function getTable(): string /** * Set the table associated with the model. * - * @param string $table + * @param string $table + * * @return $this */ public function setTable(string $table): self @@ -494,7 +508,8 @@ public function getKeyName(): string /** * Set the primary key for the model. * - * @param string $key + * @param string $key + * * @return $this */ public function setKeyName(string $key): self @@ -527,7 +542,8 @@ public function getKeyType(): string /** * Set the data type for the primary key. * - * @param string $type + * @param string $type + * * @return $this */ public function setKeyType(string $type) @@ -570,7 +586,8 @@ public function getPerPage(): int /** * Set the number of models to return per page. * - * @param int $perPage + * @param int $perPage + * * @return $this */ public function setPerPage(int $perPage): self @@ -583,7 +600,8 @@ public function setPerPage(int $perPage): self /** * Dynamically retrieve attributes on the model. * - * @param string $key + * @param string $key + * * @return mixed */ public function __get($key) @@ -594,8 +612,9 @@ public function __get($key) /** * Dynamically set attributes on the model. * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value + * * @return void */ public function __set($key, $value) @@ -606,7 +625,8 @@ public function __set($key, $value) /** * Determine if the given attribute exists. * - * @param mixed $offset + * @param mixed $offset + * * @return bool */ public function offsetExists($offset): bool @@ -617,7 +637,8 @@ public function offsetExists($offset): bool /** * Get the value for a given offset. * - * @param mixed $offset + * @param mixed $offset + * * @return mixed */ public function offsetGet($offset) @@ -628,8 +649,9 @@ public function offsetGet($offset) /** * Set the value for a given offset. * - * @param mixed $offset - * @param mixed $value + * @param mixed $offset + * @param mixed $value + * * @return void */ public function offsetSet($offset, $value): void @@ -640,7 +662,8 @@ public function offsetSet($offset, $value): void /** * Unset the value for a given offset. * - * @param mixed $offset + * @param mixed $offset + * * @return void */ public function offsetUnset($offset): void @@ -651,7 +674,8 @@ public function offsetUnset($offset): void /** * Determine if an attribute or relation exists on the model. * - * @param string $key + * @param string $key + * * @return bool */ public function __isset($key) @@ -662,7 +686,8 @@ public function __isset($key) /** * Unset an attribute on the model. * - * @param string $key + * @param string $key + * * @return void */ public function __unset($key) @@ -673,8 +698,9 @@ public function __unset($key) /** * Handle dynamic method calls into the model. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * * @return mixed */ public function __call($method, $parameters) @@ -685,12 +711,13 @@ public function __call($method, $parameters) /** * Handle dynamic static method calls into the method. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * * @return mixed */ public static function __callStatic($method, $parameters) { - return (new static)->$method(...$parameters); + return (new static())->$method(...$parameters); } } diff --git a/src/Database/Query/Builder.php b/src/Database/Query/Builder.php index 9a6f185..b2645bb 100644 --- a/src/Database/Query/Builder.php +++ b/src/Database/Query/Builder.php @@ -4,14 +4,13 @@ namespace Bavix\LaravelClickHouse\Database\Query; +use Bavix\LaravelClickHouse\Database\Connection; use Illuminate\Support\Arr; use Illuminate\Support\Collection; -use Tinderbox\Clickhouse\Common\Format; use Illuminate\Support\Traits\Macroable; -use Tinderbox\ClickhouseBuilder\Exceptions\GrammarException; -use Tinderbox\ClickhouseBuilder\Query\Grammar; +use Tinderbox\Clickhouse\Common\Format; use Tinderbox\ClickhouseBuilder\Query\BaseBuilder; -use Bavix\LaravelClickHouse\Database\Connection; +use Tinderbox\ClickhouseBuilder\Query\Grammar; class Builder extends BaseBuilder { @@ -94,10 +93,10 @@ public function newQuery(): self /** * Insert in table data from files. * - * @param array $columns - * @param array $files + * @param array $columns + * @param array $files * @param string $format - * @param int $concurrency + * @param int $concurrency * * @throws \Tinderbox\Clickhouse\Exceptions\ClientException * diff --git a/src/Database/Query/Pdo.php b/src/Database/Query/Pdo.php index d23b8aa..417dfd0 100644 --- a/src/Database/Query/Pdo.php +++ b/src/Database/Query/Pdo.php @@ -8,6 +8,7 @@ class Pdo { /** * @param mixed $binding + * * @return mixed */ public function quote($binding) diff --git a/tests/EloquentModelCastingTest.php b/tests/EloquentModelCastingTest.php index 30845ee..3cd65af 100644 --- a/tests/EloquentModelCastingTest.php +++ b/tests/EloquentModelCastingTest.php @@ -9,16 +9,16 @@ class EloquentModelCastingTest extends EloquentModelTest use Helpers; protected $casts = [ - 'intAttribute' => 'int', - 'floatAttribute' => 'float', - 'stringAttribute' => 'string', - 'boolAttribute' => 'bool', - 'booleanAttribute' => 'boolean', - 'objectAttribute' => 'object', - 'arrayAttribute' => 'array', - 'jsonAttribute' => 'json', - 'dateAttribute' => 'date', - 'datetimeAttribute' => 'datetime', + 'intAttribute' => 'int', + 'floatAttribute' => 'float', + 'stringAttribute' => 'string', + 'boolAttribute' => 'bool', + 'booleanAttribute' => 'boolean', + 'objectAttribute' => 'object', + 'arrayAttribute' => 'array', + 'jsonAttribute' => 'json', + 'dateAttribute' => 'date', + 'datetimeAttribute' => 'datetime', 'timestampAttribute' => 'timestamp', ]; diff --git a/tests/EloquentModelWithTest.php b/tests/EloquentModelWithTest.php index e073e63..7cc8ba8 100644 --- a/tests/EloquentModelWithTest.php +++ b/tests/EloquentModelWithTest.php @@ -4,8 +4,8 @@ namespace Bavix\LaravelClickHouse\Tests; -use Mockery\MockInterface; use Bavix\LaravelClickHouse\Database\Eloquent\Builder; +use Mockery\MockInterface; class EloquentModelWithTest extends EloquentModelTest { diff --git a/tests/Helpers.php b/tests/Helpers.php index 9ebbbca..7273653 100644 --- a/tests/Helpers.php +++ b/tests/Helpers.php @@ -4,9 +4,9 @@ namespace Bavix\LaravelClickHouse\Tests; -use Mockery; use Faker\Factory; use Faker\Generator; +use Mockery; trait Helpers { diff --git a/tests/Unit/Database/ConnectionTest.php b/tests/Unit/Database/ConnectionTest.php index 60ca9dd..225665c 100644 --- a/tests/Unit/Database/ConnectionTest.php +++ b/tests/Unit/Database/ConnectionTest.php @@ -4,14 +4,13 @@ namespace Bavix\LaravelClickHouse\Tests\Unit\Database; -use PHPUnit\Framework\TestCase; use Bavix\LaravelClickHouse\Database\Connection; use Bavix\LaravelClickHouse\Database\Query\Builder; +use PHPUnit\Framework\TestCase; use Tinderbox\Clickhouse\Exceptions\ClientException; class ConnectionTest extends TestCase { - /** * @var Connection */ @@ -24,8 +23,8 @@ public function setUp(): void { parent::setUp(); $this->connection = new Connection([ - 'host' => 'localhost', - 'port' => '8123', + 'host' => 'localhost', + 'port' => '8123', 'database' => 'default', ]); } @@ -39,8 +38,9 @@ public function testQuery(): void } /** - * @return void * @throws ClientException + * + * @return void */ public function testSystemEvents(): void { @@ -52,8 +52,9 @@ public function testSystemEvents(): void } /** - * @return void * @throws ClientException + * + * @return void */ public function testMyDatabase(): void { @@ -82,8 +83,7 @@ public function testMyDatabase(): void ]; $this->connection->query() ->table('tests.dt') - ->insert($values) - ; + ->insert($values); self::assertEquals(3, $this->connection->query()->table('tests.dt')->count()); diff --git a/tests/Unit/Database/Eloquent/BuilderTest.php b/tests/Unit/Database/Eloquent/BuilderTest.php index be952a4..976db31 100644 --- a/tests/Unit/Database/Eloquent/BuilderTest.php +++ b/tests/Unit/Database/Eloquent/BuilderTest.php @@ -4,21 +4,21 @@ namespace Bavix\LaravelClickHouse\Tests\Unit\Database\Eloquent; -use Mockery\Mock; -use PHPUnit\Framework\TestCase; -use Illuminate\Database\DatabaseManager; -use Tinderbox\ClickhouseBuilder\Query\Tuple; -use Bavix\LaravelClickHouse\Tests\Helpers; -use Tinderbox\ClickhouseBuilder\Query\Grammar; -use Tinderbox\ClickhouseBuilder\Query\Identifier; use Bavix\LaravelClickHouse\Database\Connection; -use Tinderbox\ClickhouseBuilder\Query\Enums\Operator; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Bavix\LaravelClickHouse\Database\Eloquent\Builder; use Bavix\LaravelClickHouse\Database\Eloquent\Collection; +use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; use Bavix\LaravelClickHouse\Tests\EloquentModelCastingTest; +use Bavix\LaravelClickHouse\Tests\Helpers; +use Illuminate\Database\DatabaseManager; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Mockery\Mock; +use PHPUnit\Framework\TestCase; +use Tinderbox\ClickhouseBuilder\Query\Enums\Operator; +use Tinderbox\ClickhouseBuilder\Query\Grammar; +use Tinderbox\ClickhouseBuilder\Query\Identifier; +use Tinderbox\ClickhouseBuilder\Query\Tuple; use Tinderbox\ClickhouseBuilder\Query\TwoElementsLogicExpression; -use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder; /** * @property Mock|Connection connection @@ -206,18 +206,18 @@ public function testFindOrFail(): void public function testGet(): void { $connectionResultRow = [ - 'id' => $this->faker()->randomDigit, - 'intAttribute' => (string) $this->faker()->randomDigit, - 'floatAttribute' => (string) $this->faker()->randomFloat(2), - 'stringAttribute' => $this->faker()->randomDigit, - 'boolAttribute' => 1, + 'id' => $this->faker()->randomDigit, + 'intAttribute' => (string) $this->faker()->randomDigit, + 'floatAttribute' => (string) $this->faker()->randomFloat(2), + 'stringAttribute' => $this->faker()->randomDigit, + 'boolAttribute' => 1, 'booleanAttribute' => 1, - 'objectAttribute' => json_encode([ + 'objectAttribute' => json_encode([ $this->faker()->word => $this->faker()->randomLetter, ]), - 'arrayAttribute' => json_encode(range(1, 5)), - 'dateAttribute' => now()->toDateTimeString(), - 'datetimeAttribute' => now()->toDateString(), + 'arrayAttribute' => json_encode(range(1, 5)), + 'dateAttribute' => now()->toDateTimeString(), + 'datetimeAttribute' => now()->toDateString(), 'timestampAttribute' => now()->toDateString(), ]; $connectionResultRow['jsonAttribute'] = json_encode($connectionResultRow['arrayAttribute']); diff --git a/tests/Unit/Database/Eloquent/CollectionTest.php b/tests/Unit/Database/Eloquent/CollectionTest.php index f8de44c..bdc4030 100644 --- a/tests/Unit/Database/Eloquent/CollectionTest.php +++ b/tests/Unit/Database/Eloquent/CollectionTest.php @@ -4,14 +4,14 @@ namespace Bavix\LaravelClickHouse\Tests\Unit\Database\Eloquent; -use Mockery\Mock; -use Carbon\Carbon; -use PHPUnit\Framework\TestCase; -use Illuminate\Database\DatabaseManager; -use Bavix\LaravelClickHouse\Tests\Helpers; use Bavix\LaravelClickHouse\Database\Connection; use Bavix\LaravelClickHouse\Database\Eloquent\Collection; use Bavix\LaravelClickHouse\Tests\EloquentModelCastingTest; +use Bavix\LaravelClickHouse\Tests\Helpers; +use Carbon\Carbon; +use Illuminate\Database\DatabaseManager; +use Mockery\Mock; +use PHPUnit\Framework\TestCase; /** * @property Mock|Connection connection @@ -84,7 +84,7 @@ public function testMapModelToArray(): void $collection = EloquentModelCastingTest::all() ->map(function (EloquentModelCastingTest $model) use ($now) { return [ - 'id' => $model->id, + 'id' => $model->id, 'datetimeAttribute' => $now, ]; }); @@ -101,6 +101,7 @@ public function testMapModelToArray(): void /** * @dataProvider findDataProvider + * * @param $key */ public function testFind($key): void @@ -126,6 +127,7 @@ public function testFind($key): void /** * @dataProvider containsDataProvider + * * @param bool $expected * @param $key * @param null $operator @@ -156,7 +158,7 @@ public function testGet(): void $connectionResult = collect() ->times(5, function (int $id) { return [ - 'id' => $id, + 'id' => $id, 'floatAttribute' => (string) $this->faker()->randomFloat(2), ]; }); @@ -190,7 +192,7 @@ public function findDataProvider(): array return [ [5], [ - tap(new EloquentModelCastingTest, function (EloquentModelCastingTest $model) { + tap(new EloquentModelCastingTest(), function (EloquentModelCastingTest $model) { $model->id = 5; }), ], diff --git a/tests/Unit/Database/Eloquent/ModelTest.php b/tests/Unit/Database/Eloquent/ModelTest.php index 6aa7d5b..2194663 100644 --- a/tests/Unit/Database/Eloquent/ModelTest.php +++ b/tests/Unit/Database/Eloquent/ModelTest.php @@ -4,13 +4,13 @@ namespace Bavix\LaravelClickHouse\Tests\Unit\Database\Eloquent; -use Illuminate\Support\Carbon; -use PHPUnit\Framework\TestCase; -use Bavix\LaravelClickHouse\Tests\Helpers; +use Bavix\LaravelClickHouse\Tests\EloquentModelCastingTest; use Bavix\LaravelClickHouse\Tests\EloquentModelTest; -use Illuminate\Database\Eloquent\MassAssignmentException; use Bavix\LaravelClickHouse\Tests\EloquentModelWithTest; -use Bavix\LaravelClickHouse\Tests\EloquentModelCastingTest; +use Bavix\LaravelClickHouse\Tests\Helpers; +use Illuminate\Database\Eloquent\MassAssignmentException; +use Illuminate\Support\Carbon; +use PHPUnit\Framework\TestCase; class ModelTest extends TestCase { diff --git a/tests/Unit/Database/Query/BuilderTest.php b/tests/Unit/Database/Query/BuilderTest.php index dbc149e..3b9e0e4 100644 --- a/tests/Unit/Database/Query/BuilderTest.php +++ b/tests/Unit/Database/Query/BuilderTest.php @@ -4,12 +4,12 @@ namespace Bavix\LaravelClickHouse\Tests\Unit\Database\Query; -use PHPUnit\Framework\TestCase; -use Bavix\LaravelClickHouse\Tests\Helpers; -use Tinderbox\ClickhouseBuilder\Query\Grammar; use Bavix\LaravelClickHouse\Database\Connection; -use Tinderbox\ClickhouseBuilder\Query\Enums\Format; use Bavix\LaravelClickHouse\Database\Query\Builder; +use Bavix\LaravelClickHouse\Tests\Helpers; +use PHPUnit\Framework\TestCase; +use Tinderbox\ClickhouseBuilder\Query\Enums\Format; +use Tinderbox\ClickhouseBuilder\Query\Grammar; /** * @property \Mockery\MockInterface|Connection connection @@ -91,8 +91,8 @@ public function testInsert(): void self::assertFalse($this->builder->insert([])); $insertedRow = [ - $this->faker()->word => $this->faker()->randomDigit, - $this->faker()->randomLetter => $this->faker()->randomDigit, + $this->faker()->word => $this->faker()->randomDigit, + $this->faker()->randomLetter => $this->faker()->randomDigit, $this->faker()->numerify('column_#') => $this->faker()->randomLetter, ]; @@ -124,8 +124,7 @@ public function testInsert(): void $this->connection ->shouldReceive('insert') ->withArgs([$generatedSql, $values]) - ->andReturn(true) - ; + ->andReturn(true); self::assertTrue($this->builder->insert($inserted)); }