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 ( 7a24d5...09959f )
by Cristian
02:56
created

CrudController::showDetailsRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers;
4
5
use Illuminate\Foundation\Bus\DispatchesJobs;
6
use Illuminate\Foundation\Validation\ValidatesRequests;
7
use Illuminate\Routing\Controller as BaseController;
8
use Illuminate\Support\Facades\Form as Form;
9
10
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest;
11
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest;
12
use Backpack\CRUD\CrudPanel;
13
14
// CRUD Traits for non-core features
15
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\AjaxTable;
16
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Reorder;
17
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Revisions;
18
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\ShowDetailsRow;
19
20
class CrudController extends BaseController
21
{
22
    use DispatchesJobs, ValidatesRequests;
23
    use AjaxTable, Reorder, Revisions, ShowDetailsRow;
24
25
    public $data = [];
26
    public $crud;
27
28
    public function __construct()
29
    {
30
        $this->crud = new CrudPanel();
31
32
        // call the setup function inside this closure to also have the request there
33
        // this way, developers can use things stored in session (auth variables, etc)
34
        $this->middleware(function ($request, $next) {
35
            $this->setup();
36
            return $next($request);
37
        });
38
    }
39
40
    /**
41
     * Allow developers to set their configuration options for a CrudPanel.
42
     */
43
    public function setup() {}
44
45
46
    /**
47
     * Display all rows in the database for this entity.
48
     *
49
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
50
     */
51
    public function index()
52
    {
53
        $this->crud->hasAccessOrFail('list');
54
55
        $this->data['crud'] = $this->crud;
56
        $this->data['title'] = ucfirst($this->crud->entity_name_plural);
57
58
        // get all entries if AJAX is not enabled
59
        if (! $this->data['crud']->ajaxTable()) {
60
            $this->data['entries'] = $this->data['crud']->getEntries();
61
        }
62
63
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
64
        // $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
65
        return view('crud::list', $this->data);
66
    }
67
68
    /**
69
     * Show the form for creating inserting a new row.
70
     *
71
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
72
     */
73 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    {
75
        $this->crud->hasAccessOrFail('create');
76
77
        // prepare the fields you need to show
78
        $this->data['crud'] = $this->crud;
79
        $this->data['fields'] = $this->crud->getCreateFields();
80
        $this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name;
81
82
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
83
        return view('crud::create', $this->data);
84
    }
85
86
    /**
87
     * Store a newly created resource in the database.
88
     *
89
     * @param StoreRequest $request - type injection used for validation using Requests
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|StoreRequest?

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...
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93
    public function storeCrud(StoreRequest $request = null)
94
    {
95
        $this->crud->hasAccessOrFail('create');
96
97
        // fallback to global request instance
98
        if (is_null($request)) {
99
            $request = \Request::instance();
100
        }
101
102
        // replace empty values with NULL, so that it will work with MySQL strict mode on
103 View Code Duplication
        foreach ($request->input() as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $request->input() of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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...
104
            if (empty($value) && $value !== '0') {
105
                $request->request->set($key, null);
106
            }
107
        }
108
109
        // insert item in the db
110
        $item = $this->crud->create($request->except(['redirect_after_save', '_token']));
111
112
        // show a success message
113
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
114
115
        // redirect the user where he chose to be redirected
116
        switch ($request->input('redirect_after_save')) {
117
            case 'current_item_edit':
118
                return \Redirect::to($this->crud->route.'/'.$item->getKey().'/edit');
119
120
            default:
121
                return \Redirect::to($request->input('redirect_after_save'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('redirect_after_save') targeting Illuminate\Http\Request::input() can also be of type array; however, Illuminate\Routing\Redirector::to() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
        }
123
    }
124
125
    /**
126
     * Show the form for editing the specified resource.
127
     *
128
     * @param int $id
129
     *
130
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
131
     */
132 View Code Duplication
    public function edit($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
133
    {
134
        $this->crud->hasAccessOrFail('update');
135
136
        // get the info for that entry
137
        $this->data['entry'] = $this->crud->getEntry($id);
138
        $this->data['crud'] = $this->crud;
139
        $this->data['fields'] = $this->crud->getUpdateFields($id);
140
        $this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name;
141
142
        $this->data['id'] = $id;
143
144
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
145
        return view('crud::edit', $this->data);
146
    }
147
148
    /**
149
     * Update the specified resource in the database.
150
     *
151
     * @param UpdateRequest $request - type injection used for validation using Requests
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|StoreRequest?

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...
152
     *
153
     * @return \Illuminate\Http\RedirectResponse
154
     */
155
    public function updateCrud(UpdateRequest $request = null)
156
    {
157
        $this->crud->hasAccessOrFail('update');
158
159
        // fallback to global request instance
160
        if (is_null($request)) {
161
            $request = \Request::instance();
162
        }
163
164
        // replace empty values with NULL, so that it will work with MySQL strict mode on
165 View Code Duplication
        foreach ($request->input() as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $request->input() of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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...
166
            if (empty($value) && $value !== '0') {
167
                $request->request->set($key, null);
168
            }
169
        }
170
171
        // update the row in the db
172
        $this->crud->update($request->get($this->crud->model->getKeyName()),
0 ignored issues
show
Bug introduced by
The method getKeyName cannot be called on $this->crud->model (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
173
                            $request->except('redirect_after_save', '_token'));
174
175
        // show a success message
176
        \Alert::success(trans('backpack::crud.update_success'))->flash();
177
178
        return \Redirect::to($this->crud->route);
179
    }
180
181
    /**
182
     * Display the specified resource.
183
     *
184
     * @param int $id
185
     *
186
     * @return Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

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...
187
     */
188 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
189
    {
190
        $this->crud->hasAccessOrFail('show');
191
192
        // get the info for that entry
193
        $this->data['entry'] = $this->crud->getEntry($id);
194
        $this->data['crud'] = $this->crud;
195
        $this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name;
196
197
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
198
        return view('crud::show', $this->data);
199
    }
200
201
    /**
202
     * Remove the specified resource from storage.
203
     *
204
     * @param int $id
205
     *
206
     * @return string
207
     */
208
    public function destroy($id)
209
    {
210
        $this->crud->hasAccessOrFail('delete');
211
212
        return $this->crud->delete($id);
213
    }
214
}
215