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

Completed
Pull Request — master (#1116)
by Oliver
04:14
created

FakeFields::addCompactedField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait FakeFields
6
{
7
    /**
8
     * Update the request input array to something that can be passed to the model's create or update function.
9
     * The resulting array will only include the fields that are stored in the database and their values,
10
     * plus the '_token' and 'redirect_after_save' variables.
11
     *
12
     * @param array $requestInput The request input.
13
     * @param string $form The CRUD form. Can be 'create' or 'update' . Default is 'create'.
14
     * @param int|bool $id The CRUD entry id in the case of the 'update' form.
15
     *
16
     * @see \Illuminate\Http\Request::all() For an example on how to get the request input.
17
     *
18
     * @return array The updated request input.
19
     */
20 11
    public function compactFakeFields($requestInput, $form = 'create', $id = false)
21
    {
22
        // get the right fields according to the form type (create/update)
23 11
        $fields = $this->getFields($form, $id);
0 ignored issues
show
Bug introduced by
It seems like getFields() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
25 9
        $compactedFakeFields = [];
26 9
        foreach ($fields as $field) {
27
            // compact fake fields
28 7
            if (isset($field['fake']) && $field['fake'] == true && array_key_exists($field['name'], $requestInput)) {
29 2
                $fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras';
30 2
                $this->addCompactedField($requestInput, $field['name'], $fakeFieldKey);
31
32 2
                if (! in_array($fakeFieldKey, $compactedFakeFields)) {
33 7
                    $compactedFakeFields[] = $fakeFieldKey;
34
                }
35
            }
36
        }
37
38
        // json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
39 9
        foreach ($compactedFakeFields as $value) {
40 2
            if (! (property_exists($this->model, 'translatable') && in_array($value, $this->model->getTranslatableAttributes(), true)) && $this->model->shouldEncodeFake($value)) {
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 179 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41 2
                $requestInput[$value] = json_encode($requestInput[$value]);
42
            }
43
        }
44
45
        // if there are no fake fields defined, return the original request input
46 9
        return $requestInput;
47
    }
48
49
    /**
50
     * Compact a fake field in the request input array.
51
     *
52
     * @param array $requestInput The request input.
53
     * @param string $fakeFieldName The fake field name.
54
     * @param string $fakeFieldKey The fake field key.
55
     */
56 2
    private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey)
57
    {
58 2
        $fakeField = $requestInput[$fakeFieldName];
59 2
        array_pull($requestInput, $fakeFieldName); // remove the fake field from the request
60
61 2
        $requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField;
62 2
    }
63
}
64