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

FakeFields::compactFakeFields()   F
last analyzed

Complexity

Conditions 23
Paths 576

Size

Total Lines 60
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 29
nc 576
nop 3
dl 0
loc 60
rs 0.5888
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 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  string|bool|Model  $model
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\CrudPanel\Traits\Model 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...
17
     * @param  array  $fields  The fields that should be compacted.
18
     * @return array The updated request input.
19
     */
20
    public function compactFakeFields($requestInput, $model = false, $fields = [])
21
    {
22
        // get the right fields according to the form type (create/update)
23
        if (empty($fields)) {
24
            $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

24
            /** @scrutinizer ignore-call */ 
25
            $fields = $this->fields();
Loading history...
25
        }
26
27
        $model = $model ? (is_string($model) ? app($model) : $model) : $this->model;
28
29
        $compactedFakeFields = [];
30
31
        foreach ($fields as $field) {
32
            // compact fake fields
33
            // explode the comma delimited names, eg: start,end in a date_range:
34
            $field['name'] = explode(',', $field['name']);
35
            foreach ($field['name'] as $fieldName) {
36
                $fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras';
37
                $isFakeField = $field['fake'] ?? false;
38
39
                // field is represented by the subfields
40
                if (isset($field['subfields']) && isset($field['model']) && $field['model'] === get_class($model)) {
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type true; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

40
                if (isset($field['subfields']) && isset($field['model']) && $field['model'] === get_class(/** @scrutinizer ignore-type */ $model)) {
Loading history...
41
                    foreach ($field['subfields'] as $subfield) {
42
                        // explode the comma delimited names, eg: start,end in a date_range:
43
                        $subfield['name'] = explode(',', $subfield['name']);
44
                        foreach ($subfield['name'] as $subfieldName) {
45
                            $subfieldName = Str::afterLast($subfieldName, '.');
46
                            $isSubfieldFake = $subfield['fake'] ?? false;
47
                            $subFakeFieldKey = isset($subfield['store_in']) ? $subfield['store_in'] : 'extras';
48
49
                            if (array_key_exists($subfieldName, $requestInput) && $isSubfieldFake) {
50
                                $this->addCompactedField($requestInput, $subfieldName, $subFakeFieldKey);
51
52
                                if (! in_array($subFakeFieldKey, $compactedFakeFields)) {
53
                                    $compactedFakeFields[] = $subFakeFieldKey;
54
                                }
55
                            }
56
                        }
57
                    }
58
                    continue;
59
                }
60
61
                if (array_key_exists($fieldName, $requestInput) && $isFakeField) {
62
                    $this->addCompactedField($requestInput, $fieldName, $fakeFieldKey);
63
64
                    if (! in_array($fakeFieldKey, $compactedFakeFields)) {
65
                        $compactedFakeFields[] = $fakeFieldKey;
66
                    }
67
                }
68
            }
69
        }
70
71
        // json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted
72
        foreach ($compactedFakeFields as $value) {
73
            if (! (property_exists($model, 'translatable') && in_array($value, $model->getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) {
74
                $requestInput[$value] = json_encode($requestInput[$value]);
75
            }
76
        }
77
78
        // if there are no fake fields defined, return the original request input
79
        return $requestInput;
80
    }
81
82
    /**
83
     * Compact a fake field in the request input array.
84
     *
85
     * @param  array  $requestInput  The request input.
86
     * @param  string  $fakeFieldName  The fake field name.
87
     * @param  string  $fakeFieldKey  The fake field key.
88
     */
89
    private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey)
90
    {
91
        $fakeField = $requestInput[$fakeFieldName];
92
        Arr::pull($requestInput, $fakeFieldName); // remove the fake field from the request
93
94
        $requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField;
95
    }
96
}
97