Passed
Push — 5.0.0 ( 746f48...fc989a )
by Fèvre
06:29
created

Select::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 20
dl 0
loc 27
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Xetaravel\View\Components;
4
5
use Closure;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Support\Collection;
8
use Illuminate\View\Component;
9
10
class Select extends Component
11
{
12
    public string $uuid;
13
14
    public function __construct(
15
        public ?string $label = null,
16
        public ?string $icon = null,
17
        public ?string $iconRight = null,
18
        public ?string $hint = null,
19
        public ?string $hintClass = 'fieldset-label',
20
        public ?string $prefix = null,
21
        public ?string $suffix = null,
22
        public ?string $placeholder = null,
23
        public ?string $placeholderValue = null,
24
        public ?bool $inline = false,
25
        public ?string $optionValue = 'id',
26
        public ?string $optionLabel = 'name',
27
        public ?string $optionClass = '',
28
        public ?string $optionStyle = '',
29
        public Collection|array $options = new Collection(),
30
31
        // Slots
32
        public mixed $prepend = null,
33
        public mixed $append = null,
34
35
        // Validations
36
        public ?string $errorClass = 'text-error',
37
        public ?bool $omitError = false,
38
        public ?bool $firstErrorOnly = false,
39
    ) {
40
        $this->uuid = md5(serialize($this));
41
    }
42
43
    public function modelName(): ?string
44
    {
45
        return $this->attributes->whereStartsWith('wire:model')->first();
46
    }
47
48
    public function errorFieldName(): ?string
49
    {
50
        return $this->modelName() ?? $this->attributes->whereStartsWith('name')->first();
51
    }
52
53
    /**
54
     * Get the view / contents that represent the component.
55
     */
56
    public function render(): View|Closure|string
57
    {
58
        return view('components.select');
59
    }
60
}
61