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

FieldWithOptions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 14
dl 0
loc 37
rs 9.6666

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
        $options = [],
29
        $unpack = false,
30
        $columns = 0,
31
        $searchable = false,
32
        $note = null,
33
        $placeholder = null,
34
        $disabled = false,
35
        $addNew = false,
36
        $moduleName = null,
37
        $storeUrl = null,
38
        $default = false,
39
        $fieldsInModal = null
40
    ) {
41
        parent::__construct(
42
            $name,
43
            $label
44
        );
45
46
        $this->options = $options;
47
        $this->unpack = $unpack;
48
        $this->columns = $columns;
49
        $this->searchable = $searchable;
50
        $this->note = $note;
51
        $this->placeholder = $placeholder;
52
        $this->disabled = $disabled;
53
        $this->addNew = $addNew;
54
        $this->default = $default;
55
        $this->moduleName = $moduleName;
56
        $this->storeUrl = $storeUrl;
57
        $this->fieldsInModal = $fieldsInModal;
58
59
        $this->confirmMessageText = null;
60
        $this->confirmTitleText = null;
61
        $this->requireConfirmation = null;
62
    }
63
64
    public function inModal(): bool {
65
        return $this->fieldsInModal ?? false;
66
    }
67
68
    public function getOptions(): array
69
    {
70
        return is_object($this->options) && method_exists($this->options, 'map') ? $this->options->map(
71
            function ($label, $value) {
72
                return [
73
                    'value' => $value,
74
                    'label' => $label,
75
                ];
76
            }
77
        )->values()->toArray() : $this->options;
78
    }
79
}
80