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

Passed
Pull Request — master (#3132)
by Cristian
16:52
created

removeColumnsThatDontBelongInsideShowOperation()   C

Complexity

Conditions 12
Paths 33

Size

Total Lines 33
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 12
c 1
b 1
f 0
nc 33
nop 0
dl 0
loc 33
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait ShowOperation
8
{
9
    /**
10
     * Define which routes are needed for this operation.
11
     *
12
     * @param string $segment    Name of the current entity (singular). Used as first URL segment.
13
     * @param string $routeName  Prefix of the route name.
14
     * @param string $controller Name of the current CrudController.
15
     */
16
    protected function setupShowRoutes($segment, $routeName, $controller)
17
    {
18
        Route::get($segment.'/{id}/show', [
19
            'as'        => $routeName.'.show',
20
            'uses'      => $controller.'@show',
21
            'operation' => 'show',
22
        ]);
23
    }
24
25
    /**
26
     * Add the default settings, buttons, etc that this operation needs.
27
     */
28
    protected function setupShowDefaults()
29
    {
30
        $this->crud->allowAccess('show');
31
        $this->crud->setOperationSetting('setFromDb', true);
32
33
        $this->crud->operation('show', function () {
34
            $this->crud->loadDefaultOperationSettingsFromConfig();
35
        });
36
37
        $this->crud->operation('list', function () {
38
            $this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning');
39
        });
40
41
        $this->crud->operation(['create', 'update'], function () {
42
            $this->crud->addSaveAction([
43
                'name' => 'save_and_preview',
44
                'visible' => function ($crud) {
45
                    return $crud->hasAccess('show');
46
                },
47
                'redirect' => function ($crud, $request, $itemId = null) {
48
                    $itemId = $itemId ?: $request->input('id');
49
                    $redirectUrl = $crud->route.'/'.$itemId.'/show';
50
                    if ($request->has('locale')) {
51
                        $redirectUrl .= '?locale='.$request->input('locale');
52
                    }
53
54
                    return $redirectUrl;
55
                },
56
                'button_text' => trans('backpack::crud.save_action_save_and_preview'),
57
            ]);
58
        });
59
    }
60
61
    /**
62
     * Display the specified resource.
63
     *
64
     * @param int $id
65
     *
66
     * @return Response
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Http\C...ers\Operations\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
67
     */
68
    public function show($id)
69
    {
70
        $this->crud->hasAccessOrFail('show');
71
72
        // get entry ID from Request (makes sure its the last ID for nested resources)
73
        $id = $this->crud->getCurrentEntryId() ?? $id;
74
75
        // get the info for that entry
76
        $this->data['entry'] = $this->crud->getEntry($id);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->data['crud'] = $this->crud;
78
        $this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.preview').' '.$this->crud->entity_name;
0 ignored issues
show
Bug introduced by
Are you sure trans('backpack::crud.preview') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        $this->data['title'] = $this->crud->getTitle() ?? /** @scrutinizer ignore-type */ trans('backpack::crud.preview').' '.$this->crud->entity_name;
Loading history...
79
80
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
81
        return view($this->crud->getShowView(), $this->data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($this->crud-...howView(), $this->data) returns the type Illuminate\View\View which is incompatible with the documented return type Backpack\CRUD\app\Http\C...ers\Operations\Response.
Loading history...
82
    }
83
84
    /**
85
     * Default behaviour for setupShowOperation, in case none has been
86
     * provided by including a setupShowOperation() method in the CrudController.
87
     *
88
     * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
89
     */
90
    protected function setupShowOperation()
91
    {
92
        // guess which columns to show, from the database table
93
        if ($this->crud->get('show.setFromDb')) {
94
            $this->crud->setFromDb();
95
        }
96
97
        // if the developer has chosen to include a modifyShowOperation() method
98
        // in his/her CrudController, make sure to call it because they probably want to
99
        // add a new column or remove an autoset column
100
        if (method_exists($this, 'modifyShowOperation')) {
101
            $this->modifyShowOperation();
102
        }
103
104
        // remove the columns that usually don't make sense inside the Show operation
105
        $this->removeColumnsThatDontBelongInsideShowOperation();
106
107
        // remove preview button from stack:line
108
        $this->crud->removeButton('show');
109
    }
110
111
    protected function removeColumnsThatDontBelongInsideShowOperation()
112
    {
113
        // cycle through columns
114
        foreach ($this->crud->columns() as $key => $column) {
115
116
            // remove any autoset relationship columns
117
            if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) {
118
                $this->crud->removeColumn($column['key']);
119
            }
120
121
            // remove any autoset table columns
122
            if ($column['type'] == 'table' && array_key_exists('autoset', $column) && $column['autoset']) {
123
                $this->crud->removeColumn($column['key']);
124
            }
125
126
            // remove the row_number column, since it doesn't make sense in this context
127
            if ($column['type'] == 'row_number') {
128
                $this->crud->removeColumn($column['key']);
129
            }
130
131
            // remove columns that have visibleInShow set as false
132
            if (isset($column['visibleInShow']) && $column['visibleInShow'] == false) {
133
                $this->crud->removeColumn($column['key']);
134
            }
135
136
            // remove the character limit on columns that take it into account
137
            if (in_array($column['type'], ['text', 'email', 'model_function', 'model_function_attribute', 'phone', 'row_number', 'select'])) {
138
                $this->crud->modifyColumn($column['key'], ['limit' => ($column['limit'] ?? 999)]);
139
            }
140
        }
141
142
        // remove bulk actions colums
143
        $this->crud->removeColumns(['blank_first_column', 'bulk_actions']);
144
    }
145
}
146