Completed
Push — master ( 9a42b3...71797f )
by Song
02:37
created

ValuePicker::addPickBtn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Form\Field;
7
use Illuminate\Support\Arr;
8
9
class ValuePicker
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $modal;
15
16
    /**
17
     * @var Text|File
18
     */
19
    protected $field;
20
21
    /**
22
     * @var string
23
     */
24
    protected $column;
25
26
    /**
27
     * @var string
28
     */
29
    protected $selecteable;
30
31
    /**
32
     * @var string
33
     */
34
    protected $separator;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $multiple = false;
40
41
    /**
42
     * ValuePicker constructor.
43
     *
44
     * @param string $selecteable
45
     * @param string $column
46
     * @param bool $multiple
47
     * @param string $separator
48
     */
49
    public function __construct($selecteable, $column = '', $multiple = false, $separator = ';')
50
    {
51
        $this->selecteable = $selecteable;
52
        $this->column      = $column;
53
        $this->multiple    = $multiple;
54
        $this->separator   = $separator;
55
    }
56
57
    /**
58
     * @param int $multiple
0 ignored issues
show
Bug introduced by
There is no parameter named $multiple. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
59
     *
60
     * @return string
61
     */
62
    protected function getLoadUrl()
63
    {
64
        $selectable = str_replace('\\', '_', $this->selecteable);
65
66
        $args = [$this->multiple, $this->column];
67
68
        return route('admin.handle-selectable', compact('selectable', 'args'));
69
    }
70
71
    /**
72
     * @param Field $field
73
     * @param \Closure|null $callback
74
     */
75
    public function mount(Field $field, \Closure $callback = null)
76
    {
77
        $this->field = $field;
0 ignored issues
show
Documentation Bug introduced by
It seems like $field of type object<Encore\Admin\Form\Field> is incompatible with the declared type object<Encore\Admin\Form...\Admin\Form\Field\File> of property $field.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
        $this->modal = sprintf('picker-modal-%s', $field->getElementClassString());
79
80
        $this->addPickBtn($callback);
81
82
        Admin::component('admin::components.filepicker', [
83
            'url'       => $this->getLoadUrl(),
84
            'modal'     => $this->modal,
85
            'selector'  => $this->field->getElementClassSelector(),
86
            'separator' => $this->separator,
87
            'multiple'  => $this->multiple,
88
            'is_file'   => $this->field instanceof File,
89
            'is_image'  => $this->field instanceof Image,
90
            'url_tpl'   => $this->field instanceof File ? $this->field->objectUrl('__URL__') : '',
91
        ]);
92
    }
93
94
    /**
95
     * @param \Closure|null $callback
96
     */
97
    protected function addPickBtn(\Closure $callback = null)
98
    {
99
        $text = admin_trans('admin.browse');
100
101
        $btn = <<<HTML
102
<a class="btn btn-primary" data-toggle="modal" data-target="#{$this->modal}">
103
    <i class="fa fa-folder-open"></i>  {$text}
104
</a>
105
HTML;
106
107
        if ($callback) {
108
            $callback($btn);
109
        } else {
110
            $this->field->addVariables(compact('btn'));
111
        }
112
    }
113
114
    /**
115
     * @param string $field
116
     * @return array|\Illuminate\Support\Collection
117
     */
118
    public function getPreview(string $field)
119
    {
120
        if (empty($value = $this->field->value())) {
121
            return [];
122
        }
123
124
        if ($this->multiple) {
125
            $value = explode($this->separator, $value);
126
        }
127
128
        return collect(Arr::wrap($value))->map(function ($item) use ($field) {
129
            return [
130
                'url'     => $this->field->objectUrl($item),
0 ignored issues
show
Bug introduced by
The method objectUrl does only exist in Encore\Admin\Form\Field\File, but not in Encore\Admin\Form\Field\Text.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
                'value'   => $item,
132
                'is_file' => $field == File::class,
133
            ];
134
        });
135
    }
136
}
137