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 (#2073)
by Tomoya
03:38
created

Buttons::buttons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
use Illuminate\Support\Collection;
6
7
trait Buttons
8
{
9
    // ------------
10
    // BUTTONS
11
    // ------------
12
13
    // TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']);
14
15
    /**
16
     * Add a button to the CRUD table view.
17
     *
18
     * @param string      $stack           Where should the button be visible? Options: top, line, bottom.
19
     * @param string      $name            The name of the button. Unique.
20
     * @param string      $type            Type of button: view or model_function.
21
     * @param string      $content         The HTML for the button.
22
     * @param bool|string $position        Position on the stack: beginning or end. If false, the position will be
23
     *                                     'beginning' for the line stack or 'end' otherwise.
24
     * @param bool        $replaceExisting True if a button with the same name on the given stack should be replaced.
25
     *
26
     * @return \Backpack\CRUD\PanelTraits\CrudButton The new CRUD button.
27
     */
28 203
    public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true)
29
    {
30 203
        if ($position == false) {
31 203
            switch ($stack) {
32 203
                case 'line':
33 1
                    $position = 'beginning';
34 1
                    break;
35
36
                default:
37 203
                    $position = 'end';
38 203
                    break;
39
            }
40
        }
41
42 203
        if ($replaceExisting) {
43 203
            $this->removeButton($name, $stack);
44
        }
45
46 203
        $button = new CrudButton($stack, $name, $type, $content);
47 203
        switch ($position) {
48 203
            case 'beginning':
49 2
                $this->buttons->prepend($button);
0 ignored issues
show
Bug introduced by
The property buttons 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...
50 2
                break;
51
52
            default:
53 203
                $this->buttons->push($button);
54 203
                break;
55
        }
56
57 203
        return $button;
58
    }
59
60
    public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false)
61
    {
62
        $this->addButton($stack, $name, 'model_function', $model_function_name, $position);
63
    }
64
65 1
    public function addButtonFromView($stack, $name, $view, $position = false)
66
    {
67 1
        $view = 'crud::buttons.'.$view;
68
69 1
        $this->addButton($stack, $name, 'view', $view, $position);
70 1
    }
71
72
    /**
73
     * @return Collection
74
     */
75
    public function buttons()
76
    {
77
        return $this->buttons;
78
    }
79
80 203
    public function initButtons()
81
    {
82 203
        $this->buttons = collect();
83
84
        // line stack
85 203
        $this->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
86 203
        $this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
87 203
        $this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end');
88 203
        $this->addButton('line', 'clone', 'view', 'crud::buttons.clone', 'end');
89 203
        $this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
90
91
        // top stack
92 203
        $this->addButton('top', 'create', 'view', 'crud::buttons.create');
93 203
        $this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
94 203
    }
95
96
    /**
97
     * Modify the attributes of a button.
98
     *
99
     * @param  string $name          The button name.
100
     * @param  array  $modifications The attributes and their new values.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $modifications not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
101
     *
102
     * @return CrudButton                The button that has suffered the changes, for daisychaining methods.
103
     */
104 View Code Duplication
    public function modifyButton($name, $modifications = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
105
    {
106
        /**
107
         * @var CrudButton|null
108
         */
109
        $button = $this->buttons()->firstWhere('name', $name);
110
111
        if (! $button) {
112
            abort(500, 'CRUD Button "'.$name.'" not found. Please check the button exists before you modify it.');
113
        }
114
115
        if (is_array($modifications)) {
116
            foreach ($modifications as $key => $value) {
117
                $button->{$key} = $value;
118
            }
119
        }
120
121
        return $button;
122
    }
123
124
    /**
125
     * Remove a button from the CRUD panel.
126
     *
127
     * @param string $name  Button name.
128
     * @param string $stack Optional stack name.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $stack not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
129
     */
130 203
    public function removeButton($name, $stack = null)
131
    {
132
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
133 203
            return $stack == null ? $button->name == $name : ($button->stack == $stack) && ($button->name == $name);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $stack of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
134 203
        });
135 203
    }
136
137
    /**
138
     * @param array $names Button names
139
     * @param string|null $stack Optional stack name.
140
     */
141 2
    public function removeButtons($names, $stack = null)
142
    {
143 2
        if (! empty($names)) {
144 2
            foreach ($names as $name) {
145 2
                $this->removeButton($name, $stack);
146
            }
147
        }
148 2
    }
149
150 1
    public function removeAllButtons()
151
    {
152 1
        $this->buttons = collect([]);
153 1
    }
154
155 2
    public function removeAllButtonsFromStack($stack)
156
    {
157
        $this->buttons = $this->buttons->reject(function ($button) use ($stack) {
158 2
            return $button->stack == $stack;
159 2
        });
160 2
    }
161
162 3
    public function removeButtonFromStack($name, $stack)
163
    {
164
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
165 3
            return $button->name == $name && $button->stack == $stack;
166 3
        });
167 3
    }
168
}
169
170
class CrudButton
171
{
172
    public $stack;
173
    public $name;
174
    public $type = 'view';
175
    public $content;
176
177 203
    public function __construct($stack, $name, $type, $content)
178
    {
179 203
        $this->stack = $stack;
180 203
        $this->name = $name;
181 203
        $this->type = $type;
182 203
        $this->content = $content;
183 203
    }
184
}
185