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 (#1485)
by Clayton
03:17
created

Buttons::addButton()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 6
dl 0
loc 31
rs 9.1128
c 0
b 0
f 0
ccs 16
cts 16
cp 1
crap 5
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
     * Remove a button from the CRUD panel.
91
     *
92
     * @param string $name Button name.
93
     * @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...
94
     */
95
    public function removeButton($name, $stack = null)
96
    {
97 159
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
98 159
            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...
99 159
        });
100 159
    }
101
102 1
    public function removeAllButtons()
103
    {
104 1
        $this->buttons = collect([]);
105 1
    }
106
107
    public function removeAllButtonsFromStack($stack)
108
    {
109 2
        $this->buttons = $this->buttons->reject(function ($button) use ($stack) {
110 2
            return $button->stack == $stack;
111 2
        });
112 2
    }
113
114
    public function removeButtonFromStack($name, $stack)
115
    {
116 3
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
117 3
            return $button->name == $name && $button->stack == $stack;
118 3
        });
119 3
    }
120
}
121
122
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...
123
{
124
    public $stack;
125
    public $name;
126
    public $type = 'view';
127
    public $content;
128
129 159
    public function __construct($stack, $name, $type, $content)
130
    {
131 159
        $this->stack = $stack;
132 159
        $this->name = $name;
133 159
        $this->type = $type;
134 159
        $this->content = $content;
135 159
    }
136
}
137