Passed
Push — 5.0.0 ( ae03fe...9fcb89 )
by Fèvre
05:56
created

Input::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
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 17
dl 0
loc 24
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\View\Component;
8
9
class Input extends Component
10
{
11
    public string $uuid;
12
13
    /**
14
     * Create a new component instance.
15
     */
16
    public function __construct(
17
        public ?string $label = null,
18
        public ?string $icon = null,
19
        public ?string $iconRight = null,
20
        public ?string $hint = null,
21
        public ?string $hintClass = 'fieldset-label',
22
        public ?string $prefix = null,
23
        public ?string $suffix = null,
24
        public ?bool $inline = false,
25
        public ?bool $clearable = false,
26
        public ?bool $money = false,
27
        public ?string $locale = 'en-US',
28
29
        // Slots
30
        public mixed $prepend = null,
31
        public mixed $append = null,
32
33
        // Validations
34
        public ?string $errorField = null,
35
        public ?string $errorClass = 'text-error',
36
        public ?bool $omitError = false,
37
        public ?bool $firstErrorOnly = false,
38
    ) {
39
        $this->uuid = md5(serialize($this));
40
    }
41
42
    public function modelName(): ?string
43
    {
44
        return $this->attributes->whereStartsWith('wire:model')->first();
45
    }
46
47
    public function errorFieldName(): ?string
48
    {
49
        return $this->errorField ?? $this->modelName();
50
    }
51
52
    public function moneySettings(): string
53
    {
54
        return json_encode([
55
            'init' => true,
56
            'maskOpts' => [
57
                'locales' => $this->locale
58
            ]
59
        ]);
60
    }
61
62
    /**
63
     * Get the view / contents that represent the component.
64
     */
65
    public function render(): View|Closure|string
66
    {
67
        return view('components.input');
68
    }
69
}
70