Completed
Push — dev ( f9231c...c21f59 )
by Marc
02:46
created

FieldWrapper::isListedAs()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 7
nc 6
nop 2
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
    public 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
    /**
104
     * @param $array
105
     * @return bool
106
     */
107
    protected function isAll($array)
108
    {
109
        return (is_array($array) && isset($array[0]) && $array[0] == '*' || $array == '*');
110
    }
111
112
    /**
113
     * @param string $visibility [visible|hidden]
114
     * @return bool
115
     */
116
    protected function isListedAs($visibility, $action = null)
117
    {
118
        if (! $action) $action = Artificer::getCurrentAction();
119
120
        $listOptions = Artificer::getModel()->getOption($action);
121
122
        if (! $listOptions || ! isset($listOptions[$visibility])) return false;
123
124
        $list = $listOptions[$visibility];
125
126
        if ($this->isAll($list)) return true;
127
128
        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...
129
    }
130
131
    /**
132
     * Hidden fields have preference.
133
     *
134
     * @return bool
135
     */
136
    public function isVisible()
137
    {
138
        if ($this->isHidden()) return false;
139
140
        return $this->isListedAs('visible');
141
    }
142
143
    /**
144
     * @param $value
145
     * @param $array
146
     * @return bool
147
     */
148
    public function isInArray($value, $array)
149
    {
150
        return (is_array($array) && in_array($value, $array)) ? true : false;
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function isHidden()
157
    {
158
        return $this->isListedAs('hidden');
159
    }
160
161
    public static function get($name)
162
    {
163
        return array_get(\View::getShared(), 'fields')[$name];
164
    }
165
166
    public function __get($name) {
167
        $accessor = 'get' . studly_case($name);
168
169
        if (! method_exists($this->field, $accessor)) {
170
            return null;
171
        }
172
173
        return $this->field->$accessor();
174
    }
175
176
    public function __call($method, $args) {
177
        if (! method_exists($this->field, $method)) {
178
            return null;
179
        }
180
181
        return $this->field->$method($args);
182
    }
183
}