Skip to content

Commit

Permalink
Fix types on Eloquent models
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Jan 2, 2024
1 parent 5442391 commit 1208746
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
22 changes: 12 additions & 10 deletions app/Models/ActivityLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace Convoy\Models;

use Carbon\Carbon;
use LogicException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use LogicException;

class ActivityLog extends Model
{
use HasFactory;

public $timestamps = false;

protected $guarded = [
Expand All @@ -27,7 +25,7 @@ class ActivityLog extends Model

protected $with = ['subjects'];

public static $validationRules = [
public static array $validationRules = [
'event' => ['required', 'string'],
'batch' => ['nullable', 'uuid'],
'ip' => ['required', 'string'],
Expand All @@ -45,7 +43,7 @@ public function actor(): MorphTo
return $morph;
}

public function subjects()
public function subjects(): HasMany
{
return $this->hasMany(ActivityLogSubject::class);
}
Expand All @@ -68,12 +66,16 @@ public function scopeForActor(Builder $builder, Model $actor): Builder
*
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
*/
public function prunable()
public function prunable(): ActivityLog
{
if (is_null(config('activity.prune_days'))) {
throw new LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
throw new LogicException(
'Cannot prune activity logs: no "prune_days" configuration value is set.',
);
}

return static::where('created_at', '<=', Carbon::now()->subDays(config('activity.prune_days')));
return static::where(
'created_at', '<=', Carbon::now()->subDays(config('activity.prune_days')),
);
}
}
9 changes: 4 additions & 5 deletions app/Models/ActivityLogSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace Convoy\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class ActivityLogSubject extends Model
{
use HasFactory;

public function activityLog()
public function activityLog(): BelongsTo
{
return $this->belongsTo(ActivityLog::class);
}

public function subject()
public function subject(): MorphTo
{
$morph = $this->morphTo();
if (method_exists($morph, 'withTrashed')) {
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Convoy\Casts\MebibytesToAndFromBytes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Backup extends Model
Expand All @@ -28,7 +29,7 @@ class Backup extends Model
'completed_at' => 'nullable|date',
];

public function server()
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
Expand Down
3 changes: 0 additions & 3 deletions app/Models/Coterm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class Coterm extends Model
{
public const COTERM_TOKEN_ID_LENGTH = 16;
public const COTERM_TOKEN_LENGTH = 64;

protected $guarded = [
'id',
'created_at',
Expand Down
5 changes: 3 additions & 2 deletions app/Models/ISO.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Convoy\Casts\MebibytesToAndFromBytes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class ISO extends Model
Expand All @@ -30,12 +31,12 @@ class ISO extends Model
'completed_at' => 'nullable|date',
];

public function node()
public function node(): BelongsTo
{
return $this->belongsTo(Node::class);
}

protected static function boot()
protected static function boot(): void
{
parent::boot();

Expand Down
2 changes: 0 additions & 2 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Location extends Model

/**
* Rules ensuring that the raw data stored in the database meets expectations.
*
* @var array
*/
public static array $validationRules = [
'short_code' => 'required|string|between:1,60|unique:locations,short_code',
Expand Down
3 changes: 0 additions & 3 deletions app/Models/SSHKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Convoy\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SSHKey extends Model
{
use HasFactory;

protected $table = 'ssh_keys';

protected $fillable = [
Expand Down
17 changes: 10 additions & 7 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Convoy\Enums\Server\Status;
use Convoy\Exceptions\Http\Server\ServerStatusConflictException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Server extends Model
Expand Down Expand Up @@ -42,33 +45,33 @@ class Server extends Model
'hydrated_at' => 'nullable|date',
];

public function node()
public function node(): BelongsTo
{
return $this->belongsTo(Node::class);
}

public function user()
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}

public function addresses()
public function addresses(): HasMany
{
return $this->hasMany(Address::class);
}

public function template()
public function template(): HasOne
{
return $this->hasOne(Template::class);
}

public function backups()
public function backups(): HasMany
{
return $this->hasMany(Backup::class);
}

/**
* Returns all of the activity log entries where the server is the subject.
* Returns all the activity log entries where the server is the subject.
*/
public function activity(): MorphToMany
{
Expand Down Expand Up @@ -97,7 +100,7 @@ public function isSuspended(): bool
*
* @throws ServerStatusConflictException
*/
public function validateCurrentState()
public function validateCurrentState(): void
{
if (
!is_null($this->status)
Expand Down
11 changes: 6 additions & 5 deletions app/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Convoy\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Ramsey\Uuid\Uuid;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class Template extends Model implements Sortable
{
use HasFactory, SortableTrait;
use SortableTrait;

public static array $validationRules = [
'template_group_id' => 'required|integer|exists:template_groups,id',
Expand All @@ -25,17 +26,17 @@ class Template extends Model implements Sortable
'updated_at',
];

public function group()
public function group(): BelongsTo
{
return $this->belongsTo(TemplateGroup::class, 'template_group_id');
}

public function buildSortQuery()
public function buildSortQuery(): Builder
{
return static::query()->where('template_group_id', $this->template_group_id);
}

protected static function boot()
protected static function boot(): void
{
parent::boot();

Expand Down
11 changes: 6 additions & 5 deletions app/Models/TemplateGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Convoy\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Ramsey\Uuid\Uuid;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class TemplateGroup extends Model implements Sortable
{
use HasFactory, SortableTrait;
use SortableTrait;

public static array $validationRules = [
'node_id' => 'required|integer|exists:nodes,id',
Expand All @@ -24,17 +25,17 @@ class TemplateGroup extends Model implements Sortable
'updated_at',
];

public function templates()
public function templates(): HasMany
{
return $this->hasMany(Template::class);
}

public function buildSortQuery()
public function buildSortQuery(): Builder
{
return static::query()->where('node_id', $this->node_id);
}

protected static function boot()
protected static function boot(): void
{
parent::boot();

Expand Down

0 comments on commit 1208746

Please sign in to comment.