GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — merge-dev-to-master ( fdc6fe )
by
unknown
11:18
created

SelectAjaxFunctions   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 254
rs 9.6
wmc 35

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getMinSymbols() 0 3 1
A getDependValue() 0 3 1
A setDataDepends() 0 5 2
A setMinSymbols() 0 5 1
A getModelForOptionsCallback() 0 3 1
A getSearch() 0 7 2
A mutateOptions() 0 3 1
A setSearch() 0 5 1
A hasDependKey() 0 3 1
A setAjaxParameters() 0 5 1
A setModelForOptionsCallback() 0 9 3
A setCustomName() 0 5 1
A getDataDepends() 0 3 1
A getCustomName() 0 3 1
A getDataDependsArray() 0 3 1
C getOptions() 0 68 16
1
<?php
2
3
namespace SleepingOwl\Admin\Traits;
4
5
use Illuminate\Support\Arr;
6
use SleepingOwl\Admin\Contracts\Form\FormInterface;
7
use SleepingOwl\Admin\Http\Controllers\FormElementController;
8
use SleepingOwl\Admin\Exceptions\Form\Element\SelectException;
9
10
trait SelectAjaxFunctions
11
{
12
    protected $search_url = null;
13
    protected $search = null;
14
    protected $min_symbols = 3;
15
    protected $default_query_preparer;
16
17
    /**
18
     * @var array
19
     */
20
    protected $params;
21
22
    /**
23
     * @var array
24
     */
25
    protected $dataDepends = [];
26
27
    /**
28
     * @var \Closure|null|mixed
29
     */
30
    protected $modelForOptionsCallback = null;
31
32
    /**
33
     * @var string|\Closure|mixed
34
     */
35
    protected $custom_name = 'custom_name';
36
37
    /**
38
     * @param string|\Closure|mixed $custom_name
39
     * @return $this
40
     */
41
    public function setCustomName($custom_name)
42
    {
43
        $this->custom_name = $custom_name;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return string|\Closure|mixed
50
     */
51
    public function getCustomName()
52
    {
53
        return $this->custom_name;
54
    }
55
56
    /**
57
     * @return null|string
58
     */
59
    public function getSearch()
60
    {
61
        if ($this->search) {
62
            return $this->search;
63
        }
64
65
        return $this->getDisplay();
0 ignored issues
show
Bug introduced by
It seems like getDisplay() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        return $this->/** @scrutinizer ignore-call */ getDisplay();
Loading history...
66
    }
67
68
    /**
69
     * @param $search
70
     * @return $this
71
     */
72
    public function setSearch($search)
73
    {
74
        $this->search = $search;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Get min symbols to search.
81
     * @return int
82
     */
83
    public function getMinSymbols()
84
    {
85
        return $this->min_symbols;
86
    }
87
88
    /**
89
     * Set min symbols to search.
90
     * @param $symbols
91
     * @return $this
92
     */
93
    public function setMinSymbols($symbols)
94
    {
95
        $this->min_symbols = $symbols;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function mutateOptions()
104
    {
105
        return $this->getOptions();
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getOptions()
112
    {
113
        // get model, model configuration interface, model logic
114
        $model = $this->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on SleepingOwl\Admin\Traits\SelectAjaxFunctions. Did you maybe mean getModelForOptionsCallback()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        /** @scrutinizer ignore-call */ 
115
        $model = $this->getModel();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        $section = \AdminSection::getModel($this->getModel());
0 ignored issues
show
Bug introduced by
The type AdminSection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
        $payload = $section->getPayload();
117
        $form_element_controller = new FormElementController();
118
        $form = $form_element_controller->getModelLogicPayload($section, $model->id, $payload);
119
120
        if ($form instanceof FormInterface) {
121
            // if defined: get values of the depends form fields
122
            $depends = json_decode($this->getDataDepends(), true);
123
            if (is_array($depends) && count($depends)) {
124
                $data_depends = [];
125
                foreach ($depends as $depend) {
126
                    $temp_element = $form->getElement($depend);
127
                    $depend_value = null;
128
                    if (mb_strpos($depend, '.')) {
129
                        $parts = explode('.', $depend);
130
                        $fieldName = array_pop($parts);
131
                        $relationName = implode('.', $parts);
132
                        if (! $model->relationLoaded($relationName)) {
133
                            $model->load($relationName);
134
                        }
135
                        $temp = $model;
136
                        $temp_fail = false;
137
                        foreach ($parts as $part) {
138
                            $temp = $temp->{$part};
139
                            if (! $temp) {
140
                                $temp_fail = true;
141
                                break;
142
                            }
143
                        }
144
                        if (! $temp_fail) {
145
                            $depend_value = $temp->{$fieldName};
146
                        }
147
                    } else {
148
                        $depend_value = $model->{$depend};
149
                    }
150
                    if (! $depend_value) {
151
                        $depend_value = $temp_element->getDefaultValue();
0 ignored issues
show
Bug introduced by
The method getDefaultValue() does not exist on SleepingOwl\Admin\Contra...rm\FormElementInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contra...rm\FormElementInterface such as SleepingOwl\Admin\Form\Related\Forms\ManyToMany or SleepingOwl\Admin\Form\Element\NamedFormElement or SleepingOwl\Admin\Form\Related\Forms\ManyToMany. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
                        /** @scrutinizer ignore-call */ 
152
                        $depend_value = $temp_element->getDefaultValue();
Loading history...
152
                    }
153
                    $data_depends[$depend] = $depend_value;
154
                }
155
                $this->setAjaxParameters($data_depends);
156
            }
157
        }
158
159
        // if defined: get model for options via callback
160
        if (is_callable($callback = $callback = $this->getModelForOptionsCallback())) {
0 ignored issues
show
Unused Code introduced by
The assignment to $callback is dead and can be removed.
Loading history...
161
            $result = $callback($this);
162
            if ($result) {
163
                $this->setModelForOptions($result);
0 ignored issues
show
Bug introduced by
The method setModelForOptions() does not exist on SleepingOwl\Admin\Traits\SelectAjaxFunctions. Did you maybe mean setModelForOptionsCallback()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
                $this->/** @scrutinizer ignore-call */ 
164
                       setModelForOptions($result);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
            }
165
        }
166
167
        if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) {
0 ignored issues
show
Bug introduced by
The method getModelForOptions() does not exist on SleepingOwl\Admin\Traits\SelectAjaxFunctions. Did you maybe mean getModelForOptionsCallback()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        if (! is_null($this->/** @scrutinizer ignore-call */ getModelForOptions()) && ! is_null($this->getDisplay())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
            $this->setOptions(
0 ignored issues
show
Bug introduced by
It seems like setOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
            $this->/** @scrutinizer ignore-call */ 
169
                   setOptions(
Loading history...
169
                $this->loadOptions()
0 ignored issues
show
Bug introduced by
It seems like loadOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
                $this->/** @scrutinizer ignore-call */ 
170
                       loadOptions()
Loading history...
170
            );
171
        }
172
173
        $options = Arr::except($this->options, $this->exclude);
174
        if ($this->isSortable()) {
0 ignored issues
show
Bug introduced by
It seems like isSortable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        if ($this->/** @scrutinizer ignore-call */ isSortable()) {
Loading history...
175
            asort($options, $this->getSortableFlags());
0 ignored issues
show
Bug introduced by
It seems like getSortableFlags() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
            asort($options, $this->/** @scrutinizer ignore-call */ getSortableFlags());
Loading history...
176
        }
177
178
        return $options;
179
    }
180
181
    /**
182
     * @param string $key
183
     *
184
     * @return bool
185
     */
186
    public function hasDependKey($key)
187
    {
188
        return Arr::has($this->params, $key);
189
    }
190
191
    /**
192
     * @param string $key
193
     *
194
     * @return mixed
195
     */
196
    public function getDependValue($key)
197
    {
198
        return Arr::get($this->params, $key, $this->getModel()->getAttribute($key));
199
    }
200
201
    /**
202
     * @return string Json
203
     */
204
    public function getDataDepends()
205
    {
206
        return json_encode($this->dataDepends);
207
    }
208
209
    /**
210
     * @return array
211
     */
212
    public function getDataDependsArray()
213
    {
214
        return $this->dataDepends;
215
    }
216
217
    /**
218
     * @param array|string $depends
219
     *
220
     * @return $this
221
     */
222
    public function setDataDepends($depends)
223
    {
224
        $this->dataDepends = is_array($depends) ? $depends : func_get_args();
225
226
        return $this;
227
    }
228
229
    /**
230
     * @param array $params
231
     *
232
     * @return $this
233
     */
234
    public function setAjaxParameters(array $params)
235
    {
236
        $this->params = $params;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @return \Closure
243
     */
244
    public function getModelForOptionsCallback()
245
    {
246
        return $this->modelForOptionsCallback;
247
    }
248
249
    /**
250
     * @param \Closure|null|mixed $modelForOptionsCallback
251
     *
252
     * @return $this
253
     * @throws SelectException
254
     */
255
    public function setModelForOptionsCallback($modelForOptionsCallback)
256
    {
257
        if (! is_callable($modelForOptionsCallback) && ! is_null($modelForOptionsCallback)) {
258
            throw new SelectException('Option for setModelForOptionsCallback must be Closure or null');
259
        }
260
261
        $this->modelForOptionsCallback = $modelForOptionsCallback;
262
263
        return $this;
264
    }
265
}
266