Passed
Pull Request — 2.x (#1360)
by Harings
14:11
created

Files::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 10
dl 0
loc 20
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 A17\Twill\View\Components;
4
5
use Illuminate\Support\Str;
6
7
class Files extends TwillFormComponent
8
{
9
10
    public $fieldNote;
11
    public $filesizeMax;
12
    public $buttonOnTop;
13
    public $itemLabel;
14
    public $note;
15
    public $max;
16
17
    public function __construct(
18
        $name,
19
        $label,
20
        $renderForBlocks = false,
21
        $renderForModal = false,
22
        $max = 1,
23
        $itemLabel = null,
24
        $note = null,
25
        $fieldNote = null,
26
        $filesizeMax = 0,
27
        $buttonOnTop = false
28
    ) {
29
        parent::__construct($name, $label, $renderForBlocks, $renderForModal);
30
        $itemLabel = $itemLabel ?? strtolower($label);
31
        $this->itemLabel = $itemLabel;
32
        $this->note = $note ?? 'Add' . ($max > 1 ? " up to $max $itemLabel" : ' one ' . Str::singular($itemLabel));
33
        $this->fieldNote = $fieldNote;
34
        $this->filesizeMax = $filesizeMax;
35
        $this->buttonOnTop = $buttonOnTop;
36
        $this->max = $max;
37
    }
38
39
    public function render()
40
    {
41
        return view('twill::partials.form._files');
42
    }
43
}
44