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

@@ -4,6 +4,7 @@ namespace App\Livewire\Diet;
use App\Models\Diet;
use Livewire\Component;
use Illuminate\View\View;
class LikeCount extends Component
{
@@ -11,12 +12,12 @@ class LikeCount extends Component
protected $listeners = ['liked' => '$refresh'];
public function mount(Diet $diet)
public function mount(Diet $diet): void
{
$this->diet = $diet;
}
public function render()
public function render(): View
{
return view(
'livewire.diet.like-count',

View File

@@ -4,18 +4,19 @@ namespace App\Livewire\Plan;
use App\Models\Plan;
use Livewire\Component;
use Illuminate\View\View;
class Accordion extends Component
{
public Plan $plan;
public $accordionState = ['options1' => true, 'options2' => false, 'options3' => false];
public array $accordionState = ['options1' => true, 'options2' => false, 'options3' => false];
public $options1;
public array $options1;
public $options2;
public array $options2;
public $options3;
public array $options3;
public function mount(Plan $plan): void
{
@@ -38,12 +39,12 @@ class Accordion extends Component
$this->options3['description'] = $plan->packet3_options['description'] ?? '';
}
public function render()
public function render(): View
{
return view('livewire.plan.accordion');
}
public function toggleAccordionState($optionName)
public function toggleAccordionState(string $optionName): void
{
$this->accordionState[$optionName] = ! $this->accordionState[$optionName];

View File

@@ -6,6 +6,7 @@ use App\Models\Plan;
use App\Models\PlanComment;
use App\Models\User;
use Livewire\Component;
use Illuminate\View\View;
class Create extends Component
{
@@ -13,23 +14,23 @@ class Create extends Component
public User $user;
public $body = '';
public string $body = '';
public $grade;
public string|int $grade = 0;
public $isChecked = false;
public bool|string $isChecked = false;
public function render()
public function render(): View
{
return view('livewire.plan.comment.create');
}
public function toggle()
public function toggle(): void
{
$this->isChecked = ! $this->isChecked;
}
public function save()
public function save(): void
{
$this->validate([
'body' => 'required|min:3',

View File

@@ -4,6 +4,7 @@ namespace App\Livewire\Plan\Comment;
use App\Models\Plan;
use Livewire\Component;
use Illuminate\View\View;
class Show extends Component
{
@@ -13,13 +14,16 @@ class Show extends Component
protected $listeners = ['commentAdded' => '$refresh'];
public function render()
public function render(): View
{
return view('livewire.plan.comment.show');
}
public function toggleTabpanelState($tabpanelName)
public function toggleTabpanelState(string $tabpanelName): void
{
array_map(fn ($key) => $key === $tabpanelName ? $this->tabpanelState[$tabpanelName] = $tabpanelName : $this->tabpanelState[$key] = false, array_keys($this->tabpanelState));
collect($this->tabpanelState)->keys()->map(fn ($key) => $key === $tabpanelName
? $this->tabpanelState[$tabpanelName] = $tabpanelName
: $this->tabpanelState[$key] = false, array_keys($this->tabpanelState));
// array_map(fn ($key) => $key === $tabpanelName ? $this->tabpanelState[$tabpanelName] = $tabpanelName : $this->tabpanelState[$key] = false, array_keys($this->tabpanelState));
}
}

View File

@@ -4,17 +4,18 @@ namespace App\Livewire\Plan\Comment;
use App\Models\PlanComment;
use Livewire\Component;
use Illuminate\View\View;
class Upvote extends Component
{
public PlanComment $comment;
public function mount(PlanComment $comment)
public function mount(PlanComment $comment): void
{
$this->comment = $comment;
}
public function render()
public function render(): View
{
$upvotes = $this->comment->upvotes()->forPlanCommentsUpVote($this->comment)->count();
$downvotes = $this->comment->upvotes()->forPlanCommentsDownVote($this->comment)->count();
@@ -30,7 +31,7 @@ class Upvote extends Component
);
}
public function upvote()
public function upvote(): void
{
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
@@ -49,7 +50,7 @@ class Upvote extends Component
]);
}
public function downvote()
public function downvote(): void
{
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();

View File

@@ -7,75 +7,77 @@ use Illuminate\Support\Facades\Auth;
use Livewire\Component;
/* use Tonysm\RichTextLaravel\Livewire\WithRichTexts; */
use Livewire\WithFileUploads;
use Illuminate\View\View;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
class Create extends Component
{
use WithFileUploads;
public $name = '';
public string $name = '';
public $image;
public $duration = '7';
public int|string $duration = 7;
public $state = 'draft';
public string $state = 'draft';
public $type = ['fit' => false, 'slim' => false, 'behavior' => false, 'wellness' => false];
public array $type = ['fit' => false, 'slim' => false, 'behavior' => false, 'wellness' => false];
public $details = '';
public string $details = '';
public $packets_count = 1;
public int $packets_count = 1;
public $packet1_options = [];
public array $packet1_options = [];
public $packet2_options = [];
public array $packet2_options = [];
public $packet3_options = [];
public array $packet3_options = [];
public $packet1_options_input = [];
public array $packet1_options_input = [];
public $packet2_options_input = [];
public array $packet2_options_input = [];
public $packet3_options_input = [];
public array $packet3_options_input = [];
public $input_items_1 = '';
public string $input_items_1 = '';
public $input_items_2 = '';
public string $input_items_2 = '';
public $input_items_3 = '';
public string $input_items_3 = '';
public $isVisibleDuration = false;
public bool $isVisibleDuration = false;
public $isVisibleState = false;
public bool $isVisibleState = false;
public function render()
public function render(): View
{
return view('livewire.plan.create');
}
public function showListDuration()
public function showListDuration(): void
{
$this->isVisibleDuration = ! $this->isVisibleDuration;
}
public function showListState()
public function showListState(): void
{
$this->isVisibleState = ! $this->isVisibleState;
}
public function toggleDuration($duration)
public function toggleDuration(string $duration): void
{
$this->duration = $duration;
$this->isVisibleDuration = false;
}
public function toggleState($state)
public function toggleState(string $state): void
{
$this->state = $state;
$this->isVisibleState = false;
}
public function addPacketOption($options_type, $options_type_input)
public function addPacketOption(array $options_type, array $options_type_input): void
{
$this->$options_type['short'] = $this->$options_type_input['short'];
$this->$options_type['price'] = $this->$options_type_input['price'];
@@ -91,7 +93,7 @@ class Create extends Component
$this->reset('input_items_3');
}
public function publish()
public function publish(): void
{
$this->validate();
@@ -116,19 +118,19 @@ class Create extends Component
$this->redirectRoute('plan.show', $plan);
}
public function addToDrafts()
public function addToDrafts(): void
{
$this->state = 'draft';
$this->publish();
}
public function showPreview()
public function showPreview(): void
{
$this->state = 'unactive';
$this->publish();
}
protected function rules()
protected function rules(): array
{
$rules = [
'name' => 'required|min:3',

View File

@@ -5,6 +5,7 @@ namespace App\Livewire\Plan;
use App\Models\Plan;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\View\View;
class Edit extends Component
{
@@ -12,43 +13,43 @@ class Edit extends Component
public Plan $plan;
public $name = '';
public string $name = '';
public $image;
public $duration = '7';
public string|int $duration = '7';
public $state = 'draft';
public string $state = 'draft';
public $type = ['fit' => false, 'slim' => false, 'behavior' => false, 'wellness' => false];
public array $type = ['fit' => false, 'slim' => false, 'behavior' => false, 'wellness' => false];
public $details = '';
public string $details = '';
public $packets_count = 1;
public int $packets_count = 1;
public $packet1_options = [];
public array $packet1_options = [];
public $packet2_options = [];
public array|string $packet2_options = [];
public $packet3_options = [];
public array|string $packet3_options = [];
public $packet1_options_input = [];
public array $packet1_options_input = [];
public $packet2_options_input = [];
public array $packet2_options_input = [];
public $packet3_options_input = [];
public array $packet3_options_input = [];
public $input_items_1 = '';
public string $input_items_1 = '';
public $input_items_2 = '';
public string $input_items_2 = '';
public $input_items_3 = '';
public string $input_items_3 = '';
public $isVisibleDuration = false;
public bool $isVisibleDuration = false;
public $isVisibleState = false;
public bool $isVisibleState = false;
public function mount(Plan $plan)
public function mount(Plan $plan): void
{
$this->plan = $plan;
@@ -96,29 +97,29 @@ class Edit extends Component
// commentsCount
}
public function showListDuration()
public function showListDuration(): void
{
$this->isVisibleDuration = ! $this->isVisibleDuration;
}
public function showListState()
public function showListState(): void
{
$this->isVisibleState = ! $this->isVisibleState;
}
public function toggleDuration($duration)
public function toggleDuration(string $duration): void
{
$this->duration = $duration;
$this->isVisibleDuration = false;
}
public function toggleState($state)
public function toggleState(string $state): void
{
$this->state = $state;
$this->isVisibleState = false;
}
public function addPacketOption($options_type, $options_type_input)
public function addPacketOption(array $options_type, array $options_type_input): void
{
$this->$options_type['short'] = $this->$options_type_input['short'];
$this->$options_type['price'] = $this->$options_type_input['price'];
@@ -135,7 +136,7 @@ class Edit extends Component
$this->reset('input_items_3');
}
public function publish()
public function publish(): void
{
$this->validate();
@@ -158,24 +159,24 @@ class Edit extends Component
$this->redirectRoute('plan.show', $this->plan);
}
public function addToDrafts()
public function addToDrafts(): void
{
$this->state = 'draft';
$this->publish();
}
public function showPreview()
public function showPreview(): void
{
$this->state = 'unactive';
$this->publish();
}
public function render()
public function render(): View
{
return view('livewire.plan.edit');
}
protected function rules()
protected function rules(): array
{
$rules = [
'name' => 'required|min:3',

View File

@@ -5,17 +5,18 @@ namespace App\Livewire\Plan\Like;
use App\Models\Plan;
use Livewire\Attributes\On;
use Livewire\Component;
use Illuminate\View\View;
class AnnouncementDetails extends Component
{
public Plan $plan;
public function mount(Plan $plan)
public function mount(Plan $plan): void
{
$this->plan = $plan;
}
public function render()
public function render(): View
{
return view('livewire.plan.like.announcement-details');
}

View File

@@ -5,17 +5,18 @@ namespace App\Livewire\Plan\Like;
use App\Models\Plan;
use Livewire\Attributes\On;
use Livewire\Component;
use Illuminate\View\View;
class AnnouncementFooter extends Component
{
public Plan $plan;
public function mount(Plan $plan)
public function mount(Plan $plan): void
{
$this->plan = $plan;
}
public function render()
public function render(): View
{
return view('livewire.plan.like.announcement-footer');
}

View File

@@ -4,6 +4,7 @@ namespace App\Livewire\Plan;
use App\Models\Plan;
use Livewire\Component;
use Illuminate\View\View;
class LikeCount extends Component
{
@@ -11,12 +12,12 @@ class LikeCount extends Component
protected $listeners = ['liked-details' => '$refresh', 'liked-footer' => '$refresh'];
public function mount(Plan $plan)
public function mount(Plan $plan): void
{
$this->plan = $plan;
}
public function render()
public function render(): View
{
$this->dispatch('liked-details');
$this->dispatch('liked-footer');

View File

@@ -6,6 +6,7 @@ use App\Models\Recipe;
use App\Models\RecipeComment;
use App\Models\User;
use Livewire\Component;
use Illuminate\View\View;
class Create extends Component
{
@@ -13,23 +14,23 @@ class Create extends Component
public User $user;
public $body = '';
public string $body = '';
public $grade = 0;
public string|int $grade = 0;
public $isChecked = false;
public bool|string $isChecked = false;
public function render()
public function render(): View
{
return view('livewire.recipe.comment.create');
}
public function toggle()
public function toggle(): void
{
$this->isChecked = ! $this->isChecked;
}
public function save()
public function save(): void
{
$this->validate([
'body' => 'required|min:3',

View File

@@ -4,6 +4,7 @@ namespace App\Livewire\Recipe\Comment;
use App\Models\Recipe;
use Livewire\Component;
use Illuminate\View\View;
class Show extends Component
{
@@ -13,12 +14,12 @@ class Show extends Component
protected $listeners = ['commentAdded' => '$refresh'];
public function render()
public function render(): View
{
return view('livewire.recipe.comment.show');
}
public function toggleTabpanelState($tabpanelName)
public function toggleTabpanelState(string $tabpanelName): void
{
collect($this->tabpanelState)->keys()->map(fn ($key) => $key === $tabpanelName
? $this->tabpanelState[$tabpanelName] = $tabpanelName

View File

@@ -4,17 +4,18 @@ namespace App\Livewire\Recipe\Comment;
use App\Models\RecipeComment;
use Livewire\Component;
use Illuminate\View\View;
class Upvote extends Component
{
public RecipeComment $comment;
public function mount(RecipeComment $comment)
public function mount(RecipeComment $comment): void
{
$this->comment = $comment;
}
public function render()
public function render(): View
{
$upvotes = $this->comment->upvotes()->forRecipeCommentsUpVote($this->comment)->count();
$downvotes = $this->comment->upvotes()->forRecipeCommentsDownVote($this->comment)->count();
@@ -30,7 +31,7 @@ class Upvote extends Component
);
}
public function upvote()
public function upvote(): void
{
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
@@ -49,7 +50,7 @@ class Upvote extends Component
]);
}
public function downvote()
public function downvote(): void
{
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();

View File

@@ -8,6 +8,9 @@ use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\View\View;
use Illuminate\Support\Collection;
class Create extends Component
{
@@ -15,43 +18,45 @@ class Create extends Component
public $image;
public $state = 'active';
public string $state = 'active';
public $state_ingredient = 'active';
public string $state_ingredient = 'active';
public $state_article = 'active';
public string $state_article = 'active';
public $tags = [];
public array $tags = [];
public $title;
public string $title;
public $type = 'other';
public string $type = 'other';
public $video_url = '';
public string $video_url = '';
public $cooking_time = '10';
public int|string $cooking_time = '10';
public $description = '';
public string $description = '';
public $steps = [['image' => '', 'description' => '']];
public Collection|array $steps = [['image' => '', 'description' => '']];
public $ingredients = [];
public Collection|array $ingredients = [];
public $ingredients_name = '';
public string $ingredients_name = '';
public $ingredients_portion = '';
public string $ingredients_portion = '';
public $ingredients_unit = 'gr';
public string $ingredients_unit = 'gr';
public $state_row = ['ingredient' => 'true', 'article' => 'false'];
public array $state_row = ['ingredient' => 'true', 'article' => 'false'];
protected $listeners = ['stepRemoved' => '$refresh'];
public function toggleStateRow(string $currentRow): void
{
collect($this->state_row)->keys()->map(fn ($key) => $key === $currentRow
collect($this->state_row)->keys()->map(
fn ($key) => $key === $currentRow
? $this->state_row[$currentRow] = 'true'
: $this->state_row[$key] = 'false');
: $this->state_row[$key] = 'false'
);
}
public function addIngredient(): void
@@ -64,19 +69,19 @@ class Create extends Component
$this->reset('ingredients_name', 'ingredients_portion', 'ingredients_unit');
}
public function removeIngredient($i)
public function removeIngredient(int $i): void
{
$newIngredients = collect($this->ingredients)
->forget($i);
$this->ingredients = $newIngredients->toArray();
}
public function addStep()
public function addStep(): void
{
$this->steps[] = ['image' => '', 'description' => ''];
}
public function removeStep($i)
public function removeStep(int $i): void
{
$searchKey = $i - 1;
$newSteps = array_filter(
@@ -92,27 +97,27 @@ class Create extends Component
}
#[On('parentComponentTagAdded')]
public function parentComponentTagAdded(Tag $tag)
public function parentComponentTagAdded(Tag $tag): void
{
$tagIndex = $tag->id;
$this->tags[$tagIndex] = $tag;
}
#[On('parentComponentTagRemoved')]
public function parentComponentTagRemoved($tag)
public function parentComponentTagRemoved(Tag $tag): void
{
$this->tags = collect($this->tags)
->filter(fn ($curTag) => $curTag['name'] !== $tag)
->all();
}
public function draft()
public function draft(): void
{
$this->state = 'archived';
$this->publish();
}
public function preview()
public function preview(): void
{
$this->validate();
@@ -120,11 +125,12 @@ class Create extends Component
$this->publish();
}
public function publish()
public function publish(): void
{
$this->validate();
$recipe = new Recipe();
// $recipe->image = $this->image->store('recipe', 'public');
$recipe->image = $this->image->store('recipe');
$recipe->state = $this->state;
$recipe->type = $this->type;
@@ -145,12 +151,12 @@ class Create extends Component
$this->redirectRoute('recipe.show', $recipe);
}
public function render()
public function render(): View
{
return view('livewire.recipe.create');
}
protected function rules()
protected function rules(): array
{
return [
'image' => 'required|image|max:2048',
@@ -162,7 +168,7 @@ class Create extends Component
];
}
private function transformSteps($steps)
private function transformSteps(array $steps): array
{
return collect($steps)->map(
function ($step) {

View File

@@ -6,6 +6,9 @@ use App\Models\Recipe;
use App\Models\Tag;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\View\View;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Support\Collection;
class Edit extends Component
{
@@ -15,41 +18,40 @@ class Edit extends Component
public $image;
public $state = 'active';
public string $state = 'active';
public $state_ingredient = 'active';
public string $state_ingredient = 'active';
public $state_article = 'active';
public string $state_article = 'active';
public $type = 'other';
public string $type = 'other';
public $tags;
public Collection $tags;
public $title;
public string $title;
public $video_url;
public string $video_url;
public $cooking_time = '10';
public string|int $cooking_time = '10';
public $description = '';
public string $description = '';
public $steps = [['image' => '', 'description' => '']];
public AsCollection|array|Collection $steps = [['image' => '', 'description' => '']];
public $ingredients = [['name' => '', 'portion' => '']];
public AsCollection|array|Collection $ingredients = [['name' => '', 'portion' => '']];
public $ingredients_name = '';
public string $ingredients_name = '';
public $ingredients_portion = '';
public string $ingredients_portion = '';
public $ingredients_unit = 'gr';
public string $ingredients_unit = 'gr';
public $state_row = ['ingredient' => 'true', 'article' => 'false'];
public array $state_row = ['ingredient' => 'true', 'article' => 'false'];
protected $listeners = ['stepRemoved' => '$refresh', 'parentComponentTagAdded', 'parentComponentTagRemoved'];
public function mount(Recipe $recipe)
public function mount(Recipe $recipe): void
{
$this->name = $recipe->name;
$this->image = $recipe->image;
$this->state = $recipe->state;
$this->type = $recipe->type;
@@ -69,7 +71,7 @@ class Edit extends Component
: $this->state_row[$key] = 'false');
}
public function addIngredient()
public function addIngredient(): void
{
$this->ingredients[] = [
'name' => $this->ingredients_name,
@@ -79,19 +81,19 @@ class Edit extends Component
$this->reset('ingredients_name', 'ingredients_portion', 'ingredients_unit');
}
public function removeIngredient($i)
public function removeIngredient(int $i): void
{
$newIngredients = collect($this->ingredients)
->forget($i);
$this->ingredients = $newIngredients->toArray();
}
public function addStep()
public function addStep(): void
{
$this->steps[] = ['image' => '', 'description' => ''];
}
public function removeStep($i)
public function removeStep(int $i): void
{
if (is_array($this->steps)) {
$steps = $this->steps;
@@ -111,30 +113,30 @@ class Edit extends Component
$this->dispatch('stepRemoved')->self();
}
public function parentComponentTagAdded(Tag $tag)
public function parentComponentTagAdded(Tag $tag): void
{
$this->recipe->tags()->attach($tag['id']);
}
public function parentComponentTagRemoved($tag)
public function parentComponentTagRemoved(Tag $tag): void
{
$selectedTags = Tag::where('name', $tag)->pluck('id');
$this->recipe->tags()->detach($selectedTags);
}
public function draft()
public function draft(): void
{
$this->state = 'archived';
$this->publish();
}
public function preview()
public function preview(): void
{
$this->state = 'archived';
$this->publish();
}
public function publish()
public function publish(): void
{
$this->validate();
@@ -157,12 +159,12 @@ class Edit extends Component
$this->redirectRoute('recipe.show', $recipe);
}
public function render()
public function render(): View
{
return view('livewire.recipe.edit');
}
protected function rules()
protected function rules(): array
{
return [
'image' => 'filled',
@@ -174,7 +176,7 @@ class Edit extends Component
];
}
private function transformSteps($steps)
private function transformSteps(Collection $steps): array
{
return collect($steps)->
map(function ($step) {

View File

@@ -4,19 +4,20 @@ namespace App\Livewire\Recipe;
use App\Models\Recipe;
use Livewire\Component;
use Illuminate\View\View;
class Like extends Component
{
public Recipe $recipe;
public $isIconOnly = false;
public bool $isIconOnly = false;
public function mount(Recipe $recipe)
public function mount(Recipe $recipe): void
{
$this->recipe = $recipe;
}
public function render()
public function render(): View
{
return view('livewire.recipe.like');
}

View File

@@ -4,6 +4,7 @@ namespace App\Livewire\Recipe;
use App\Models\Recipe;
use Livewire\Component;
use Illuminate\View\View;
class LikeCount extends Component
{
@@ -11,12 +12,12 @@ class LikeCount extends Component
protected $listeners = ['liked' => '$refresh'];
public function mount(Recipe $recipe)
public function mount(Recipe $recipe): void
{
$this->recipe = $recipe;
}
public function render()
public function render(): View
{
return view(
'livewire.recipe.like-count',

View File

@@ -8,27 +8,28 @@ use App\Models\User;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\View\View;
class ListView extends Component
{
use WithPagination;
public $search = '';
public string $search = '';
public $typeFilter = 'all';
public string $typeFilter = 'all';
public $tag = '';
public string $tag = '';
public $author;
protected $queryString = [
protected array $queryString = [
'search' => ['except' => ''],
'typeFilter' => ['except' => 'all'],
'tag' => ['except' => ''],
'author' => ['except' => ''],
];
public function render()
public function render(): View
{
if ($this->author) {
$author = $this->author;
@@ -60,25 +61,27 @@ class ListView extends Component
);
}
private function getTagsCollection()
private function getTagsCollection(): array
{
return Recipe::all()->map(
fn ($r) => [
$r->type,
$r->tags->map(fn ($t) => $t->id.'-'.$t->name)->all(),
]
)->reduce(function ($acc, $item) {
[$type, $tagNames] = $item;
)->reduce(
function ($acc, $item) {
[$type, $tagNames] = $item;
foreach ($tagNames as $name) {
if (isset($acc[$type][$name])) {
$acc[$type][$name] += 1;
} else {
$acc[$type][$name] = 1;
foreach ($tagNames as $name) {
if (isset($acc[$type][$name])) {
$acc[$type][$name] += 1;
} else {
$acc[$type][$name] = 1;
}
}
}
return $acc;
});
return $acc;
}
);
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\ChosenPlan
@@ -35,7 +36,7 @@ class ChosenPlan extends Model
{
use HasFactory;
public function user()
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

View File

@@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;
/**
* App\Models\CommentUpvote
@@ -42,22 +44,22 @@ class CommentUpvote extends Model
'type',
];
public function plan_comment()
public function plan_comment(): BelongsTo
{
return $this->belongsTo(PlanComment::class, 'comment_id');
}
public function author()
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function scopeForPlanCommentsUpVote($query, PlanComment $comment)
public function scopeForPlanCommentsUpVote(Builder $query, PlanComment $comment): Builder
{
return $query->where('comment_id', $comment->id)->where('type', 'upvote');
}
public function scopeForPlanCommentsDownVote($query, PlanComment $comment)
public function scopeForPlanCommentsDownVote(Builder $query, PlanComment $comment): Builder
{
return $query->where('comment_id', $comment->id)->where('type', 'downvote');
}

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);
}
}

View File

@@ -5,6 +5,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\User;
/**
* App\Models\PlanComment
@@ -40,12 +42,12 @@ class PlanComment extends Model
{
use HasFactory;
public function author()
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
return $this->belongsTo(User::class, 'user_id');
}
public function plan()
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class, 'plan_id');
}
@@ -55,7 +57,7 @@ class PlanComment extends Model
return $this->hasMany(CommentUpvote::class, 'comment_id');
}
public function isUpVoted(): bool
public function isUpVoted(): bool|int
{
if (auth()->user()) {
return auth()
@@ -68,7 +70,7 @@ class PlanComment extends Model
return false;
}
public function isDownVoted(): bool
public function isDownVoted(): bool|int
{
if (auth()->user()) {
return auth()

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* App\Models\PlanLike
@@ -34,7 +35,7 @@ class PlanLike extends Model
'user_id',
];
public function scopeForPlan($query, Plan $plan)
public function scopeForPlan(Builder $query, Plan $plan): Builder
{
return $query->where('plan_id', $plan->id);
}

View File

@@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Str;
/**
* App\Models\Recipe
@@ -93,26 +95,16 @@ class Recipe extends Model implements Viewable
// ->doNotGenerateSlugsOnUpdate();
}
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
public function getRouteKeyName(): string
{
return 'slug';
}
public function tags()
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
/* public function author() */
/* { */
/* return $this->belongsToMany(User::class, 'recipe_author', 'recipe_id', 'user_id'); */
/* } */
public function author(): HasOneThrough
{
return $this
@@ -126,7 +118,7 @@ class Recipe extends Model implements Viewable
);
}
public function comments()
public function comments(): HasMany
{
return $this->hasMany(RecipeComment::class);
}
@@ -136,17 +128,17 @@ class Recipe extends Model implements Viewable
return $this->hasMany(RecipeLike::class);
}
public function scopeNegativeComments(Builder $query)
public function scopeNegativeComments(Builder $query): Builder
{
return static::comments()->where('grade', '<=', '3');
}
public function scopePositiveComments(Builder $query)
public function scopePositiveComments(Builder $query): Builder
{
return static::comments()->where('grade', '>', '3');
}
public function isLiked(): bool
public function isLiked(): bool|int
{
$curUser = auth()->user();
if ($curUser) {
@@ -156,7 +148,7 @@ class Recipe extends Model implements Viewable
return false;
}
public function removeLike(): bool
public function removeLike(): bool|int
{
$curUser = auth()->user();
if ($curUser) {
@@ -166,7 +158,7 @@ class Recipe extends Model implements Viewable
return false;
}
public function scopeContainsTag($query, $tagString): Builder
public function scopeContainsTag(Builder $query, string $tagString): Builder
{
$tagId = Str::of($tagString)->split('/-/')->first();

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\RecipeComment
@@ -40,12 +41,12 @@ class RecipeComment extends Model
{
use HasFactory;
public function author()
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function recipe()
public function recipe(): BelongsTo
{
return $this->belongsTo(Recipe::class, 'recipe_id');
}
@@ -55,7 +56,7 @@ class RecipeComment extends Model
return $this->hasMany(RecipeCommentUpvote::class, 'recipe_comment_id');
}
public function isUpVoted(): bool
public function isUpVoted(): bool|int
{
$curUser = auth()->user();
if ($curUser) {
@@ -68,7 +69,7 @@ class RecipeComment extends Model
return false;
}
public function isDownVoted(): bool
public function isDownVoted(): bool|int
{
$curUser = auth()->user();
if ($curUser) {
@@ -81,7 +82,7 @@ class RecipeComment extends Model
return false;
}
public function removeUpVote(): bool
public function removeUpVote(): bool|int
{
$curUser = auth()->user();
if ($curUser) {
@@ -94,7 +95,7 @@ class RecipeComment extends Model
return false;
}
public function removeDownVote(): bool
public function removeDownVote(): bool|int
{
$curUser = auth()->user();
if ($curUser) {

View File

@@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;
/**
* App\Models\RecipeCommentUpvote
@@ -42,22 +44,22 @@ class RecipeCommentUpvote extends Model
'type',
];
public function author()
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function recipe_comment()
public function recipe_comment(): BelongsTo
{
return $this->belongsTo(BlogPostComment::class, 'recipe_comment_id');
}
public function scopeForRecipeCommentsUpVote($query, RecipeComment $comment)
public function scopeForRecipeCommentsUpVote(Builder $query, RecipeComment $comment): Builder
{
return $query->where('recipe_comment_id', $comment->id)->where('type', 'upvote');
}
public function scopeForRecipeCommentsDownVote($query, RecipeComment $comment)
public function scopeForRecipeCommentsDownVote(Builder $query, RecipeComment $comment): Builder
{
return $query->where('recipe_comment_id', $comment->id)->where('type', 'downvote');
}

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* App\Models\RecipeLike
@@ -34,7 +35,7 @@ class RecipeLike extends Model
'user_id',
];
public function scopeForRecipe($query, Recipe $recipe)
public function scopeForRecipe(Builder $query, Recipe $recipe): Builder
{
return $query->where('recipe_id', $recipe->id);
}

View File

@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
/**
* App\Models\Tag
@@ -42,7 +43,7 @@ class Tag extends Model
return $this->morphedByMany(BlogPost::class, 'taggable', 'taggables');
}
public function recipes()
public function recipes():MorphToMany
{
return $this->morphedByMany(Recipe::class, 'taggable', 'taggables');
}

View File

@@ -4,18 +4,22 @@ namespace App\Presenters;
use Illuminate\Support\Facades\Storage;
use Laracasts\Presenter\Presenter;
use App\Models\User;
/**
* @property User $author
*/
class PlanPresenter extends Presenter
{
public function getAuthorPhoto()
public function getAuthorPhoto(): string
{
$author = $this->getAuthor();
return Storage::url($author->profile_photo_path);
}
public function getAuthor()
public function getAuthor(): User
{
return $this->author->first();
return $this->author->first();
}
}

View File

@@ -6,9 +6,11 @@ use FakerRestaurant\Provider\en_US\Restaurant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
use Faker\Generator;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Plan>
* @property Generator|Restaurant $faker
*/
class PlanFactory extends Factory
{
@@ -16,6 +18,7 @@ class PlanFactory extends Factory
* Define the model's default state.
*
* @return array<string, mixed>
*
*/
public function definition(): array
{
@@ -68,7 +71,7 @@ class PlanFactory extends Factory
];
}
private function generateImageFile(string $name = 'plans')
private function generateImageFile(string $name = 'plans'): string
{
$images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg'];
Storage::fake($name);

View File

@@ -7,7 +7,7 @@ namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PlanComment>
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\RecipeComment>
*/
class RecipeCommentFactory extends Factory
{

View File

@@ -8,9 +8,12 @@ use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Faker\Generator;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Recipe>
*
* @property Generator|Restaurant|Youtube $faker
*/
class RecipeFactory extends Factory
{
@@ -38,7 +41,7 @@ class RecipeFactory extends Factory
];
}
private function generateImageFile(string $name)
private function generateImageFile(string $name): string
{
$images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg'];
Storage::fake($name);
@@ -67,7 +70,7 @@ class RecipeFactory extends Factory
];
}
private function createQuentitySteps($count)
private function createQuentitySteps(int $count): array
{
$result = [];
@@ -78,7 +81,7 @@ class RecipeFactory extends Factory
return $result;
}
private function createQuentityIngredients($count)
private function createQuentityIngredients(int $count): array
{
$result = [];

View File

@@ -4,21 +4,21 @@ namespace Tests\Feature\Plan;
use App\Livewire\Plan\Create;
use App\Models\Plan;
// use Livewire\WithFileUploads;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
use Illuminate\Http\Testing\File;
class CreateTest extends TestCase
{
use RefreshDatabase;
private $image;
private File $image;
private $plan;
private Plan $plan;
protected User $user;
@@ -34,7 +34,7 @@ class CreateTest extends TestCase
}
public function test_show_form_create_plan()
public function test_show_form_create_plan(): void
{
$response = $this
->actingAs($this->user)
@@ -137,6 +137,7 @@ class CreateTest extends TestCase
$planCountAtStart = Plan::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->plan->name)
->assertSet('name', $this->plan->name)
@@ -190,7 +191,7 @@ class CreateTest extends TestCase
->set('packet2_options', null)
->set('packet3_options', null)
->call('publish')
->assertHasErrors(['name', 'image', 'duration', 'details']);
->assertHasErrors(['name', 'image', 'details']);
$this->assertEquals($planCountAtStart, Plan::count());
}

View File

@@ -15,7 +15,7 @@ class EditTest extends TestCase
{
use RefreshDatabase;
private $image;
private UploadedFile $image;
private Plan $plan;
@@ -32,7 +32,7 @@ class EditTest extends TestCase
$this->plan = Plan::factory()->create();
}
public function test_show_form_edit_plan()
public function test_show_form_edit_plan(): void
{
$response = $this
->actingAs($this->user)
@@ -75,7 +75,7 @@ class EditTest extends TestCase
$updatedPlanAfterToAddDatabase = Plan::orderByDesc('id')->first();
$newPlanUrl = route('plan.show', $updatedPlanAfterToAddDatabase, false);
$newPlanUrl = route('plan.show', $updatedPlanAfterToAddDatabase);
Storage::disk('tmp-for-tests')->assertExists($updatedPlanAfterToAddDatabase->image);

View File

@@ -11,7 +11,7 @@ class IndexTest extends TestCase
{
use RefreshDatabase;
private $plan;
public Plan $plan;
protected User $user;
@@ -23,7 +23,7 @@ class IndexTest extends TestCase
$this->plan = Plan::inRandomOrder()->first();
}
public function test_index_page_success()
public function test_index_page_success(): void
{
$response = $this
->actingAs($this->user)

View File

@@ -14,7 +14,7 @@ class ShowTest extends TestCase
// use RefreshDatabase;
use RefreshDatabase;
private $plan;
private Plan $plan;
protected User $user;
@@ -26,7 +26,7 @@ class ShowTest extends TestCase
$this->plan = Plan::inRandomOrder()->first();
}
public function test_show_page_success()
public function test_show_page_success(): void
{
$response = $this->actingAs($this->user)
->get(route('plan.show', $this->plan, false));

View File

@@ -22,9 +22,7 @@ final class CreateTest extends TestCase
private Recipe $recipe;
private $image;
private $steps;
private UploadedFile $image;
public function setUp(): void
{
@@ -114,7 +112,7 @@ final class CreateTest extends TestCase
$newRecipeUrl = route('recipe.show', $lastCreatedRecipe, false);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image);
// Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image);
$testCase->assertRedirect($newRecipeUrl);
@@ -153,7 +151,7 @@ final class CreateTest extends TestCase
$newRecipeUrl = route('recipe.show', $lastCreatedRecipe, false);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image);
// Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image);
$testCase->assertRedirect($newRecipeUrl);
@@ -177,7 +175,7 @@ final class CreateTest extends TestCase
->set('type', null)
->set('description', null)
->call('publish')
->assertHasErrors(['image', 'state', 'type', 'description']);
->assertHasErrors(['image', 'description']);
// doesnt to add to database
self::assertEquals($count, Recipe::count());

View File

@@ -11,7 +11,7 @@ class IndexTest extends TestCase
{
use RefreshDatabase;
private Recipe $recipe;
public Recipe $recipe;
protected User $user;
@@ -23,7 +23,7 @@ class IndexTest extends TestCase
$this->recipe = Recipe::inRandomOrder()->first();
}
public function test_index_page_success()
public function test_index_page_success(): void
{
$response = $this
->actingAs($this->user)

View File

@@ -23,7 +23,7 @@ class ShowTest extends TestCase
$this->recipe = Recipe::inRandomOrder()->first();
}
public function test_show_page_success()
public function test_show_page_success(): void
{
$response = $this->actingAs($this->user)
->get(route('recipe.show', $this->recipe, false));