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

Choices::__construct()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 45
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 3
c 1
b 0
f 0
nc 2
nop 35
dl 0
loc 45
rs 9.6111

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
declare(strict_types=1);
4
5
namespace Xetaravel\View\Components;
6
7
use Closure;
8
use Exception;
9
use Illuminate\Contracts\View\View;
10
use Illuminate\Support\Collection;
11
use Illuminate\View\Component;
12
13
class Choices extends Component
14
{
15
    public string $uuid;
16
17
    public function __construct(
18
        public ?string $label = null,
19
        public ?string $hint = null,
20
        public ?string $hintClass = 'fieldset-label',
21
        public ?string $icon = null,
22
        public ?string $iconRight = null,
23
        public ?bool $inline = false,
24
        public ?bool $clearable = false,
25
        public ?string $prefix = null,
26
        public ?string $suffix = null,
27
        public ?bool $searchable = false,
28
        public ?bool $single = false,
29
        public ?bool $compact = false,
30
        public ?string $compactText = 'selected',
31
        public ?bool $allowAll = false,
32
        public ?string $debounce = '250ms',
33
        public ?int $minChars = 0,
34
        public ?string $allowAllText = 'Select all',
35
        public ?string $removeAllText = 'Remove all',
36
        public ?string $searchFunction = 'search',
37
        public ?string $optionValue = 'id',
38
        public ?string $optionLabel = 'name',
39
        public ?string $optionSubLabel = '',
40
        public ?string $optionAvatar = 'avatar',
41
        public ?bool $valuesAsString = false,
42
        public ?string $height = 'max-h-64',
43
        public Collection|array $options = new Collection(),
44
        public ?string $noResultText = 'No results found.',
45
46
        // Validations
47
        public ?string $errorField = null,
48
        public ?string $errorClass = 'text-error',
49
        public ?bool $omitError = false,
50
        public ?bool $firstErrorOnly = false,
51
52
        // Slots
53
        public mixed $item = null,
54
        public mixed $selection = null,
55
        public mixed $prepend = null,
56
        public mixed $append = null
57
    ) {
58
        $this->uuid = md5(serialize($this));
59
60
        if (($this->allowAll || $this->compact) && ($this->single || $this->searchable)) {
61
            throw new Exception("`allow-all` and `compact` does not work combined with `single` or `searchable`.");
62
        }
63
    }
64
65
    public function modelName(): ?string
66
    {
67
        return $this->attributes->whereStartsWith('wire:model')->first();
68
    }
69
70
    public function errorFieldName(): ?string
71
    {
72
        return $this->errorField ?? $this->modelName();
73
    }
74
75
    public function isReadonly(): bool
76
    {
77
        return $this->attributes->has('readonly') && $this->attributes->get('readonly') === true;
78
    }
79
80
    public function isRequired(): bool
81
    {
82
        return $this->attributes->has('required') && $this->attributes->get('required') === true;
83
    }
84
85
    public function isDisabled(): bool
86
    {
87
        return $this->attributes->has('disabled') && $this->attributes->get('disabled') === true;
88
    }
89
90
    public function getOptionValue($option): mixed
91
    {
92
        $value = data_get($option, $this->optionValue);
93
94
        if ($this->valuesAsString) {
95
            return "'$value'";
96
        }
97
98
        return is_numeric($value) && ! str($value)->startsWith('0') ? $value : "'$value'";
99
    }
100
    /**
101
     * Get the view / contents that represent the component.
102
     */
103
    public function render(): View|Closure|string
104
    {
105
        return view('components.choices');
106
    }
107
}
108