Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

FormBuilder::form()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 22
cts 22
cp 1
rs 8.439
cc 6
eloc 23
nc 8
nop 2
crap 6
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 \Illuminate\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 45
    public function form(FormInterface $form, array $attrs = [])
36
    {
37
        // Populate form from edited model
38 45
        $model = $form->getModel();
39 45
        if ($model instanceof Model) {
40 9
            Former::populate($model);
41
        }
42
43
        // Start a form and add rules
44 45
        $formType = $form->openType();
45 45
        $former   = Former::$formType();
46
        array_walk($attrs, function ($value, $attr) use ($former) {
47 45
            if ($value === null) {
48 38
                $former->$attr();
49
            } else {
50 45
                $former->$attr($value);
51
            }
52 45
        });
53 45
        $former->rules($form->rules());
54
55
        // Generate form fields
56 45
        $output = $former;
57 45
        $fields = $form->fields();
58 45
        foreach ($fields as $name => $field) {
59 45
            $element = $this->element($name, $field);
60
61 45
            if ($element instanceof Field) {
62 45
                if (null === $model) {
63 41
                    $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 45
            $output .= $element;
68
        }
69
70
        // Generate form actions
71 45
        $output .= $this->actions($form);
72
73
        // Close the opened form
74 45
        $output .= Former::close();
75
76 45
        return $output;
77
    }
78
79
    /**
80
     * Generate Former field.
81
     *
82
     * @param string $name
83
     * @param array  $field
84
     *
85
     * @return Field
86
     */
87 45
    public function element($name, array $field)
88
    {
89 45
        $filterKeys = ['type'];
90 45
        $attrs      = array_diff_key($field, array_flip($filterKeys));
91
92
        // Create field with name
93 45
        $element = Former::{$field['type']}($name);
94
95
        // Create field attributes
96 45
        array_walk($attrs, function ($value, $attr) use ($element) {
97 45
            if ($value === null) {
98 24
                $element->$attr();
99
            } else {
100 45
                $element->$attr($value);
101
            }
102 45
        });
103
104 45
        return $element;
105
    }
106
107
    /**
108
     * Render form actions.
109
     *
110
     * @param FormInterface $form
111
     *
112
     * @return string
113
     */
114 45
    public function actions(FormInterface $form)
115
    {
116 45
        $output  = '';
117 45
        $buttons = $form->actions();
118 45
        if (!empty($buttons)) {
119 37
            $actions = Former::actions()->addClass('form-actions');
120 37
            foreach ($buttons as $options) {
121 37
                if (is_array($options)) {
122 29
                    $actions->{$options['type']}($options['label'], $options);
123
                } else {
124 37
                    $actions->primary_submit(trans('tinyissue.' . $options));
125
                }
126
            }
127 37
            $output .= $actions;
128
        }
129
130 45
        return $output;
131
    }
132
}
133