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

FieldWithOptions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 16
dl 0
loc 42
rs 9.584

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->options = $this->getOptions();
52
        $this->unpack = $unpack;
53
        $this->columns = $columns;
54
        $this->searchable = $searchable;
55
        $this->note = $note;
56
        $this->placeholder = $placeholder;
57
        $this->disabled = $disabled;
58
        $this->addNew = $addNew;
59
        $this->default = $default;
60
        $this->moduleName = $moduleName;
61
        $this->storeUrl = $storeUrl;
62
        $this->fieldsInModal = $fieldsInModal;
63
64
        $this->confirmMessageText = null;
65
        $this->confirmTitleText = null;
66
        $this->requireConfirmation = null;
67
    }
68
69
    public function isInModal(): bool
70
    {
71
        return $this->fieldsInModal ?? false;
72
    }
73
74
    public function getOptions(): array
75
    {
76
        return is_object($this->options) && method_exists($this->options, 'map') ? $this->options->map(
0 ignored issues
show
introduced by
The condition is_object($this->options) is always false.
Loading history...
77
            function ($label, $value) {
78
                return [
79
                    'value' => $value,
80
                    'label' => $label,
81
                ];
82
            }
83
        )->values()->toArray() : $this->options;
84
    }
85
}
86