Markdown::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
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 10
dl 0
loc 15
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
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 Markdown extends Component
12
{
13
    public string $uuid;
14
15
    public string $uploadUrl = '';
16
17
    public function __construct(
18
        public ?string $value = null,
19
        public ?string $label = null,
20
        public ?string $hint = null,
21
        public ?string $hintClass = 'fieldset-label',
22
        public ?string $disk = 'public',
23
        public ?string $folder = 'markdown',
24
        public ?array $config = [],
25
26
        // Validations
27
        public ?string $errorClass = 'text-error',
28
        public ?bool $omitError = false,
29
        public ?bool $firstErrorOnly = false,
30
    ) {
31
        $this->uuid = md5(serialize($this));
32
        //$this->uploadUrl = route('upload', absolute: false);
33
    }
34
35
    public function setup(): string
36
    {
37
        $setup = array_merge([
38
            'spellChecker' => false,
39
            'autoSave' => false,
40
            'uploadImage' => false,
41
            'imageAccept' => 'image/png, image/jpeg',
42
            'toolbar' => [
43
                'heading', 'bold', 'italic', 'strikethrough', '|',
44
                'code', 'quote', 'unordered-list', 'ordered-list', 'horizontal-rule', '|',
45
                'link', 'table', '|',
46
                'preview', 'side-by-side'
47
            ],
48
            //'forceSync' => true
49
        ], $this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        ], /** @scrutinizer ignore-type */ $this->config);
Loading history...
50
51
        // Table default CSS class `.table` breaks the layout.
52
        // Here is a workaround
53
        $table = "{ 'title' : 'Table', 'name' : 'myTable', 'action' : EasyMDE.drawTable, 'className' : 'fa fa-table' }";
54
55
        return str(json_encode($setup))
56
            ->replace("\"", "'")
57
            ->trim('{}')
58
            ->replace("'table'", $table)
59
            ->toString();
60
    }
61
62
    public function modelName(): ?string
63
    {
64
        return $this->attributes->whereStartsWith('wire:model')->first();
65
    }
66
67
    public function errorFieldName(): ?string
68
    {
69
        return $this->modelName() ?? $this->attributes->whereStartsWith('name')->first();
70
    }
71
72
    /**
73
     * Get the view / contents that represent the component.
74
     */
75
    public function render(): View|Closure|string
76
    {
77
        return view('components.markdown');
78
    }
79
}
80