Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Setup Failed
Pull Request — master (#4161)
by
unknown
15:22
created

FakeFields::compactFakeFields()   F

Complexity

Conditions 21
Paths 528

Size

Total Lines 56
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 21
eloc 26
c 1
b 1
f 0
nc 528
nop 3
dl 0
loc 56
rs 0.6555

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 Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
trait FakeFields
9
{
10
    /**
11
     * Update the request input array to something that can be passed to the model's create or update function.
12
     * The resulting array will only include the fields that are stored in the database and their values,
13
     * plus the '_token' and 'redirect_after_save' variables.
14
     *
15
     * @param  array  $requestInput  The request input.
16
     * @param  array  $fields
17
     *
18
     * @see \Illuminate\Http\Request::all() For an example on how to get the request input.
19
     *
20
     * @return array The updated request input.
21
     */
22
    public function compactFakeFields($requestInput, $model = false, $fields = [])
23
    {
24
        // get the right fields according to the form type (create/update)
25
        if (empty($fields)) {
26
            $fields = $this->fields();
0 ignored issues
show
Bug introduced by
It seems like fields() 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

26
            /** @scrutinizer ignore-call */ 
27
            $fields = $this->fields();
Loading history...
27
        }
28
29
        $model = $model ? (is_string($model) ? app($model) : $model) : $this->model;
30
31
        $compactedFakeFields = [];
32
33
        foreach ($fields as $field) {
34
            // compact fake fields
35
            // cast the field name to array first, to account for array field names
36
            // in fields that send multiple inputs and want them all saved to the database
37
            foreach ((array) $field['name'] as $fieldName) {
38
                $fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras';
39
                $isFakeField = $field['fake'] ?? false;
40
41
                // field is represented by the subfields
42
                if (isset($field['subfields']) && $field['model'] === get_class($model)) {
43
                    foreach ($field['subfields'] as $subfield) {
44
                        $subfieldName = Str::afterLast($subfield['name'], '.');
45
                        $isSubfieldFake = $subfield['fake'] ?? false;
46
                        $subFakeFieldKey = isset($subfield['store_in']) ? $subfield['store_in'] : 'extras';
47
48
                        if (array_key_exists($subfieldName, $requestInput) && $isSubfieldFake) {
49
                            $this->addCompactedField($requestInput, $subfieldName, $subFakeFieldKey);
50
51
                            if (! in_array($subFakeFieldKey, $compactedFakeFields)) {
52
                                $compactedFakeFields[] = $subFakeFieldKey;
53
                            }
54
                        }
55
                    }
56
                    continue;
57
                }
58
59
                if (array_key_exists($fieldName, $requestInput) && $isFakeField) {
60
                    $this->addCompactedField($requestInput, $fieldName, $fakeFieldKey);
61
62
                    if (! in_array($fakeFieldKey, $compactedFakeFields)) {
63
                        $compactedFakeFields[] = $fakeFieldKey;
64
                    }
65
                }
66
            }
67
        }
68
69
        // json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted
70
        foreach ($compactedFakeFields as $value) {
71
            if (! (property_exists($model, 'translatable') && in_array($value, $model->getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) {
0 ignored issues
show
Bug introduced by
The method getTranslatableAttributes() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

71
            if (! (property_exists($model, 'translatable') && in_array($value, $model->/** @scrutinizer ignore-call */ getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) {

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...
72
                $requestInput[$value] = json_encode($requestInput[$value]);
73
            }
74
        }
75
76
        // if there are no fake fields defined, return the original request input
77
        return $requestInput;
78
    }
79
80
    /**
81
     * Compact a fake field in the request input array.
82
     *
83
     * @param  array  $requestInput  The request input.
84
     * @param  string  $fakeFieldName  The fake field name.
85
     * @param  string  $fakeFieldKey  The fake field key.
86
     */
87
    private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey)
88
    {
89
        $fakeField = $requestInput[$fakeFieldName];
90
        Arr::pull($requestInput, $fakeFieldName); // remove the fake field from the request
91
92
        $requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField;
93
    }
94
}
95