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 ( 6bda91...26ffdc )
by Cristian
03:56 queued 24s
created

SaveActions::performSaveAction()   B

Complexity

Conditions 8
Paths 28

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 28
nop 1
dl 0
loc 36
rs 8.0995
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
trait SaveActions
6
{
7
    /**
8
     * Get save actions, with pre-selected action from stored session variable or config fallback.
9
     *
10
     * TODO: move this to the CrudPanel object; They don't belong in controllers;
11
     *
12
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
13
     */
14
    public function getSaveAction()
15
    {
16
        $saveAction = session('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
17
18
        // Permissions and their related actions.
19
        $permissions = [
20
            'list'   => 'save_and_back',
21
            'update' => 'save_and_edit',
22
            'create' => 'save_and_new',
23
        ];
24
25
        $saveOptions = collect($permissions)
26
            // Restrict list to allowed actions.
27
            ->filter(function ($action, $permission) {
28
                return $this->crud->hasAccess($permission);
0 ignored issues
show
Bug introduced by
The property crud 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...
29
            })
30
            // Generate list of possible actions.
31
            ->mapWithKeys(function ($action, $permission) {
0 ignored issues
show
Unused Code introduced by
The parameter $permission 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...
32
                return [$action => $this->getSaveActionButtonName($action)];
33
            })
34
            ->all();
35
36
        // Set current action if it exist, or first available option.
37
        if (isset($saveOptions[$saveAction])) {
38
            $saveCurrent = [
39
                'value' => $saveAction,
40
                'label' => $saveOptions[$saveAction],
41
            ];
42
        } else {
43
            $saveCurrent = [
44
                'value' => key($saveOptions),
45
                'label' => reset($saveOptions),
46
            ];
47
        }
48
49
        // Remove active action from options.
50
        unset($saveOptions[$saveCurrent['value']]);
51
52
        return [
53
            'active'  => $saveCurrent,
54
            'options' => $saveOptions,
55
        ];
56
    }
57
58
    /**
59
     * Change the session variable that remembers what to do after the "Save" action.
60
     *
61
     * @param  string|null  $forceSaveAction
62
     * @return void
63
     */
64
    public function setSaveAction($forceSaveAction = null)
65
    {
66
        $saveAction = $forceSaveAction ?:
67
            \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
68
69
        if (config('backpack.crud.show_save_action_change', true) &&
70
            session('save_action', 'save_and_back') !== $saveAction) {
71
            \Alert::info(trans('backpack::crud.save_action_changed_notification'))->flash();
72
        }
73
74
        session(['save_action' => $saveAction]);
75
    }
76
77
    /**
78
     * Redirect to the correct URL, depending on which save action has been selected.
79
     *
80
     * @param  string  $itemId
0 ignored issues
show
Documentation introduced by
Should the type for parameter $itemId 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...
81
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
82
     */
83
    public function performSaveAction($itemId = null)
84
    {
85
        $saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
86
        $itemId = $itemId ?: \Request::input('id');
87
88
        switch ($saveAction) {
89
            case 'save_and_new':
90
                $redirectUrl = $this->crud->route.'/create';
91
                break;
92
            case 'save_and_edit':
93
                $redirectUrl = $this->crud->route.'/'.$itemId.'/edit';
94
                if (\Request::has('current_tab')) {
95
                    $redirectUrl = $redirectUrl.'#'.\Request::get('current_tab');
96
                }
97
                if (\Request::has('locale')) {
98
                    $redirectUrl .= '?locale='.\Request::input('locale');
99
                }
100
101
                break;
102
            case 'save_and_back':
103
            default:
104
                $redirectUrl = $this->crud->route;
105
                break;
106
        }
107
108
        // if the request is AJAX, return a JSON response
109
        if ($this->request->ajax()) {
0 ignored issues
show
Bug introduced by
The property request 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...
110
            return [
111
                'success' => true,
112
                'data' => $this->crud->entry,
113
                'redirect_url' => $redirectUrl,
114
            ];
115
        }
116
117
        return \Redirect::to($redirectUrl);
118
    }
119
120
    /**
121
     * Get the translated text for the Save button.
122
     *
123
     * @param  string  $actionValue
124
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Contracts\Tr...lator|string|array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
125
     */
126
    private function getSaveActionButtonName($actionValue = 'save_and_back')
127
    {
128
        switch ($actionValue) {
129
            case 'save_and_edit':
130
                return trans('backpack::crud.save_action_save_and_edit');
131
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
132
            case 'save_and_new':
133
                return trans('backpack::crud.save_action_save_and_new');
134
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
135
            case 'save_and_back':
136
            default:
137
                return trans('backpack::crud.save_action_save_and_back');
138
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
139
        }
140
    }
141
}
142