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 (#1505)
by Oliver
05:09
created

CrudButton   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 10
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
1
<?php
2
3
namespace Backpack\CRUD\PanelTraits;
4
5
trait Buttons
6
{
7
    // ------------
8
    // BUTTONS
9
    // ------------
10
11
    // TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
12
13
    /**
14
     * Add a button to the CRUD table view.
15
     *
16
     * @param string $stack Where should the button be visible? Options: top, line, bottom.
17
     * @param string $name The name of the button. Unique.
18
     * @param string $type Type of button: view or model_function.
19
     * @param string $content The HTML for the button.
20
     * @param bool|string $position Position on the stack: beginning or end. If false, the position will be
21
     *                                 'beginning' for the line stack or 'end' otherwise.
22
     * @param bool $replaceExisting True if a button with the same name on the given stack should be replaced.
23
     * @return \Backpack\CRUD\PanelTraits\CrudButton The new CRUD button.
24
     */
25 159
    public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true)
26
    {
27 159
        if ($position == false) {
28
            switch ($stack) {
29 159
                case 'line':
30 1
                    $position = 'beginning';
31 1
                    break;
32
33
                default:
34 159
                    $position = 'end';
35 159
                    break;
36
            }
37
        }
38
39 159
        if ($replaceExisting) {
40 159
            $this->removeButton($name, $stack);
41
        }
42
43 159
        $button = new CrudButton($stack, $name, $type, $content);
44
        switch ($position) {
45 159
            case 'beginning':
46 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...
47 2
                break;
48
49
            default:
50 159
                $this->buttons->push($button);
51 159
                break;
52
        }
53
54 159
        return $button;
55
    }
56
57
    public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $model_function_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
58
    {
59
        $this->addButton($stack, $name, 'model_function', $model_function_name, $position);
60
    }
61
62 1
    public function addButtonFromView($stack, $name, $view, $position = false)
63
    {
64 1
        $view = 'vendor.backpack.crud.buttons.'.$view;
65
66 1
        $this->addButton($stack, $name, 'view', $view, $position);
67 1
    }
68
69
    public function buttons()
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...
70
    {
71
        return $this->buttons;
72
    }
73
74 159
    public function initButtons()
75
    {
76 159
        $this->buttons = collect();
77
78
        // line stack
79 159
        $this->addButton('line', 'preview', 'view', 'crud::buttons.preview', 'end');
80 159
        $this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
81 159
        $this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end');
82 159
        $this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
83
84
        // top stack
85 159
        $this->addButton('top', 'create', 'view', 'crud::buttons.create');
86 159
        $this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
87 159
    }
88
89
    /**
90
     * Modify the attributes of a button.
91
     *
92
     * @param  string $name          The button name.
93
     * @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...
94
     * @return button                The button that has suffered the changes, for daisychaining methods.
95
     */
96 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...
97 159
    {
98 159
        $button = $this->buttons()->firstWhere('name', $name);
99 159
100 159
        if (! $button) {
101
            abort(500, 'CRUD Button "'.$name.'" not found. Please check the button exists before you modify it.');
102 1
        }
103
104 1
        if (is_array($modifications)) {
105 1
            foreach ($modifications as $key => $value) {
106
                $button->{$key} = $value;
107
            }
108
        }
109 2
110 2
        return $button;
111 2
    }
112 2
113
    /**
114
     * Remove a button from the CRUD panel.
115
     *
116 3
     * @param string $name Button name.
117 3
     * @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...
118 3
     */
119 3
    public function removeButton($name, $stack = null)
120
    {
121
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
122
            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...
123
        });
124
    }
125
126
    public function removeAllButtons()
127
    {
128
        $this->buttons = collect([]);
129 159
    }
130
131 159
    public function removeAllButtonsFromStack($stack)
132 159
    {
133 159
        $this->buttons = $this->buttons->reject(function ($button) use ($stack) {
134 159
            return $button->stack == $stack;
135 159
        });
136
    }
137
138
    public function removeButtonFromStack($name, $stack)
139
    {
140
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
141
            return $button->name == $name && $button->stack == $stack;
142
        });
143
    }
144
}
145
146
class CrudButton
0 ignored issues
show
Coding Style Compatibility introduced by
Each trait must be in a file by itself

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
147
{
148
    public $stack;
149
    public $name;
150
    public $type = 'view';
151
    public $content;
152
153
    public function __construct($stack, $name, $type, $content)
154
    {
155
        $this->stack = $stack;
156
        $this->name = $name;
157
        $this->type = $type;
158
        $this->content = $content;
159
    }
160
}
161