Completed
Push — dev ( 9ff19d...9155f2 )
by Marc
03:27
created

FieldWrapper::getInstalledWidgets()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php namespace Mascame\Artificer\Fields;
2
3
use Mascame\Artificer\Artificer;
4
use Mascame\Formality\Field\FieldInterface;
5
use Mascame\Formality\Field\TypeInterface;
6
7
class FieldWrapper
8
{
9
    use Filterable;
10
11
    protected $widgets = [];
12
13
    /**
14
     * Sometimes ajax limits output, setting this to true will return all
15
     *
16
     * @var bool
17
     */
18
    public $showFullField = false;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $withWidgets = false;
24
25
    /**
26
     * @var FieldInterface|TypeInterface
27
     */
28
    public $field;
29
30
    /**
31
     * Field constructor.
32
     * @param FieldInterface|TypeInterface $field
33
     * @param null $relation
0 ignored issues
show
Bug introduced by
There is no parameter named $relation. 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...
34
     */
35
    public function __construct(FieldInterface $field)
36
    {
37
        $this->field = $field;
38
39
        $this->widgets = $this->getInstalledWidgets();
40
    }
41
42
    /**
43
     * Only get widgets that are installed
44
     *
45
     * @return array
46
     */
47
    protected function getInstalledWidgets()
48
    {
49
        $installedWidgets = [];
50
        $widgetManager = Artificer::widgetManager();
51
        $widgets = $this->field->getOption('widgets', []);
0 ignored issues
show
Bug introduced by
The method getOption does only exist in Mascame\Formality\Field\FieldInterface, but not in Mascame\Formality\Field\TypeInterface.

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...
52
53
        foreach ($widgets as $widget) {
54
            if ($widgetManager->isInstalled($widget)) {
55
                $installedWidgets[] = $widget;
56
            }
57
        }
58
59
        return $installedWidgets;
60
    }
61
62
    /**
63
     * @param null $value
64
     * @return null
65
     */
66
    public function show($value = null)
67
    {
68
        $value = ($value) ? $this->field->setValue($value) : $this->field->getOption('default');
0 ignored issues
show
Bug introduced by
The method setValue does only exist in Mascame\Formality\Field\FieldInterface, but not in Mascame\Formality\Field\TypeInterface.

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...
Bug introduced by
The method getOption does only exist in Mascame\Formality\Field\FieldInterface, but not in Mascame\Formality\Field\TypeInterface.

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...
69
70
        if ($show = $this->field->getOption('show')) {
71
            if (is_callable($show)) {
72
                return $show($value);
73
            }
74
        }
75
76
        return $this->field->show();
0 ignored issues
show
Bug introduced by
The method show does only exist in Mascame\Formality\Field\TypeInterface, but not in Mascame\Formality\Field\FieldInterface.

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...
77
    }
78
79
    /**
80
     * @return bool|mixed|null|string
81
     */
82
    public function output()
83
    {
84
        if ($this->isHidden()) return null;
85
86
        $field = $this;
87
88
        if ($this->withWidgets) {
89
            $field = $this->applyWidgets();
90
        }
91
92
        return $field->field->output();
93
    }
94
95
    public function withWidgets() {
96
        $this->withWidgets = true;
97
98
        return $this;
99
    }
100
101
    protected function applyWidgets() {
102
        $field = $this;
103
104
        foreach ($this->widgets as $widget) {
105
            $widget = Artificer::widgetManager()->get($widget);
106
            $widget->assets(Artificer::assetManager());
107
108
            $field = $widget->field($field);
109
        }
110
111
        return $field;
112
    }
113
114
    /**
115
     * @param $array
116
     * @return bool
117
     */
118
    protected function isAll($array)
119
    {
120
        return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*');
121
    }
122
123
    /**
124
     * @param string $visibility [visible|hidden]
125
     * @return bool
126
     */
127
    protected function isListedAs($visibility, $action = null)
128
    {
129
        if (! $action) $action = Artificer::getCurrentAction();
130
131
        $listOptions = Artificer::getModel()->getOption($action);
132
133
        if (! $listOptions || ! isset($listOptions[$visibility])) return false;
134
135
        $list = $listOptions[$visibility];
136
137
        if ($this->isAll($list)) return true;
138
139
        return $this->isInArray($this->field->getName(), $list);
0 ignored issues
show
Bug introduced by
The method getName does only exist in Mascame\Formality\Field\FieldInterface, but not in Mascame\Formality\Field\TypeInterface.

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...
140
    }
141
142
    /**
143
     * Hidden fields have preference.
144
     *
145
     * @return bool
146
     */
147
    public function isVisible()
148
    {
149
        if ($this->isHidden()) return false;
150
151
        return $this->isListedAs('visible');
152
    }
153
154
    /**
155
     * @param $value
156
     * @param $array
157
     * @return bool
158
     */
159
    public function isInArray($value, $array)
160
    {
161
        return (is_array($array) && in_array($value, $array)) ? true : false;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isHidden()
168
    {
169
        return $this->isListedAs('hidden');
170
    }
171
172
    public static function get($name)
173
    {
174
        return array_get(\View::getShared(), 'fields')[$name];
175
    }
176
177
    public function __get($name) {
178
        $accessor = 'get' . studly_case($name);
179
180
        if (! method_exists($this->field, $accessor)) {
181
            return null;
182
        }
183
184
        return $this->field->$accessor();
185
    }
186
187
    public function __call($method, $args) {
188
        if (! method_exists($this->field, $method)) {
189
            return null;
190
        }
191
192
        return $this->field->$method($args);
193
    }
194
}