Passed
Pull Request — master (#1312)
by Diego
05:44
created

InputSwitch::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 12
dl 0
loc 18
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0

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 JeroenNoten\LaravelAdminLte\View\Components\Form;
4
5
class InputSwitch extends InputGroupComponent
6
{
7
    use Traits\OldValueSupportTrait;
8
9
    /**
10
     * The Bootstrap Switch plugin configuration parameters. Array with
11
     * 'key => value' pairs, where the key should be an existing configuration
12
     * property of the plugin.
13
     *
14
     * @var array
15
     */
16
    public $config;
17
18
    /**
19
     * Create a new component instance.
20
     * Note this component requires the 'Bootstrap Switch' plugin.
21
     *
22
     * @return void
23
     */
24 3
    public function __construct(
25
        $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
26
        $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
27
        $errorKey = null, $config = [], $isChecked = null,
28
        $enableOldSupport = null
29 3
    ) {
30 3
        parent::__construct(
31 3
            $name, $id, $label, $igroupSize, $labelClass, $fgroupClass,
32 3
            $igroupClass, $disableFeedback, $errorKey
33
        );
34 3
35 3
        $this->config = is_array($config) ? $config : [];
36
37
        if (isset($isChecked)) {
38
            $this->config['state'] = ! empty($isChecked);
39
        }
40
41
        $this->enableOldSupport = isset($enableOldSupport);
42
    }
43
44 1
    /**
45
     * Make the class attribute for the "input-group" element. Note we overwrite
46 1
     * the method of the parent class.
47
     *
48 1
     * @return string
49 1
     */
50
    public function makeInputGroupClass()
51
    {
52 1
        $classes = ['input-group'];
53 1
54
        if (isset($this->size) && in_array($this->size, ['sm', 'lg'])) {
55
            $classes[] = "input-group-{$this->size}";
56 1
        }
57 1
58
        if ($this->isInvalid()) {
59
            $classes[] = 'adminlte-invalid-iswgroup';
60 1
        }
61
62
        if (isset($this->igroupClass)) {
63
            $classes[] = $this->igroupClass;
64
        }
65
66
        return implode(' ', $classes);
67
    }
68
69 1
    /**
70
     * Make the class attribute for the input group item. Note we overwrite
71 1
     * the method of the parent class.
72
     *
73 1
     * @return string
74 1
     */
75
    public function makeItemClass()
76
    {
77 1
        $classes = [];
78
79
        if ($this->isInvalid()) {
80
            $classes[] = 'is-invalid';
81
        }
82
83
        return implode(' ', $classes);
84
    }
85 1
86
    /**
87 1
     * Get the view / contents that represent the component.
88
     *
89
     * @return \Illuminate\View\View|string
90
     */
91
    public function render()
92
    {
93
        return view('adminlte::components.form.input-switch');
94
    }
95
}
96