refactoring static analyse code

This commit is contained in:
2024-01-09 10:46:41 +03:00
parent a93719a99a
commit 590d3b987d
39 changed files with 293 additions and 280 deletions

View File

@@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Laracasts\Presenter\PresentableTrait;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* App\Models\Plan
@@ -86,35 +87,27 @@ class Plan extends Model implements Viewable
'packet3_options' => 'array',
];
protected $presenter = PlanPresenter::class;
protected string $presenter = PlanPresenter::class;
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom(['name'])
->saveSlugsTo('slug')
// ->slugsShouldBeNoLongerThan(20)
->usingSeparator('_');
// ->usingLanguage('ru')
// ->doNotGenerateSlugsOnUpdate();
}
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
public function getRouteKeyName(): string
{
return 'slug';
}
public function author()
public function author(): BelongsToMany
{
return $this->belongsToMany(User::class, 'plan_author', 'plan_id', 'user_id');
}
public function comments()
public function comments(): HasMany
{
return $this->hasMany(PlanComment::class);
}
@@ -124,7 +117,7 @@ class Plan extends Model implements Viewable
return $this->hasMany(PlanLike::class);
}
public function isLiked(): bool
public function isLiked(): bool|int
{
if (auth()->user()) {
return auth()->user()->plan_likes()->forPlan($this)->count();
@@ -139,7 +132,6 @@ class Plan extends Model implements Viewable
for ($i = 1; $i <= $this->packets_count; $i++) {
$key = 'packet'.$i.'_options';
// $data = $this->unserializeOptionsObj($this->$key);
$data = $this->$key;
if (! is_null($data)) {
$result = array_merge($result, data_get($data, 'items', []));
@@ -151,7 +143,6 @@ class Plan extends Model implements Viewable
public function types(): Attribute
{
// $data = $this->unserializeOptionsArr($this->type);
$data = $this->type;
return new Attribute(
@@ -164,8 +155,6 @@ class Plan extends Model implements Viewable
$result = 0;
for ($i = 1; $i <= $this->packets_count; $i++) {
$key = 'packet'.$i.'_options';
// dd($this->$key);
// $data = $this->unserializeOptionsObj($this->$key);
$data = $this->$key;
if (! is_null($data)) {
$curPrice = (int) data_get($data, 'price', 0);
@@ -187,31 +176,13 @@ class Plan extends Model implements Viewable
return false;
}
public function scopeNegativeComments(Builder $query)
public function scopeNegativeComments(Builder $query): HasMany
{
return static::comments()->where('grade', '<=', '3');
return static::comments()->where('grade', '<=', '3');
}
public function scopePositiveComments(Builder $query)
public function scopePositiveComments(Builder $query): HasMany
{
return static::comments()->where('grade', '>', '3');
}
private function unserializeOptionsObj($str)
{
if (preg_match('/^".*"$/', $str)) {
return json_decode(stripslashes(mb_substr($str, 1, -1)));
}
return json_decode(stripslashes($str));
}
private function unserializeOptionsArr($str)
{
if (preg_match('/^".*"$/', $str)) {
return json_decode(stripslashes(mb_substr($str, 1, -1)), true);
}
return json_decode(stripslashes($str), true);
}
}