Completed
Push — master ( 822bf4...c53791 )
by Maxime
03:17
created

FormView::add()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 11
nc 18
nop 5
dl 0
loc 31
rs 4.8196
c 0
b 0
f 0
ccs 21
cts 21
cp 1
crap 10

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