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

Browser::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 37
c 1
b 0
f 0
nc 8
nop 18
dl 0
loc 64
rs 9.328

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Browser extends TwillFormComponent
8
{
9
    public $moduleName;
10
    public $modules;
11
    public $endpoints;
12
    public $max;
13
    public $note;
14
    public $fieldNote;
15
    public $browserNote;
16
    public $itemLabel;
17
    public $buttonOnTop;
18
    public $wide;
19
    public $sortable;
20
    public $endpoint;
21
    public $routePrefix;
22
    public $params;
23
24
    public function __construct(
25
        $label,
26
        $name = null,
27
        $renderForBlocks = false,
28
        $renderForModal = false,
29
        $moduleName = null,
30
        $modules = [],
31
        $endpoints = [],
32
        $endpoint = null,
33
        $max = 1,
34
        $note = null,
35
        $fieldNote = null,
36
        $browserNote = null,
37
        $itemLabel = null,
38
        $buttonOnTop = false,
39
        $wide = false,
40
        $sortable = true,
41
        $routePrefix = null,
42
        $params = []
43
    ) {
44
        $name = $name ?? $moduleName;
45
        parent::__construct($name, $label, $renderForBlocks, $renderForModal);
46
        $this->name = $name;
47
        $this->moduleName = $moduleName;
48
        $this->modules = $modules;
49
        $this->endpoints = $endpoints;
50
        $this->endpoint = $endpoint;
51
        $this->max = $max;
52
        $this->note = $note;
53
        $this->fieldNote = $fieldNote;
54
        $this->browserNote = $browserNote;
55
        $this->itemLabel = $itemLabel;
56
        $this->buttonOnTop = $buttonOnTop;
57
        $this->wide = $wide;
58
        $this->sortable = $sortable;
59
        $this->routePrefix = $routePrefix;
60
        $this->params = $params;
61
62
        $endpointsFromModules = isset($this->modules) ? collect($this->modules)->map(function ($module) {
63
            return [
64
                'label' => $module['label'] ?? ucfirst($module['name']),
65
                'value' => moduleRoute(
66
                    $module['name'],
67
                    $module['routePrefix'] ?? null,
68
                    'browser',
69
                    $module['params'] ?? [],
70
                    false
71
                ),
72
            ];
73
        })->toArray() : null;
74
75
        $this->endpoints = $this->endpoints ?? $endpointsFromModules ?? [];
76
        $this->endpoint = $this->endpoint ?? (!empty($endpoints) ? null : moduleRoute(
77
                $this->moduleName,
78
                $this->routePrefix,
79
                'browser',
80
                $this->params,
81
                false
82
            ));
83
84
        $this->itemLabel = $this->itemLabel ?? strtolower($this->label);
85
86
        $this->note = $this->note ??
87
            'Add' . ($this->max > 1 ? " up to {$this->max} " . $itemLabel : ' one ' . Str::singular($this->itemLabel));
88
    }
89
90
    public function render()
91
    {
92
        return view('twill::partials.form._browser');
93
    }
94
}
95