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 (#487)
by Julien
03:21
created

Boxes::boxExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait Boxes
6
{
7
    public $sideBoxesEnabled = false;
8
    public $boxesOptions = [];
9
10
    public function enableSideBoxes()
11
    {
12
        $this->sideBoxesEnabled = true;
13
14
        return $this->sideBoxesEnabled;
15
    }
16
17
    public function disableSideBoxes()
18
    {
19
        $this->sideBoxesEnabled = false;
20
21
        return $this->sideBoxesEnabled;
22
    }
23
24
    public function sideBoxesEnabled()
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...
25
    {
26
        return $this->sideBoxesEnabled;
27
    }
28
29
    public function sideBoxesDisabled()
30
    {
31
        return ! $this->sideBoxesEnabled;
32
    }
33
34
    public function boxExists($label)
35
    {
36
        $boxes = $this->getBoxes();
37
38
        return in_array($label, $boxes);
39
    }
40
41
    public function getLastBox()
42
    {
43
        $boxes = $this->getBoxes();
44
45
        if (count($boxes)) {
46
            return last($boxes);
47
        }
48
49
        return false;
50
    }
51
52
    public function isLastBox($label)
53
    {
54
        return $this->getLastBox() == $label;
55
    }
56
57
    public function getBoxFields($label)
58
    {
59
        if ($this->boxExists($label)) {
60
            $all_fields = $this->getCurrentFields();
0 ignored issues
show
Bug introduced by
It seems like getCurrentFields() 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...
61
62
            $fields_for_current_box = collect($all_fields)->filter(function ($value, $key) use ($label) {
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...
63
                return isset($value['box']) && $value['box'] == $label;
64
            });
65
66 View Code Duplication
            if ($this->isLastBox($label)) {
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...
67
                $fields_without_a_box = collect($all_fields)->filter(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...
68
                    return ! isset($value['box']);
69
                });
70
71
                $fields_for_current_box = $fields_for_current_box->merge($fields_without_a_box);
72
            }
73
74
            return $fields_for_current_box;
75
        }
76
77
        return [];
78
    }
79
80
    public function setBoxOptions($label, array $options)
81
    {
82
        $this->boxesOptions[$label] = $options;
83
84
        // if a "side" box was mentioned, we should enable it
85
        if (! empty($options['side'])) {
86
            $this->enableSideBoxes();
87
        }
88
    }
89
90
    public function getBoxOptions($label)
91
    {
92
        $boxOptions = [
93
            'side' => false,
94
            'class' => '',
95
            'collapsed' => false,
96
        ];
97
98
        if (isset($this->boxesOptions[$label])) {
99
            $boxOptions = array_merge($boxOptions, $this->boxesOptions[$label]);
100
        }
101
102
        if ($boxOptions['collapsed']) {
103
            $boxOptions['class'] = trim($boxOptions['class'].' collapsed-box');
104
        }
105
106
        return $boxOptions;
107
    }
108
109
    public function getBoxes($columnFilter = null)
110
    {
111
        $boxes = [];
112
        $fields = $this->getCurrentFields();
0 ignored issues
show
Bug introduced by
It seems like getCurrentFields() 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...
113
114
        $fields_with_boxes = collect($fields)
0 ignored issues
show
Unused Code introduced by
$fields_with_boxes 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...
115
            ->filter(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...
116
                return isset($value['box']);
117
            })
118
            ->each(function ($value, $key) use (&$boxes, $columnFilter) {
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...
119
                if (! isset($columnFilter) || ($columnFilter === 'side' xor ! $this->getBoxOptions($value['box'])['side'])) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 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...
120
                    if (! in_array($value['box'], $boxes)) {
121
                        $boxes[] = $value['box'];
122
                    }
123
                }
124
            });
125
126
        // Default Box
127
        if (empty($boxes) && $columnFilter !== 'side') {
128
            $boxes[] = ucfirst($this->entity_name);
0 ignored issues
show
Bug introduced by
The property entity_name 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...
129
        }
130
131
        return $boxes;
132
    }
133
}
134