Completed
Pull Request — master (#2905)
by
unknown
02:26
created

Embeds::getValidationAttributes()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 86

Duplication

Lines 33
Ratio 38.37 %

Importance

Changes 0
Metric Value
cc 13
nc 9
nop 1
dl 33
loc 86
rs 5.5987
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\EmbeddedForm;
6
use Encore\Admin\Form\Field;
7
use Illuminate\Support\Facades\Validator;
8
9
class Embeds extends Field
10
{
11
    /**
12
     * @var \Closure
13
     */
14
    protected $builder = null;
15
    protected $view = 'admin::form.embeds';
16
17
    /**
18
     * Create a new HasMany field instance.
19
     *
20
     * @param string $column
21
     * @param array  $arguments
22
     */
23 View Code Duplication
    public function __construct($column, $arguments = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $this->column = $column;
26
27
        if (count($arguments) == 1) {
28
            $this->label = $this->formatLabel();
29
            $this->builder = $arguments[0];
30
        }
31
32
        if (count($arguments) == 2) {
33
            list($this->label, $this->builder) = $arguments;
34
        }
35
    }
36
37
    /**
38
     * Prepare input data for insert or update.
39
     *
40
     * @param array $input
41
     *
42
     * @return array
43
     */
44
    public function prepare($input)
45
    {
46
        $form = $this->buildEmbeddedForm();
47
48
        return $form->setOriginal($this->original)->prepare($input);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getValidationRules(array $input)
55
    {
56
        if (!array_key_exists($this->column, $input)) {
57
            return false;
58
        }
59
60
        $input = array_only($input, $this->column);
61
        $rules = $attributes = $messages = $newInputs = [];
0 ignored issues
show
Unused Code introduced by
$newInputs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$messages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$attributes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
        $rel = $this->column;
63
        $availInput = $input;
64 View Code Duplication
        $array_key_attach_str = function (array $a, string $b, string $c = '.') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Unused Code introduced by
$array_key_attach_str is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
            return call_user_func_array(
66
                'array_merge',
67
                array_map(function ($u, $v) use ($b, $c) {
68
                    return ["{$b}{$c}{$u}" => $v];
69
                }, array_keys($a), array_values($a))
70
            );
71
        };
72
73
        $array_clean_merge = function (array $a, $b) {
74
            return array_merge($a, call_user_func_array('array_merge', $b));
75
        };
76
77
        /** @var Field $field */
78
        foreach ($this->buildEmbeddedForm()->fields() as $field) {
79
            if (!$fieldRules = $field->getRules()) {
80
                continue;
81
            }
82
83
            $column = $field->column();
84
            $columns = is_array($column) ? $column : [$column];
85 View Code Duplication
            if ($field instanceof Field\MultipleSelect || $field instanceof Field\Listbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                $availInput[$column] = array_filter($availInput[$column], 'strlen');
87
                $availInput[$column] = $availInput[$column] ?: null;
88
            }
89
            /*
90
             *
91
             * For single column field format rules to:
92
             * [
93
             *     'extra.name' => 'required'
94
             *     'extra.email' => 'required'
95
             * ]
96
             *
97
             * For multiple column field with rules like 'required':
98
             * 'extra' => [
99
             *     'start' => 'start_at'
100
             *     'end'   => 'end_at',
101
             * ]
102
             *
103
             * format rules to:
104
             * [
105
             *     'extra.start_atstart' => 'required'
106
             *     'extra.end_atend' => 'required'
107
             * ]
108
             */
109
            $newColumn = array_map(function ($k, $v) use ($rel) {
110
                //Fix ResetInput Function! A Headache Implementation!
111
                return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}";
112
            }, array_keys($columns), array_values($columns));
113
114
            $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules;
115 View Code Duplication
            $newRules = array_map(function ($v) use ($fieldRules, $availInput) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
                list($k, $c) = explode('.', $v);
117
                //Fix ResetInput Function! A Headache Implementation!
118
                $col = explode(':', $c)[0];
119
120
                if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) {
121
                    return $array_key_attach_str(preg_replace('/./', $fieldRules, $availInput[$k][$col]), $v, ':');
0 ignored issues
show
Bug introduced by
The variable $array_key_attach_str does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
122
                }
123
124
                //May Have Problem in Dealing with File Upload in Edit Mode
125
                return [$v => $fieldRules];
126
            }, $newColumn);
127
            $rules = $array_clean_merge($rules, $newRules);
128
        }
129
130
        return $rules;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getValidationAttributes(array $input)
137
    {
138
        if (!array_key_exists($this->column, $input)) {
139
            return false;
140
        }
141
142
        $input = array_only($input, $this->column);
143
        $rules = $attributes = $messages = $newInputs = [];
0 ignored issues
show
Unused Code introduced by
$newInputs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$messages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$rules is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
        $rel = $this->column;
145
        $availInput = $input;
146 View Code Duplication
        $array_key_attach_str = function (array $a, string $b, string $c = '.') {
0 ignored issues
show
Unused Code introduced by
$array_key_attach_str is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
            return call_user_func_array(
148
                'array_merge',
149
                array_map(function ($u, $v) use ($b, $c) {
150
                    return ["{$b}{$c}{$u}" => $v];
151
                }, array_keys($a), array_values($a))
152
            );
153
        };
154
155
        $array_clean_merge = function (array $a, $b) {
156
            return array_merge($a, call_user_func_array('array_merge', $b));
157
        };
158
159
        /** @var Field $field */
160
        foreach ($this->buildEmbeddedForm()->fields() as $field) {
161
            if (!$fieldRules = $field->getRules()) {
162
                continue;
163
            }
164
165
            $column = $field->column();
166
            $columns = is_array($column) ? $column : [$column];
167 View Code Duplication
            if ($field instanceof Field\MultipleSelect || $field instanceof Field\Listbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
                $availInput[$column] = array_filter($availInput[$column], 'strlen');
169
                $availInput[$column] = $availInput[$column] ?: null;
170
            }
171
            /*
172
             *
173
             * For single column field format rules to:
174
             * [
175
             *     'extra.name' => 'required'
176
             *     'extra.email' => 'required'
177
             * ]
178
             *
179
             * For multiple column field with rules like 'required':
180
             * 'extra' => [
181
             *     'start' => 'start_at'
182
             *     'end'   => 'end_at',
183
             * ]
184
             *
185
             * format rules to:
186
             * [
187
             *     'extra.start_atstart' => 'required'
188
             *     'extra.end_atend' => 'required'
189
             * ]
190
             */
191
            $newColumn = array_map(function ($k, $v) use ($rel) {
192
                //Fix ResetInput Function! A Headache Implementation!
193
                return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}";
194
            }, array_keys($columns), array_values($columns));
195
196 View Code Duplication
            $newAttributes = array_map(function ($v) use ($field, $availInput) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
                list($k, $c) = explode('.', $v);
198
                //Fix ResetInput Function! A Headache Implementation!
199
                $col = explode(':', $c)[0];
200
                if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) {
201
                    return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field) {
202
                        $w = $field->label();
203
                        //Fix ResetInput Function! A Headache Implementation!
204
                        $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : '';
205
206
                        return ["{$v}:{$u}" => $w];
207
                    }, array_keys($availInput[$k][$col])));
208
                }
209
210
                //May Have Problem in Dealing with File Upload in Edit Mode
211
                $w = $field->label();
212
                //Fix ResetInput Function! A Headache Implementation!
213
                $w .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[2])[0].']' : '';
214
215
                return [$v => $w];
216
            }, $newColumn);
217
            $attributes = $array_clean_merge($attributes, $newAttributes);
218
        }
219
220
        return $attributes;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function getValidationInput(array $input)
227
    {
228
        if (!array_key_exists($this->column, $input)) {
229
            return false;
230
        }
231
232
        $input = array_only($input, $this->column);
233
        $rules = $attributes = $messages = $newInputs = [];
0 ignored issues
show
Unused Code introduced by
$messages is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$attributes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$rules is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
234
        $rel = $this->column;
235
        $availInput = $input;
236 View Code Duplication
        $array_key_attach_str = function (array $a, string $b, string $c = '.') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
            return call_user_func_array(
238
                'array_merge',
239
                array_map(function ($u, $v) use ($b, $c) {
240
                    return ["{$b}{$c}{$u}" => $v];
241
                }, array_keys($a), array_values($a))
242
            );
243
        };
244
245
        $array_clean_merge = function (array $a, $b) {
246
            return array_merge($a, call_user_func_array('array_merge', $b));
247
        };
248
249
        /** @var Field $field */
250
        foreach ($this->buildEmbeddedForm()->fields() as $field) {
251
            if (!$fieldRules = $field->getRules()) {
252
                continue;
253
            }
254
255
            $column = $field->column();
256
            $columns = is_array($column) ? $column : [$column];
257 View Code Duplication
            if ($field instanceof Field\MultipleSelect || $field instanceof Field\Listbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
                $availInput[$column] = array_filter($availInput[$column], 'strlen');
259
                $availInput[$column] = $availInput[$column] ?: null;
260
            }
261
            /*
262
             *
263
             * For single column field format rules to:
264
             * [
265
             *     'extra.name' => 'required'
266
             *     'extra.email' => 'required'
267
             * ]
268
             *
269
             * For multiple column field with rules like 'required':
270
             * 'extra' => [
271
             *     'start' => 'start_at'
272
             *     'end'   => 'end_at',
273
             * ]
274
             *
275
             * format rules to:
276
             * [
277
             *     'extra.start_atstart' => 'required'
278
             *     'extra.end_atend' => 'required'
279
             * ]
280
             */
281
            $newColumn = array_map(function ($k, $v) use ($rel) {
282
                //Fix ResetInput Function! A Headache Implementation!
283
                return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}";
284
            }, array_keys($columns), array_values($columns));
285
286 View Code Duplication
            $newInput = array_map(function ($v) use ($availInput, $array_key_attach_str) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
                list($k, $c) = explode('.', $v);
288
                //Fix ResetInput Function! A Headache Implementation!
289
                $col = explode(':', $c)[0];
290
                if (!array_key_exists($col, $availInput[$k])) {
291
                    return [$v => null];
292
                }
293
294
                if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) {
295
                    return $array_key_attach_str($availInput[$k][$col], $v, ':');
296
                }
297
298
                return [$v => $availInput[$k][$col]];
299
            }, $newColumn);
300
            $newInputs = $array_clean_merge($newInputs, $newInput);
301
        }
302
303
        return $newInputs;
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function getValidationMessages(array $input)
310
    {
311
        if (!array_key_exists($this->column, $input)) {
312
            return false;
313
        }
314
315
        $input = array_only($input, $this->column);
316
        $rules = $attributes = $messages = $newInputs = [];
0 ignored issues
show
Unused Code introduced by
$newInputs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$attributes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$rules is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
317
        $rel = $this->column;
318
        $availInput = $input;
319 View Code Duplication
        $array_key_attach_str = function (array $a, string $b, string $c = '.') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320
            return call_user_func_array(
321
                'array_merge',
322
                array_map(function ($u, $v) use ($b, $c) {
323
                    return ["{$b}{$c}{$u}" => $v];
324
                }, array_keys($a), array_values($a))
325
            );
326
        };
327
328
        $array_clean_merge = function (array $a, $b) {
329
            return array_merge($a, call_user_func_array('array_merge', $b));
330
        };
331
332
        /** @var Field $field */
333
        foreach ($this->buildEmbeddedForm()->fields() as $field) {
334
            if (!$fieldRules = $field->getRules()) {
335
                continue;
336
            }
337
338
            $column = $field->column();
339
            $columns = is_array($column) ? $column : [$column];
340 View Code Duplication
            if ($field instanceof Field\MultipleSelect || $field instanceof Field\Listbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
                $availInput[$column] = array_filter($availInput[$column], 'strlen');
342
                $availInput[$column] = $availInput[$column] ?: null;
343
            }
344
            /*
345
             *
346
             * For single column field format rules to:
347
             * [
348
             *     'extra.name' => 'required'
349
             *     'extra.email' => 'required'
350
             * ]
351
             *
352
             * For multiple column field with rules like 'required':
353
             * 'extra' => [
354
             *     'start' => 'start_at'
355
             *     'end'   => 'end_at',
356
             * ]
357
             *
358
             * format rules to:
359
             * [
360
             *     'extra.start_atstart' => 'required'
361
             *     'extra.end_atend' => 'required'
362
             * ]
363
             */
364
            $newColumn = array_map(function ($k, $v) use ($rel) {
365
                //Fix ResetInput Function! A Headache Implementation!
366
                return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}";
367
            }, array_keys($columns), array_values($columns));
368
369 View Code Duplication
            if ($field->validationMessages) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
                $newMessages = array_map(function ($v) use ($field, $availInput, $array_key_attach_str) {
371
                    list($k, $c) = explode('.', $v);
372
                    //Fix ResetInput Function! A Headache Implementation!
373
                    $col = explode(':', $c)[0];
374
                    if (array_key_exists($col, $availInput[$k]) && is_array($availInput[$k][$col])) {
375
                        return call_user_func_array('array_merge', array_map(function ($u) use ($v, $field, $array_key_attach_str) {
376
                            return $array_key_attach_str($field->validationMessages, "{$v}:{$u}");
377
                        }, array_keys($availInput[$k][$col])));
378
                    }
379
380
                    //May Have Problem in Dealing with File Upload in Edit Mode
381
                    return $array_key_attach_str($field->validationMessages, $v);
382
                }, $newColumn);
383
                $messages = $array_clean_merge($messages, $newMessages);
384
            }
385
        }
386
387
        return $messages;
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393
    public function getValidator(array $input)
394
    {
395
        if (!array_key_exists($this->column, $input)) {
396
            return false;
397
        }
398
399
        $rules = $this->getValidationRules($input);
400
        if (empty($rules)) {
401
            return false;
402
        }
403
404 View Code Duplication
        $array_key_clean_undot = function (array $a) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
            $keys = preg_grep('/[\.\:]/', array_keys($a));
406
            if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
407
                foreach ($keys as $key) {
408
                    array_set($a, str_replace(':', '', $key), $a[$key]);
409
                    unset($a[$key]);
410
                }
411
            }
412
413
            return $a;
414
        };
415
416 View Code Duplication
        $array_key_clean = function (array $a) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
417
            $a = count($a) ? call_user_func_array('array_merge', array_map(function ($k, $v) {
418
                return [str_replace(':', '', $k) => $v];
419
            }, array_keys($a), array_values($a))) : $a;
420
421
            return $a;
422
        };
423
424
        $attributes = $this->getValidationAttributes($input);
425
        $messages = $this->getValidationMessages($input);
426
        $newInputs = $this->getValidationInput($input);
427
428
        $input = $array_key_clean_undot(array_filter($newInputs, 'strlen', ARRAY_FILTER_USE_KEY));
429
        $rules = $array_key_clean($rules);
430
        $attributes = $array_key_clean($attributes);
431
        $messages = $array_key_clean($messages);
432
433
        if (empty($input)) {
434
            $input = [$rel => $availInput];
0 ignored issues
show
Bug introduced by
The variable $rel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $availInput does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
435
        }
436
437
        return Validator::make($input, $rules, $messages, $attributes);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Illuminate\Suppo...messages, $attributes); (Illuminate\Contracts\Validation\Validator) is incompatible with the return type of the parent method Encore\Admin\Form\Field::getValidator of type boolean|Illuminate\Support\Facades\Validator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
438
    }
439
440
    /**
441
     * Get data for Embedded form.
442
     *
443
     * Normally, data is obtained from the database.
444
     *
445
     * When the data validation errors, data is obtained from session flash.
446
     *
447
     * @return array
448
     */
449
    protected function getEmbeddedData()
450
    {
451
        if ($old = old($this->column)) {
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, old() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
452
            return $old;
453
        }
454
455
        if (empty($this->value)) {
456
            return [];
457
        }
458
459
        if (is_string($this->value)) {
460
            return json_decode($this->value, true);
461
        }
462
463
        return (array) $this->value;
464
    }
465
466
    /**
467
     * Build a Embedded Form and fill data.
468
     *
469
     * @return EmbeddedForm
470
     */
471
    protected function buildEmbeddedForm()
472
    {
473
        $form = new EmbeddedForm($this->elementName ?: $this->column);
0 ignored issues
show
Bug introduced by
It seems like $this->elementName ?: $this->column can also be of type array; however, Encore\Admin\Form\EmbeddedForm::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
474
475
        $form->setParent($this->form);
476
477
        call_user_func($this->builder, $form);
478
479
        //Fix the Bug of Embeds Fields Cannot be used within HasMany
480
        if ($this->elementName) {
481
            list($rel, $key, $col) = explode('.', $this->errorKey);
482
            $form->fields()->each(function (Field $field) use ($rel,$key,$col) {
483
                $column = $field->column();
484
                $elementName = $elementClass = $errorKey = [];
485
                if (is_array($column)) {
486
                    foreach ($column as $k => $name) {
487
                        $errorKey[$k] = sprintf('%s.%s.%s.%s', $rel, $key, $col, $name);
488
                        $elementName[$k] = sprintf('%s[%s][%s][%s]', $rel, $key, $col, $name);
489
                        $elementClass[$k] = [$rel, $col, $name];
490
                    }
491
                } else {
492
                    $errorKey = sprintf('%s.%s.%s.%s', $rel, $key, $col, $column);
493
                    $elementName = sprintf('%s[%s][%s][%s]', $rel, $key, $col, $column);
494
                    $elementClass = [$rel, $col, $column];
495
                }
496
                $field->setErrorKey($errorKey)
0 ignored issues
show
Bug introduced by
It seems like $errorKey defined by array() on line 484 can also be of type array; however, Encore\Admin\Form\Field::setErrorKey() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
497
                    ->setElementName($elementName)
0 ignored issues
show
Bug introduced by
It seems like $elementName defined by $elementClass = $errorKey = array() on line 484 can also be of type array; however, Encore\Admin\Form\Field::setElementName() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
498
                    ->setElementClass($elementClass);
499
            });
500
        }
501
        $form->fill($this->getEmbeddedData());
502
503
        return $form;
504
    }
505
506
    /**
507
     * Render the form.
508
     *
509
     * @return \Illuminate\View\View
510
     */
511
    public function render()
512
    {
513
        return parent::render()->with(['form' => $this->buildEmbeddedForm()]);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
514
    }
515
}
516