Button::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
nc 8
nop 15
dl 0
loc 20
rs 10
c 1
b 0
f 0

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 Illuminate\Contracts\View\View;
9
use Illuminate\View\Component;
10
11
class Button extends Component
12
{
13
    public string $uuid;
14
15
    public string $tooltipPosition = 'lg:tooltip-top';
16
17
    public function __construct(
18
        public ?string $label = null,
19
        public ?string $icon = null,
20
        public ?string $iconClasses = null,
21
        public ?string $iconRight = null,
22
        public ?string $spinner = null,
23
        public ?string $link = null,
24
        public ?bool $external = false,
25
        public ?bool $wireNavigate = false,
26
        public ?bool $responsive = false,
27
        public ?string $badge = null,
28
        public ?string $badgeClasses = null,
29
        public ?string $tooltip = null,
30
        public ?string $tooltipLeft = null,
31
        public ?string $tooltipRight = null,
32
        public ?string $tooltipBottom = null,
33
    ) {
34
        $this->uuid = md5(serialize($this));
35
        $this->tooltip = $this->tooltip ?? $this->tooltipLeft ?? $this->tooltipRight ?? $this->tooltipBottom;
36
        $this->tooltipPosition = $this->tooltipLeft ? 'lg:tooltip-left' : ($this->tooltipRight ? 'lg:tooltip-right' : ($this->tooltipBottom ? 'lg:tooltip-bottom' : 'lg:tooltip-top'));
37
    }
38
39
    public function spinnerTarget(): ?string
40
    {
41
        if ($this->spinner === '1') {
42
            return $this->attributes->whereStartsWith('wire:click')->first();
43
        }
44
45
        return $this->spinner;
46
    }
47
48
    /**
49
     * Get the view / contents that represent the component.
50
     */
51
    public function render(): View|Closure|string
52
    {
53
        return view('components.button');
54
    }
55
}
56