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 (#1204)
by Alashov
03:18
created

SaveActions::getSaveAction()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 38
Code Lines 28

Duplication

Lines 8
Ratio 21.05 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 28
c 1
b 1
f 0
nc 16
nop 0
dl 8
loc 38
rs 6.7272
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures;
4
5
//save_and_back save_and_edit save_and_new
6
trait SaveActions
7
{
8
    /**
9
     * Get the save configured save action or the one stored in a session variable.
10
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
11
     */
12
    public function getSaveAction()
13
    {
14
        $cantCreate = ! $this->crud->hasAccess('create');
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...
15
        $saveAction = session('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
16
        if ($saveAction == 'save_and_new' && $cantCreate) {
17
            $saveAction = 'save_and_back';
18
        }
19
        $saveOptions = [];
20
        $saveCurrent = [
21
            'value' => $saveAction,
22
            'label' => $this->getSaveActionButtonName($saveAction),
23
        ];
24
25
        switch ($saveAction) {
26
            case 'save_and_edit':
27
                $saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back');
28
                $saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new');
29
                break;
30 View Code Duplication
            case 'save_and_new':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
                $saveOptions['save_and_back'] = $this->getSaveActionButtonName('save_and_back');
32
                $saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit');
33
                break;
34
            case 'save_and_back':
35 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
                $saveOptions['save_and_edit'] = $this->getSaveActionButtonName('save_and_edit');
37
                $saveOptions['save_and_new'] = $this->getSaveActionButtonName('save_and_new');
38
                break;
39
        }
40
41
        if ($cantCreate) {
42
            unset($saveOptions['save_and_new']);
43
        }
44
45
        return [
46
            'active' => $saveCurrent,
47
            'options' => $saveOptions,
48
        ];
49
    }
50
51
    /**
52
     * Change the session variable that remembers what to do after the "Save" action.
53
     * @param [type] $forceSaveAction [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
54
     */
55
    public function setSaveAction($forceSaveAction = null)
56
    {
57
        if ($forceSaveAction) {
58
            $saveAction = $forceSaveAction;
59
        } else {
60
            $saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
61
        }
62
63
        if (session('save_action', 'save_and_back') !== $saveAction && config('backpack.crud.show_save_action_change', true)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
64
            \Alert::info(trans('backpack::crud.save_action_changed_notification'))->flash();
65
        }
66
67
        session(['save_action' => $saveAction]);
68
    }
69
70
    /**
71
     * Redirect to the correct URL, depending on which save action has been selected.
72
     * @param  [type] $itemId [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
73
     * @return [type]         [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
74
     */
75
    public function performSaveAction($itemId = null)
76
    {
77
        $saveAction = \Request::input('save_action', config('backpack.crud.default_save_action', 'save_and_back'));
78
        $itemId = $itemId ? $itemId : \Request::input('id');
79
80
        switch ($saveAction) {
81
            case 'save_and_new':
82
                $redirectUrl = $this->crud->route.'/create';
83
                break;
84
            case 'save_and_edit':
85
                $redirectUrl = $this->crud->route.'/'.$itemId.'/edit';
86
                if (\Request::has('locale')) {
87
                    $redirectUrl .= '?locale='.\Request::input('locale');
88
                }
89
                break;
90
            case 'save_and_back':
91
            default:
92
                $redirectUrl = $this->crud->route;
93
                break;
94
        }
95
96
        return \Redirect::to($redirectUrl);
97
    }
98
99
    /**
100
     * Get the translated text for the Save button.
101
     * @param  string $actionValue [description]
102
     * @return [type]              [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
103
     */
104
    private function getSaveActionButtonName($actionValue = 'save_and_back')
105
    {
106
        switch ($actionValue) {
107
            case 'save_and_edit':
108
                return trans('backpack::crud.save_action_save_and_edit');
109
                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...
110
            case 'save_and_new':
111
                return trans('backpack::crud.save_action_save_and_new');
112
                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...
113
            case 'save_and_back':
114
            default:
115
                return trans('backpack::crud.save_action_save_and_back');
116
                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...
117
        }
118
    }
119
}
120