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
Push — master ( 0be2ba...247314 )
by Cristian
04:16
created

Create::getRelationFields()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.0231

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 14
nop 1
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
crap 8.0231
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait Create
6
{
7
    /*
8
    |--------------------------------------------------------------------------
9
    |                                   CREATE
10
    |--------------------------------------------------------------------------
11
    */
12
13
    /**
14
     * Insert a row in the database.
15
     *
16
     * @param  [Request] All input values to be inserted.
17
     *
18
     * @return [Eloquent Collection]
0 ignored issues
show
Documentation introduced by
The doc-type Eloquent">[Eloquent could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
19
     */
20 4
    public function create($data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
21
    {
22 4
        $data = $this->decodeJsonCastedAttributes($data, 'create');
0 ignored issues
show
Bug introduced by
It seems like decodeJsonCastedAttributes() 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...
23 4
        $data = $this->compactFakeFields($data, 'create');
0 ignored issues
show
Bug introduced by
It seems like compactFakeFields() 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
        // ommit the n-n relationships when updating the eloquent item
26 4
        $nn_relationships = array_pluck($this->getRelationFieldsWithPivot('create'), 'name');
27 4
        $item = $this->model->create(array_except($data, $nn_relationships));
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...
28
29
        // if there are any relationships available, also sync those
30 4
        $this->syncPivot($item, $data);
31
32 4
        return $item;
33
    }
34
35
    /**
36
     * Get all fields needed for the ADD NEW ENTRY form.
37
     *
38
     * @return [array] The fields with attributes and fake attributes.
0 ignored issues
show
Documentation introduced by
The doc-type [array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
39
     */
40 22
    public function getCreateFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
41
    {
42 22
        return $this->create_fields;
0 ignored issues
show
Bug introduced by
The property create_fields 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...
43
    }
44
45
    /**
46
     * Get all fields with relation set (model key set on field).
47
     *
48
     * @param [string: create/update/both]
49
     *
50
     * @return [array] The fields with model key set.
0 ignored issues
show
Documentation introduced by
The doc-type [array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
51
     */
52 15
    public function getRelationFields($form = 'create')
53
    {
54 15
        if ($form == 'create') {
55 13
            $fields = $this->create_fields;
56
        } else {
57 2
            $fields = $this->update_fields;
0 ignored issues
show
Bug introduced by
The property update_fields 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...
58
        }
59
60 15
        $relationFields = [];
61
62 15
        foreach ($fields as $field) {
63 14
            if (isset($field['model'])) {
64 8
                array_push($relationFields, $field);
65
            }
66
67 14
            if (isset($field['subfields']) &&
68 14
                is_array($field['subfields']) &&
69 14
                count($field['subfields'])) {
70
                foreach ($field['subfields'] as $subfield) {
71 14
                    array_push($relationFields, $subfield);
72
                }
73
            }
74
        }
75
76 15
        return $relationFields;
77
    }
78
79
    /**
80
     * Get all fields with n-n relation set (pivot table is true).
81
     *
82
     * @param [string: create/update/both]
83
     *
84
     * @return [array] The fields with n-n relationships.
0 ignored issues
show
Documentation introduced by
The doc-type [array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
85
     */
86 7
    public function getRelationFieldsWithPivot($form = 'create')
87
    {
88 7
        $all_relation_fields = $this->getRelationFields($form);
89
90 7
        return array_where($all_relation_fields, function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91 3
            return isset($value['pivot']) && $value['pivot'];
92 7
        });
93
    }
94
95 8
    public function syncPivot($model, $data, $form = 'create')
96
    {
97 8
        $fields_with_relationships = $this->getRelationFields($form);
98
99 8
        foreach ($fields_with_relationships as $key => $field) {
100 4
            if (isset($field['pivot']) && $field['pivot']) {
101 3
                $values = isset($data[$field['name']]) ? $data[$field['name']] : [];
102 3
                $model->{$field['name']}()->sync($values);
103
104 2
                if (isset($field['pivotFields'])) {
105
                    foreach ($field['pivotFields'] as $pivotField) {
106
                        foreach ($data[$pivotField] as $pivot_id => $pivot_field) {
107
                            $model->{$field['name']}()->updateExistingPivot($pivot_id, [$pivotField => $pivot_field]);
108
                        }
109
                    }
110
                }
111
            }
112
113 3
            if (isset($field['morph']) && $field['morph'] && isset($data[$field['name']])) {
114
                $values = $data[$field['name']];
115 3
                $model->{$field['name']}()->sync($values);
116
            }
117
        }
118 7
    }
119
}
120