Completed
Push — master ( 67f65f...23a098 )
by Song
14s queued 10s
created

Text::render()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class Text extends Field
8
{
9
    use PlainInput;
10
11
    /**
12
     * @var string
13
     */
14
    protected $icon = 'fa-pencil';
15
16
    protected $withoutIcon = false;
17
18
    /**
19
     * Set custom fa-icon.
20
     *
21
     * @param string $icon
22
     *
23
     * @return $this
24
     */
25
    public function icon($icon)
26
    {
27
        $this->icon = $icon;
28
29
        return $this;
30
    }
31
32
    /**
33
     * Render this filed.
34
     *
35
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
36
     */
37
    public function render()
38
    {
39
        $this->initPlainInput();
40
41
        if (!$this->withoutIcon) {
42
            $this->prepend('<i class="fa '.$this->icon.' fa-fw"></i>');
43
        }
44
        $this->defaultAttribute('type', 'text')
45
            ->defaultAttribute('id', $this->id)
46
            ->defaultAttribute('name', $this->elementName ?: $this->formatName($this->column))
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, Encore\Admin\Form\Field::formatName() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
            ->defaultAttribute('value', old($this->elementName ?: $this->column, $this->value()))
0 ignored issues
show
Bug introduced by
It seems like $this->elementName ?: $this->column can also be of type array; however, old() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
            ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
49
            ->defaultAttribute('placeholder', $this->getPlaceholder());
50
51
        $this->addVariables([
52
            'prepend' => $this->prepend,
53
            'append'  => $this->append,
54
        ]);
55
56
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type string|Illuminate\View\V...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 56 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
57
    }
58
59
    /**
60
     * Add inputmask to an elements.
61
     *
62
     * @param array $options
63
     *
64
     * @return $this
65
     */
66
    public function inputmask($options)
67
    {
68
        $options = json_encode_options($options);
69
70
        $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
71
72
        return $this;
73
    }
74
75
    /**
76
     * Add datalist element to Text input.
77
     *
78
     * @param array $entries
79
     *
80
     * @return $this
81
     */
82
    public function datalist($entries = [])
83
    {
84
        $this->defaultAttribute('list', "list-{$this->id}");
85
86
        $datalist = "<datalist id=\"list-{$this->id}\">";
87
        foreach ($entries as $k => $v) {
88
            $datalist .= "<option value=\"{$k}\">{$v}</option>";
89
        }
90
        $datalist .= '</datalist>';
91
92
        return $this->append($datalist);
93
    }
94
95
    /**
96
     * show no icon in font of input.
97
     *
98
     * @return $this
99
     */
100
    public function withoutIcon()
101
    {
102
        $this->withoutIcon = true;
103
104
        return $this;
105
    }
106
}
107