Completed
Push — dev ( 38969c...e9d5f9 )
by Marc
13:15
created

FieldWrapper::withWidgets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php namespace Mascame\Artificer\Fields;
2
3
use App;
4
use Mascame\Artificer\Artificer;
5
use Mascame\Artificer\Widget\AbstractWidget;
6
use Mascame\Formality\Field\FieldInterface;
7
use Mascame\Formality\Field\TypeInterface;
8
9
class FieldWrapper
10
{
11
    use Filterable;
12
13
    public static $widgets = [];
14
15
    /**
16
     * Sometimes ajax limits output, setting this to true will return all
17
     *
18
     * @var bool
19
     */
20
    public $showFullField = false;
21
22
    /**
23
     * @var FieldInterface|TypeInterface
24
     */
25
    protected $field;
26
27
    /**
28
     * Field constructor.
29
     * @param FieldInterface|TypeInterface $field
30
     * @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...
31
     */
32
    public function __construct(FieldInterface $field)
33
    {
34
        $this->field = $field;
35
36
        $this->boot();
37
    }
38
39
    /**
40
     * @param $widget
41
     * @return bool
42
     */
43
    public function addWidget(AbstractWidget $widget)
44
    {
45
        if ( ! in_array($widget->name, self::$widgets)) {
46
            self::$widgets[$widget->name] = $widget;
47
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
55
    /**
56
     * Used to load custom assets, widgets, ...
57
     *
58
     */
59
    protected function boot()
60
    {
61
        if ( ! $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...
62
            return null;
63
        }
64
65
        $widgets = $this->field->getOption('widgets');
66
67
        foreach ($widgets as $widget) {
68
            try {
69
                $this->addWidget(App::make($widget));
70
            } catch (\Exception $e) {
71
                throw new \Exception("Widget '{$widget}' was not found");
72
            }
73
        }
74
    }
75
76
    /**
77
     * @param null $value
78
     * @return null
79
     */
80
    public function show($value = null)
81
    {
82
        $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...
83
84
        if ($show = $this->field->getOption('show')) {
85
            if (is_callable($show)) {
86
                return $show($value);
87
            }
88
        }
89
90
        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...
91
    }
92
93
    /**
94
     * @return bool|mixed|null|string
95
     */
96
    public function output()
97
    {
98
        if ($this->isHidden()) return null;
99
100
        return $this->field->output();
0 ignored issues
show
Bug introduced by
The method output 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...
101
    }
102
103
    public function withWidgets($output) {
104
        foreach (self::$widgets as $widget) {
105
            $output = $widget->field($output);
106
        }
107
108
        return $output;
109
    }
110
111
    /**
112
     * @param $array
113
     * @return bool
114
     */
115
    protected function isAll($array)
116
    {
117
        return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*');
118
    }
119
120
    /**
121
     * @param string $visibility [visible|hidden]
122
     * @return bool
123
     */
124
    protected function isListedAs($visibility, $action = null)
125
    {
126
        if (! $action) $action = Artificer::getCurrentAction();
127
128
        $listOptions = Artificer::getModel()->getOption($action);
129
130
        if (! $listOptions || ! isset($listOptions[$visibility])) return false;
131
132
        $list = $listOptions[$visibility];
133
134
        if ($this->isAll($list)) return true;
135
136
        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...
137
    }
138
139
    /**
140
     * Hidden fields have preference.
141
     *
142
     * @return bool
143
     */
144
    public function isVisible()
145
    {
146
        if ($this->isHidden()) return false;
147
148
        return $this->isListedAs('visible');
149
    }
150
151
    /**
152
     * @param $value
153
     * @param $array
154
     * @return bool
155
     */
156
    public function isInArray($value, $array)
157
    {
158
        return (is_array($array) && in_array($value, $array)) ? true : false;
159
    }
160
161
    /**
162
     * @return bool
163
     */
164
    public function isHidden()
165
    {
166
        return $this->isListedAs('hidden');
167
    }
168
169
    public static function get($name)
170
    {
171
        return array_get(\View::getShared(), 'fields')[$name];
172
    }
173
174
    public function __get($name) {
175
        $accessor = 'get' . studly_case($name);
176
177
        if (! method_exists($this->field, $accessor)) {
178
            return null;
179
        }
180
181
        return $this->field->$accessor();
182
    }
183
184
    public function __call($method, $args) {
185
        if (! method_exists($this->field, $method)) {
186
            return null;
187
        }
188
189
        return $this->field->$method($args);
190
    }
191
}