Completed
Push — master ( 90cc04...822bf4 )
by Maxime
03:27
created

FormView::add()   C

Complexity

Conditions 13
Paths 36

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 13
nc 36
nop 5
dl 0
loc 31
ccs 20
cts 20
cp 1
crap 13
rs 5.1234
c 0
b 0
f 0

How to fix   Complexity   

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:

1
<?php namespace Distilleries\FormBuilder;
2
3
use Kris\LaravelFormBuilder\Form;
4
5
class FormView extends Form
6
{
7
8
9
    // ------------------------------------------------------------------------------------------------
10
    // ------------------------------------------------------------------------------------------------
11
    // ------------------------------------------------------------------------------------------------
12 140
13
    public function add($name, $type = 'text', array $options = [], $modify = false, $noOveride = false)
14
{
15
16 140
17 140
    if (!isset($options['noInEditView'])) {
18 132
        $options['noInEditView'] = false;
19 132
    }
20
21
22 140
    if (!empty($options) && empty($options['default_value']) && !empty($options['value'])) {
23 140
        $options['default_value'] = $options['value'];
24 4
    }
25
26 4
27 4
    if (!empty($this->formOptions) && !empty($this->formOptions['do_not_display_' . $name]) && $this->formOptions['do_not_display_' . $name] === true) {
28 4
        $type = 'hidden';
29 4
30
        if (!empty($options) && !empty($options['selected'])) {
31 4
            $options['default_value'] = $options['selected'];
32
        }
33 140
34 140
    }
35 36
36 36
    if ($type == 'choice' && !isset($options['selected'])) {
37 4
        if (isset($this->model->{$name})) {
38 4
            $options['selected'] = $this->model->{$name};
39 36
        }
40
    }
41 140
42
    return parent::add($name, $type, $options, $modify);
43
}
44
45
    // ------------------------------------------------------------------------------------------------
46
    // ------------------------------------------------------------------------------------------------
47
    // ------------------------------------------------------------------------------------------------
48 76
49
    public function renderFormView(array $options = [])
50 76
    {
51
        return $this->view($options, $this->fields);
52
    }
53
54
    // ------------------------------------------------------------------------------------------------
55
56 4
57
    public function renderRestView()
58 4
    {
59
        $fields = $this->getUnrenderedFields();
60 4
61
        return $this->view([], $fields);
62
    }
63
64
    // ------------------------------------------------------------------------------------------------
65 80
66
    protected function view($options, $fields)
67 80
    {
68
        $formOptions = $this->formHelper->mergeOptions($this->formOptions, $options);
69 80
70 80
        return $this->formHelper->getView()
71 80
            ->make($this->formHelper->getConfig('form'))
72 80
            ->with(['showFields' => true])
73 80
            ->with(['showEnd' => false])
74 80
            ->with(['showStart' => false])
75 80
            ->with(['isNotEditable' => true])
76 80
            ->with('formOptions', $formOptions)
77 80
            ->with('fields', $fields)
78 80
            ->with('model', $this->getModel())
79
            ->render();
80
    }
81
}