meeting the requirements of a static analyzer

This commit is contained in:
Anatoliy Poloz
2024-01-15 17:11:55 +03:00
81 changed files with 1320 additions and 501 deletions

View File

@@ -8,13 +8,8 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Str;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* App\Models\Diet
@@ -29,7 +24,6 @@ use Spatie\Sluggable\SlugOptions;
* @property mixed $daily_schedule A list of planned foods
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $author
* @property-read int|null $author_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ChosenDiet> $choses
@@ -70,10 +64,9 @@ class Diet extends Model implements Viewable
{
use HasFactory, InteractsWithViews;
protected $withCount = [
'likes',
'comments',
'choses',
protected $casts = [
'type' => 'array',
'daily_schedule' => 'array',
];
private array $allowedDietTypes = [
@@ -83,13 +76,17 @@ class Diet extends Model implements Viewable
'veg',
];
// protected $casts = [
// 'type' => 'array',
// 'packet1_options' => 'array',
// 'packet2_options' => 'array',
// 'packet3_options' => 'array',
// protected $withCount = [
// 'likes',
// 'comments',
// 'choses',
// ];
public static function getWithLikesCommentsChosesCount(): Builder
{
return self::withCount(['likes', 'comments', 'choses']);
}
public function author(): BelongsToMany
{
return $this
@@ -113,7 +110,7 @@ class Diet extends Model implements Viewable
return $this->hasMany(DietLike::class);
}
public function isLiked(): bool
public function isLiked(): bool|int
{
if (auth()->user()) {
return auth()->user()->diet_likes()->forDiet($this)->count() > 0;
@@ -122,7 +119,7 @@ class Diet extends Model implements Viewable
return false;
}
public function removeLike(): bool
public function removeLike(): bool|int
{
if (auth()->user()) {
return auth()->user()->diet_likes()->forDiet($this)->delete();
@@ -133,7 +130,7 @@ class Diet extends Model implements Viewable
public function types(): Attribute
{
$data = $this->unserializeOptionsArr($this->type);
$data = $this->type;
return new Attribute(
get: fn () => array_keys($data),
@@ -142,7 +139,7 @@ class Diet extends Model implements Viewable
public function schedules(): Attribute
{
$data = $this->unserializeOptionsArr($this->daily_schedule);
$data = $this->daily_schedule;
return new Attribute(
get: fn () => $data,
@@ -154,22 +151,22 @@ class Diet extends Model implements Viewable
return $this->hasMany(ChosenDiet::class);
}
public function scopeDurationMin(Builder $query, int $days): Builder
public function scopeDurationMin(Builder $query, string $days): Builder
{
return $query->where('duration', '>=', $days);
}
public function scopeDurationMax(Builder $query, int $days): Builder
public function scopeDurationMax(Builder $query, string $days): Builder
{
return $query->where('duration', '<=', $days);
}
public function scopeInvolvedMin(Builder $query, int $number): Builder
public function scopeInvolvedMin(Builder $query, string $number): Builder
{
return $query->having('choses_count', '>=', $number);
}
public function scopeInvolvedMax(Builder $query, int $number): Builder
public function scopeInvolvedMax(Builder $query, string $number): Builder
{
return $query->having('choses_count', '<=', $number);
}
@@ -189,13 +186,4 @@ class Diet extends Model implements Viewable
return $query;
}
private function unserializeOptionsArr(string $str): array
{
if (preg_match('/^".*"$/', $str)) {
return json_decode(stripslashes(mb_substr($str, 1, -1)), true);
}
return json_decode(stripslashes($str), true);
}
}