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 ( bab703...dad563 )
by Cristian
08:35
created

Buttons::addButtonFromView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 147
    public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true)
26
    {
27 147
        if ($position == false) {
28
            switch ($stack) {
29 147
                case 'line':
30 1
                    $position = 'beginning';
31 1
                    break;
32
33 147
                default:
34 147
                    $position = 'end';
35 147
                    break;
36 147
            }
37 147
        }
38
39 147
        if ($replaceExisting) {
40 147
            $this->removeButton($name, $stack);
41 147
        }
42
43 147
        $button = new CrudButton($stack, $name, $type, $content);
44
        switch ($position) {
45 147
            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 147
            default:
50 147
                $this->buttons->push($button);
51 147
                break;
52 147
        }
53
54 147
        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 147
    public function initButtons()
75
    {
76 147
        $this->buttons = collect();
77
78
        // line stack
79 147
        $this->addButton('line', 'preview', 'view', 'crud::buttons.preview', 'end');
80 147
        $this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
81 147
        $this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end');
82 147
        $this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end');
83
84
        // top stack
85 147
        $this->addButton('top', 'create', 'view', 'crud::buttons.create');
86 147
        $this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
87 147
    }
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 147
    public function removeButton($name, $stack = null)
96
    {
97
        $this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) {
98 147
            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 147
        });
100 147
    }
101
102 1
    public function removeAllButtons()
103
    {
104 1
        $this->buttons = collect([]);
105 1
    }
106
107 2
    public function removeAllButtonsFromStack($stack)
108
    {
109
        $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 147
    public function __construct($stack, $name, $type, $content)
130
    {
131 147
        $this->stack = $stack;
132 147
        $this->name = $name;
133 147
        $this->type = $type;
134 147
        $this->content = $content;
135 147
    }
136
}
137