FormBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 108
ccs 43
cts 43
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A element() 0 19 2
B form() 0 43 6
A actions() 0 18 4
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Extensions\Html;
13
14
use Former;
15
use Former\Traits\Field;
16
use Illuminate\Database\Eloquent\Model;
17
use Request;
18
use Tinyissue\Form\FormInterface;
19
20
/**
21
 * FormBuilder is a class to extend Laravel FormBuilder to add extra view macro.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
class FormBuilder extends \Collective\Html\FormBuilder
26
{
27
    /**
28
     * Render Form object into Html form with Former.
29
     *
30
     * @param FormInterface $form
31
     * @param array         $attrs
32
     *
33
     * @return string
34
     */
35 48
    public function form(FormInterface $form, array $attrs = [])
36
    {
37
        // Populate form from edited model
38 48
        $model = $form->getModel();
39 48
        if ($model instanceof Model) {
40 11
            Former::populate($model);
41
        }
42
43
        // Start a form and add rules
44 48
        $formType = $form->openType();
45 48
        $former   = Former::$formType();
46
        array_walk($attrs, function ($value, $attr) use ($former) {
47 48
            if ($value === null) {
48 41
                $former->$attr();
49
            } else {
50 48
                $former->$attr($value);
51
            }
52 48
        });
53 48
        $former->rules($form->rules());
54
55
        // Generate form fields
56 48
        $output = $former;
57 48
        $fields = $form->fields();
58 48
        foreach ($fields as $name => $field) {
59 48
            $element = $this->element($name, $field);
60
61 48
            if ($element instanceof Field) {
62 48
                if (null === $model) {
63 44
                    $element->value = Request::input($name);
1 ignored issue
show
Documentation Bug introduced by
It seems like \Request::input($name) can also be of type array. However, the property $value is declared as type string|null|object<HtmlObject\Traits\Tag>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64
                }
65
            }
66
67 48
            $output .= $element;
68
        }
69
70
        // Generate form actions
71 48
        $output .= $this->actions($form);
72
73
        // Close the opened form
74 48
        $output .= Former::close();
75
76 48
        return $output;
77
    }
78
79
    /**
80
     * Generate Former field.
81
     *
82
     * @param string $name
83
     * @param array  $field
84
     *
85
     * @return Field
86
     */
87 48
    public function element($name, array $field)
88
    {
89 48
        $filterKeys = ['type'];
90 48
        $attrs      = array_diff_key($field, array_flip($filterKeys));
91
92
        // Create field with name
93 48
        $element = Former::{$field['type']}($name);
94
95
        // Create field attributes
96 48
        array_walk($attrs, function ($value, $attr) use ($element) {
97 48
            if ($value === null) {
98 25
                $element->$attr();
99
            } else {
100 48
                $element->$attr($value);
101
            }
102 48
        });
103
104 48
        return $element;
105
    }
106
107
    /**
108
     * Render form actions.
109
     *
110
     * @param FormInterface $form
111
     *
112
     * @return string
113
     */
114 48
    public function actions(FormInterface $form)
115
    {
116 48
        $output  = '';
117 48
        $buttons = $form->actions();
118 48
        if (!empty($buttons)) {
119 39
            $actions = Former::actions()->addClass('form-actions');
120 39
            foreach ($buttons as $options) {
121 39
                if (is_array($options)) {
122 30
                    $actions->{$options['type']}($options['label'], $options);
123
                } else {
124 39
                    $actions->primary_submit(trans('tinyissue.' . $options));
125
                }
126
            }
127 39
            $output .= $actions;
128
        }
129
130 48
        return $output;
131
    }
132
}
133