GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( b2510c...ee0bb8 )
by
unknown
12:46
created

AdminController::inlineEdit()   C

Complexity

Conditions 19
Paths 24

Size

Total Lines 69
Code Lines 40

Duplication

Lines 15
Ratio 21.74 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 15
loc 69
c 1
b 0
f 0
cc 19
eloc 40
nc 24
nop 2
ccs 0
cts 48
cp 0
crap 380
rs 5.6192

How to fix   Long Method    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 SleepingOwl\Admin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use SleepingOwl\Admin\Form\FormElements;
8
use SleepingOwl\Admin\Form\Columns\Column;
9
use SleepingOwl\Admin\Display\DisplayTable;
10
use Illuminate\Contracts\Support\Renderable;
11
use SleepingOwl\Admin\Display\DisplayTabbed;
12
use Illuminate\Validation\ValidationException;
13
use SleepingOwl\Admin\Contracts\AdminInterface;
14
use SleepingOwl\Admin\Model\ModelConfiguration;
15
use Illuminate\Contracts\Foundation\Application;
16
use SleepingOwl\Admin\Contracts\Form\FormInterface;
17
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
18
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;
19
20
class AdminController extends Controller
21
{
22
    /**
23
     * @var \DaveJamesMiller\Breadcrumbs\Manager
24
     */
25
    protected $breadcrumbs;
26
27
    /**
28
     * @var AdminInterface
29
     */
30
    protected $admin;
31
32
    /**
33
     * @var
34
     */
35
    private $parentBreadcrumb = 'home';
36
37
    /**
38
     * @var Application
39
     */
40
    public $app;
41
42
    /**
43
     * AdminController constructor.
44
     *
45
     * @param Request $request
46
     * @param AdminInterface $admin
47
     * @param Application $application
48
     */
49
    public function __construct(Request $request, AdminInterface $admin, Application $application)
50
    {
51
        $this->app = $application;
52
        $this->admin = $admin;
53
        $this->breadcrumbs = $admin->template()->breadcrumbs();
54
55
        $admin->navigation()->setCurrentUrl($request->getUri());
56
57
        if (! $this->breadcrumbs->exists('home')) {
58
            $this->breadcrumbs->register('home', function ($breadcrumbs) {
59
                $breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard'));
60
            });
61
        }
62
63
        $breadcrumbs = [];
64
65
        if ($currentPage = $admin->navigation()->getCurrentPage()) {
66
            foreach ($currentPage->getPathArray() as $page) {
67
                $breadcrumbs[] = [
68
                    'id' => $page['id'],
69
                    'title' => $page['title'],
70
                    'url' => $page['url'],
71
                    'parent' => $this->parentBreadcrumb,
72
                ];
73
74
                $this->parentBreadcrumb = $page['id'];
75
            }
76
        }
77
78
        foreach ($breadcrumbs as  $breadcrumb) {
79
            if (! $this->breadcrumbs->exists($breadcrumb['id'])) {
80
                $this->breadcrumbs->register($breadcrumb['id'], function ($breadcrumbs) use ($breadcrumb) {
81
                    $breadcrumbs->parent($breadcrumb['parent']);
82
                    $breadcrumbs->push($breadcrumb['title'], $breadcrumb['url']);
83
                });
84
            }
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getParentBreadcrumb()
92
    {
93
        return $this->parentBreadcrumb;
94
    }
95
96
    /**
97
     * @param string $parentBreadcrumb
98
     */
99
    public function setParentBreadcrumb($parentBreadcrumb)
100
    {
101
        $this->parentBreadcrumb = $parentBreadcrumb;
102
    }
103
104
    /**
105
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
106
     */
107
    public function getDashboard()
108
    {
109
        return $this->renderContent(
110
            $this->admin->template()->view('dashboard'),
0 ignored issues
show
Bug introduced by
It seems like $this->admin->template()->view('dashboard') targeting SleepingOwl\Admin\Contra...mplateInterface::view() can also be of type boolean or object<Illuminate\Contracts\View\Factory>; however, SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept object<Illuminate\Contra...port\Renderable>|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...
111
            trans('sleeping_owl::lang.dashboard')
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.dashboard') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept string|null, 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...
112
        );
113
    }
114
115
    /**
116
     * @param ModelConfigurationInterface $model
117
     *
118
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
119
     *
120
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
121
     */
122
    public function getDisplay(ModelConfigurationInterface $model)
123
    {
124
        if (! $model->isDisplayable()) {
125
            abort(404);
126
        }
127
128
        return $this->render($model, $model->fireDisplay());
129
    }
130
131
    /**
132
     * @param ModelConfigurationInterface $model
133
     *
134
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
135
     *
136
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
137
     */
138
    public function getCreate(ModelConfigurationInterface $model)
139
    {
140
        if (! $model->isCreatable()) {
141
            abort(404);
142
        }
143
144
        $create = $model->fireCreate();
145
146
        $this->registerBreadcrumb($model->getCreateTitle(), $this->parentBreadcrumb);
0 ignored issues
show
Bug introduced by
It seems like $model->getCreateTitle() targeting SleepingOwl\Admin\Contra...rface::getCreateTitle() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Http\C...r::registerBreadcrumb() 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...
147
148
        return $this->render($model, $create, $model->getCreateTitle());
0 ignored issues
show
Bug introduced by
It seems like $model->getCreateTitle() targeting SleepingOwl\Admin\Contra...rface::getCreateTitle() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Http\C...minController::render() does only seem to accept string|null, 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...
149
    }
150
151
    /**
152
     * @param ModelConfigurationInterface $model
153
     * @param Request $request
154
     *
155
     * @return \Illuminate\Http\RedirectResponse
156
     */
157
    public function postStore(ModelConfigurationInterface $model, Request $request)
158
    {
159
        if (! $model->isCreatable()) {
160
            abort(404);
161
        }
162
163
        $createForm = $model->fireCreate();
164
        $nextAction = $request->input('next_action');
165
166
        $backUrl = $this->getBackUrl($request);
167
168 View Code Duplication
        if ($createForm instanceof FormInterface) {
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...
169
            try {
170
                $createForm->validateForm($request, $model);
171
172
                if ($createForm->saveForm($request, $model) === false) {
173
                    return redirect()->back()->with([
174
                        '_redirectBack' => $backUrl,
175
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
176
                    ]);
177
                }
178
            } catch (ValidationException $exception) {
179
                return redirect()->back()
180
                    ->withErrors($exception->validator)
181
                    ->withInput()
182
                    ->with([
183
                        '_redirectBack' => $backUrl,
184
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
185
                    ]);
186
            }
187
        }
188
189
        if ($nextAction == 'save_and_continue') {
190
            $newModel = $createForm->getModel();
191
            $primaryKey = $newModel->getKeyName();
192
193
            $redirectUrl = $model->getEditUrl($newModel->{$primaryKey});
194
            $redirectPolicy = $model->getRedirect();
195
196
            /* Make redirect when use in model config && Fix editable redirect */
197
            if ($redirectPolicy->get('create') == 'display' || ! $model->isEditable($newModel)) {
198
                $redirectUrl = $model->getDisplayUrl();
199
            }
200
201
            $response = redirect()->to(
202
                $redirectUrl
203
            )->with([
204
                '_redirectBack' => $backUrl,
205
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
206
            ]);
207 View Code Duplication
        } elseif ($nextAction == 'save_and_create') {
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...
208
            $response = redirect()->to($model->getCreateUrl($request->except([
209
                '_redirectBack',
210
                '_token',
211
                'url',
212
                'next_action',
213
            ])))->with([
214
                '_redirectBack' => $backUrl,
215
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
216
            ]);
217
        } else {
218
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
0 ignored issues
show
Bug introduced by
It seems like $request->input('_redire...model->getDisplayUrl()) targeting Illuminate\Http\Concerns...ractsWithInput::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...
219
        }
220
221
        return $response->with('success_message', $model->getMessageOnCreate());
222
    }
223
224
    /**
225
     * @param ModelConfigurationInterface $model
226
     * @param int                $id
227
     *
228
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
229
     *
230
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
231
     */
232
    public function getEdit(ModelConfigurationInterface $model, $id)
233
    {
234
        $item = $model->getRepository()->find($id);
235
236
        if (is_null($item) || ! $model->isEditable($item)) {
237
            abort(404);
238
        }
239
240
        $this->registerBreadcrumb($model->getEditTitle(), $this->parentBreadcrumb);
0 ignored issues
show
Bug introduced by
It seems like $model->getEditTitle() targeting SleepingOwl\Admin\Contra...terface::getEditTitle() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Http\C...r::registerBreadcrumb() 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...
241
242
        return $this->render($model, $model->fireEdit($id), $model->getEditTitle());
0 ignored issues
show
Bug introduced by
It seems like $model->getEditTitle() targeting SleepingOwl\Admin\Contra...terface::getEditTitle() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Http\C...minController::render() does only seem to accept string|null, 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...
243
    }
244
245
    /**
246
     * @param ModelConfigurationInterface $model
247
     * @param Request $request
248
     * @param int $id
249
     *
250
     * @return \Illuminate\Http\RedirectResponse
251
     *
252
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
253
     */
254
    public function postUpdate(ModelConfigurationInterface $model, Request $request, $id)
255
    {
256
        /** @var FormInterface $editForm */
257
        $editForm = $model->fireEdit($id);
258
        $item = $editForm->getModel();
259
260
        if (is_null($item) || ! $model->isEditable($item)) {
261
            abort(404);
262
        }
263
264
        $nextAction = $request->input('next_action');
265
266
        $backUrl = $this->getBackUrl($request);
267
268 View Code Duplication
        if ($editForm instanceof FormInterface) {
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...
269
            try {
270
                $editForm->validateForm($request, $model);
271
272
                if ($editForm->saveForm($request, $model) === false) {
273
                    return redirect()->back()->with([
274
                        '_redirectBack' => $backUrl,
275
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
276
                    ]);
277
                }
278
            } catch (ValidationException $exception) {
279
                return redirect()->back()
280
                    ->withErrors($exception->validator)
281
                    ->withInput()
282
                    ->with([
283
                        '_redirectBack' => $backUrl,
284
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
285
                    ]);
286
            }
287
        }
288
289
        $redirectPolicy = $model->getRedirect();
290
291
        if ($nextAction == 'save_and_continue') {
292
            $response = redirect()->back()->with([
293
                '_redirectBack' => $backUrl,
294
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
295
            ]);
296
297
            if ($redirectPolicy->get('edit') == 'display') {
298
                $response = redirect()->to(
299
                    $model->getDisplayUrl()
300
                )->with([
301
                    '_redirectBack' => $backUrl,
302
                    'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
303
                ]);
304
            }
305 View Code Duplication
        } elseif ($nextAction == 'save_and_create') {
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...
306
            $response = redirect()->to($model->getCreateUrl($request->except([
307
                '_redirectBack',
308
                '_token',
309
                'url',
310
                'next_action',
311
            ])))->with([
312
                '_redirectBack' => $backUrl,
313
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
314
            ]);
315
        } else {
316
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
0 ignored issues
show
Bug introduced by
It seems like $request->input('_redire...model->getDisplayUrl()) targeting Illuminate\Http\Concerns...ractsWithInput::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...
317
        }
318
319
        return $response->with('success_message', $model->getMessageOnUpdate());
320
    }
321
322
    /**
323
     * @param ModelConfigurationInterface $model
324
     * @param Request $request
325
     *
326
     * @return bool
327
     *
328
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
329
     */
330
    public function inlineEdit(ModelConfigurationInterface $model, Request $request)
331
    {
332
        $field = $request->input('name');
333
        $id = $request->input('pk');
334
        $display = $model->fireDisplay();
335
        $column = null;
336
337
        /* @var ColumnEditableInterface|null $column */
338
        if (is_callable([$display, 'getColumns'])) {
339
            $column = $display->getColumns()->all()->filter(function ($column) use ($field) {
340
                return ($column instanceof ColumnEditableInterface) and $field == $column->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
341
            })->first();
342
        } else {
343
            if ($display instanceof DisplayTabbed) {
344
                foreach ($display->getTabs() as $tab) {
345
                    $content = $tab->getContent();
346
347 View Code Duplication
                    if ($content instanceof DisplayTable) {
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...
348
                        $column = $content->getColumns()->all()->filter(function ($column) use ($field) {
349
                            return ($column instanceof ColumnEditableInterface) and $field == $column->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
350
                        })->first();
351
                    }
352
                    if ($content instanceof FormElements) {
353
                        foreach ($content->getElements() as $element) {
354
355
                            //Return data-table if inside FormElements
356 View Code Duplication
                            if ($element instanceof DisplayTable) {
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...
357
                                $column = $element->getColumns()->all()->filter(function ($column) use ($field) {
358
                                    return ($column instanceof ColumnEditableInterface) and $field == $column->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
359
                                })->first();
360
                            }
361
362
                            //Try to find inline Editable in columns
363
                            if ($element instanceof Column) {
364
                                foreach ($element->getElements() as $columnElement) {
365 View Code Duplication
                                    if ($columnElement instanceof DisplayTable) {
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...
366
                                        $column = $columnElement->getColumns()->all()->filter(function ($column) use ($field) {
367
                                            return ($column instanceof ColumnEditableInterface) and $field == $column->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
368
                                        })->first();
369
                                    }
370
                                }
371
                            }
372
                        }
373
                    }
374
                }
375
            }
376
        }
377
378
        if (is_null($column)) {
379
            abort(404);
380
        }
381
382
        $repository = $model->getRepository();
383
        $item = $repository->find($id);
384
385
        if (is_null($item) || ! $model->isEditable($item)) {
386
            abort(404);
387
        }
388
389
        $column->setModel($item);
390
391
        if ($model->fireEvent('updating', true, $item) === false) {
392
            return;
393
        }
394
395
        $column->save($request, $model);
396
397
        $model->fireEvent('updated', false, $item);
398
    }
399
400
    /**
401
     * @param ModelConfigurationInterface $model
402
     * @param int                $id
403
     *
404
     * @return \Illuminate\Http\RedirectResponse
405
     *
406
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
407
     */
408 View Code Duplication
    public function deleteDelete(ModelConfigurationInterface $model, Request $request, $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...
409
    {
410
        $item = $model->getRepository()->find($id);
411
412
        if (is_null($item) || ! $model->isDeletable($item)) {
413
            abort(404);
414
        }
415
416
        $model->fireDelete($id);
417
418
        if ($model->fireEvent('deleting', true, $item) === false) {
419
            return redirect()->back();
420
        }
421
422
        $model->getRepository()->delete($id);
423
424
        $model->fireEvent('deleted', false, $item);
425
426
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug introduced by
It seems like $request->input('_redire...back()->getTargetUrl()) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, redirect() does only seem to accept string|null, 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...
427
            ->with('success_message', $model->getMessageOnDelete());
428
    }
429
430
    /**
431
     * @param ModelConfigurationInterface $model
432
     * @param Request $request
433
     * @param int $id
434
     *
435
     * @return \Illuminate\Http\RedirectResponse
436
     *
437
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
438
     */
439 View Code Duplication
    public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $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...
440
    {
441
        if (! $model->isRestorableModel()) {
442
            abort(404);
443
        }
444
445
        $item = $model->getRepository()->findOnlyTrashed($id);
446
447
        if (is_null($item) || ! $model->isRestorable($item)) {
448
            abort(404);
449
        }
450
451
        $model->fireDestroy($id);
452
453
        if ($model->fireEvent('destroying', true, $item) === false) {
454
            return redirect()->back();
455
        }
456
457
        $model->getRepository()->forceDelete($id);
458
459
        $model->fireEvent('destroyed', false, $item);
460
461
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug introduced by
It seems like $request->input('_redire...back()->getTargetUrl()) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, redirect() does only seem to accept string|null, 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...
462
            ->with('success_message', $model->getMessageOnDestroy());
463
    }
464
465
    /**
466
     * @param ModelConfigurationInterface|ModelConfiguration $model
467
     * @param Request $request
468
     * @param int $id
469
     *
470
     * @return \Illuminate\Http\RedirectResponse
471
     *
472
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
473
     */
474 View Code Duplication
    public function postRestore(ModelConfigurationInterface $model, Request $request, $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...
475
    {
476
        if (! $model->isRestorableModel()) {
477
            abort(404);
478
        }
479
480
        $item = $model->getRepository()->findOnlyTrashed($id);
481
482
        if (is_null($item) || ! $model->isRestorable($item)) {
483
            abort(404);
484
        }
485
486
        $model->fireRestore($id);
487
488
        if ($model->fireEvent('restoring', true, $item) === false) {
489
            return redirect()->back();
490
        }
491
492
        $model->getRepository()->restore($id);
493
494
        $model->fireEvent('restored', false, $item);
495
496
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug introduced by
It seems like $request->input('_redire...back()->getTargetUrl()) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, redirect() does only seem to accept string|null, 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...
497
            ->with('success_message', $model->getMessageOnRestore());
498
    }
499
500
    /**
501
     * @param ModelConfigurationInterface $model
502
     * @param Renderable|string $content
503
     * @param string|null $title
504
     *
505
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
506
     */
507
    public function render(ModelConfigurationInterface $model, $content, $title = null)
508
    {
509
        if ($content instanceof Renderable) {
510
            $content = $content->render();
511
        }
512
513
        if (is_null($title)) {
514
            $title = $model->getTitle();
515
        }
516
517
        return $this->admin->template()->view('_layout.inner')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
518
            ->with('title', $title)
519
            ->with('content', $content)
520
            ->with('breadcrumbKey', $this->parentBreadcrumb);
521
    }
522
523
    /**
524
     * @param Renderable|string $content
525
     * @param string|null       $title
526
     *
527
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
528
     */
529
    public function renderContent($content, $title = null)
530
    {
531
        if ($content instanceof Renderable) {
532
            $content = $content->render();
533
        }
534
535
        return $this->admin->template()->view('_layout.inner')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
536
            ->with('title', $title)
537
            ->with('content', $content)
538
            ->with('breadcrumbKey', $this->parentBreadcrumb);
539
    }
540
541
    /**
542
     * @param Request $request
543
     *
544
     * @return null|string
545
     */
546
    protected function getBackUrl(Request $request)
547
    {
548
        if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) {
549
            $backUrl = null;
550
            $request->merge(['_redirectBack' => $backUrl]);
551
        }
552
553
        return $backUrl;
554
    }
555
556
    public function getWildcard()
557
    {
558
        abort(404);
559
    }
560
561
    /**
562
     * @param string $title
563
     * @param string $parent
564
     */
565
    protected function registerBreadcrumb($title, $parent)
566
    {
567
        $this->breadcrumbs->register('render', function ($breadcrumbs) use ($title, $parent) {
568
            $breadcrumbs->parent($parent);
569
            $breadcrumbs->push($title);
570
        });
571
572
        $this->parentBreadcrumb = 'render';
573
    }
574
}
575