Skip to content

Commit

Permalink
init fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 1, 2024
1 parent 20c4ea8 commit e802f6c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
5 changes: 1 addition & 4 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

class Connection extends \Tinderbox\ClickhouseBuilder\Integrations\Laravel\Connection
{
/**
* @return Builder|\Tinderbox\ClickhouseBuilder\Integrations\Laravel\Builder
*/
public function query()
public function query(): Builder

Check failure on line 13 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / phpstan

Return type (Bavix\LaravelClickHouse\Database\Query\Builder) of method Bavix\LaravelClickHouse\Database\Connection::query() should be compatible with return type (Tinderbox\ClickhouseBuilder\Integrations\Laravel\Builder) of method Tinderbox\ClickhouseBuilder\Integrations\Laravel\Connection::query()
{
return new Builder($this, new Grammar());
}
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ public function newModelInstance($attributes = [])
->setConnection($this->query->getConnection()->getName());
}

public function create ($attributes = [])

Check failure on line 655 in src/Database/Eloquent/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Builder::create() has no return type specified.

Check failure on line 655 in src/Database/Eloquent/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Builder::create() has parameter $attributes with no type specified.
{
return tap($this->newModelInstance($attributes), static function ($instance) {
$instance->save();
});
}

public function getQuery(): QueryBuilder
{
return $this->query;
Expand Down
6 changes: 2 additions & 4 deletions src/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ trait HasAttributes

public function getDates(): array
{
return property_exists($this, 'dates') && is_array($this->dates)
? $this->dates
: [];
return $this->dates ?? [];
}

public function getCasts(): array
{
return $this->casts;
return $this->casts ?? [];
}

protected function getDateFormat(): string
Expand Down
92 changes: 72 additions & 20 deletions src/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use ArrayAccess;
use Bavix\LaravelClickHouse\Database\Connection;
use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Concerns\GuardsAttributes;
Expand All @@ -17,11 +20,14 @@
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use JsonSerializable;
use Tinderbox\ClickhouseBuilder\Query\Grammar;

abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
abstract class Model implements ArrayAccess, UrlRoutable, Arrayable, Jsonable, JsonSerializable
{
use Concerns\HasAttributes;
use Concerns\Common;
Expand All @@ -35,7 +41,7 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*
* @var bool
*/
public $exists = false;
public bool $exists = false;

/**
* Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.
Expand Down Expand Up @@ -93,19 +99,9 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*/
protected $perPage = 15;

/**
* The connection resolver instance.
*
* @var Resolver
*/
protected static $resolver;
protected static ConnectionResolverInterface $resolver;

/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected static $dispatcher;
protected static Dispatcher $dispatcher;

/**
* The array of booted models.
Expand All @@ -114,6 +110,8 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*/
protected static $booted = [];

public bool $wasRecentlyCreated = false;

/**
* Create a new Eloquent model instance.
*/
Expand Down Expand Up @@ -381,7 +379,7 @@ public function jsonSerialize(): array
/**
* Get the database connection for the model.
*/
public function getConnection(): \Illuminate\Database\Connection
public function getConnection(): ConnectionInterface
{
return static::resolveConnection($this->getConnectionName());
}
Expand All @@ -408,10 +406,8 @@ public function setConnection(string $name)

/**
* Resolve a connection instance.
*
* @return Connection|\Illuminate\Database\ConnectionInterface
*/
public static function resolveConnection(string $connection = null)
public static function resolveConnection(string $connection = null): ConnectionInterface
{
return static::getConnectionResolver()->connection($connection);
}
Expand Down Expand Up @@ -456,6 +452,52 @@ public function setTable(string $table): self
return $this;
}

public function resolveRouteBinding($value, $field = null): ?Model

Check failure on line 455 in src/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Model::resolveRouteBinding() is not final, but since the containing class is abstract, it should be.
{
return $this->resolveRouteBindingQuery($this, $value, $field)->first();

Check failure on line 457 in src/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Model::resolveRouteBinding() should return Bavix\LaravelClickHouse\Database\Eloquent\Model|null but returns object|null.
}

public function resolveSoftDeletableRouteBinding(mixed $value, ?string $field = null): ?Model

Check failure on line 460 in src/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Model::resolveSoftDeletableRouteBinding() is not final, but since the containing class is abstract, it should be.
{
return $this->resolveRouteBindingQuery($this, $value, $field)->withTrashed()->first();

Check failure on line 462 in src/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Bavix\LaravelClickHouse\Database\Eloquent\Builder::withTrashed().
}

public function resolveChildRouteBinding($childType, $value, $field): ?Model

Check failure on line 465 in src/Database/Eloquent/Model.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\LaravelClickHouse\Database\Eloquent\Model::resolveChildRouteBinding() is not final, but since the containing class is abstract, it should be.
{
return $this->resolveChildRouteBindingQuery($childType, $value, $field)->first();
}

public function resolveSoftDeletableChildRouteBinding(string $childType, mixed $value, ?string $field = null): ?Model
{
return $this->resolveChildRouteBindingQuery($childType, $value, $field)->withTrashed()->first();
}

protected function resolveChildRouteBindingQuery(string $childType, mixed $value, ?string $field = null): Relation|Model
{
$relationship = $this->{$this->childRouteBindingRelationshipName($childType)}();

$field = $field ?: $relationship->getRelated()->getRouteKeyName();

if ($relationship instanceof HasManyThrough ||
$relationship instanceof BelongsToMany) {
$field = $relationship->getRelated()->getTable().'.'.$field;
}

return $relationship instanceof Model
? $relationship->resolveRouteBindingQuery($relationship, $value, $field)
: $relationship->getRelated()->resolveRouteBindingQuery($relationship, $value, $field);
}

protected function childRouteBindingRelationshipName(string $childType): string
{
return Str::plural(Str::camel($childType));
}

public function resolveRouteBindingQuery(Relation|Model $query, mixed $value, ?string $field = null): Builder
{
return $query->where($field ?? $this->getRouteKeyName(), $value);
}

/**
* Get the primary key for the model.
*/
Expand Down Expand Up @@ -504,6 +546,16 @@ public function setKeyType(string $type)
return $this;
}

public function getRouteKey(): mixed
{
return $this->getAttribute($this->getRouteKeyName());
}

public function getRouteKeyName(): string
{
return $this->getKeyName();
}

/**
* Get the value of the model's primary key.
*
Expand Down Expand Up @@ -533,9 +585,9 @@ public function getPerPage(): int
/**
* Set the number of models to return per page.
*
* @return $this
* @return static
*/
public function setPerPage(int $perPage): self
public function setPerPage(int $perPage): static
{
$this->perPage = $perPage;

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Builder extends BaseBuilder
__call as macroCall;
}

protected $connection;
protected Connection $connection;

public function __construct(
Connection $connection,
Expand Down

0 comments on commit e802f6c

Please sign in to comment.