Passed
Pull Request — 2.x (#1360)
by Harings
09:37
created

FieldWithOptions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 1
b 0
f 0
nc 1
nop 16
dl 0
loc 41
rs 9.6

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
abstract class FieldWithOptions extends TwillFormComponent
6
{
7
    public $options;
8
    public $unpack;
9
    public $columns;
10
    public $searchable;
11
    public $note;
12
    public $placeholder;
13
    public $disabled;
14
    public $addNew;
15
    public $moduleName;
16
    public $storeUrl;
17
    public $required;
18
    public $fieldsInModal;
19
    public $default;
20
    /** Below are unused but needed to keep compatible  */
21
    public $confirmMessageText;
22
    public $confirmTitleText;
23
    public $requireConfirmation;
24
25
    public function __construct(
26
        $name,
27
        $label,
28
        $renderForBlocks = false,
29
        $renderForModal = false,
30
        $options = [],
31
        $unpack = false,
32
        $columns = 0,
33
        $searchable = false,
34
        $note = null,
35
        $placeholder = null,
36
        $disabled = false,
37
        $addNew = false,
38
        $moduleName = null,
39
        $storeUrl = null,
40
        $default = null,
41
        $fieldsInModal = false
42
    ) {
43
        parent::__construct(
44
            $name,
45
            $label,
46
            $renderForBlocks,
47
            $renderForModal
48
        );
49
50
        $this->options = $options;
51
        $this->unpack = $unpack;
52
        $this->columns = $columns;
53
        $this->searchable = $searchable;
54
        $this->note = $note;
55
        $this->placeholder = $placeholder;
56
        $this->disabled = $disabled;
57
        $this->addNew = $addNew;
58
        $this->default = $default;
59
        $this->moduleName = $moduleName;
60
        $this->storeUrl = $storeUrl;
61
        $this->fieldsInModal = $fieldsInModal;
62
63
        $this->confirmMessageText = null;
64
        $this->confirmTitleText = null;
65
        $this->requireConfirmation = null;
66
    }
67
68
    public function isInModal(): bool {
69
        return $this->fieldsInModal ?? false;
70
    }
71
72
    public function getOptions(): array
73
    {
74
        return is_object($this->options) && method_exists($this->options, 'map') ? $this->options->map(
75
            function ($label, $value) {
76
                return [
77
                    'value' => $value,
78
                    'label' => $label,
79
                ];
80
            }
81
        )->values()->toArray() : $this->options;
82
    }
83
}
84