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 — v6 (#4971)
by Pedro
29:14 queued 14:37
created

HasForm::formRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 12
nc 2
nop 5
dl 0
loc 16
rs 9.8666
c 2
b 1
f 1
1
<?php
2
3
namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\Str;
7
8
trait HasForm
9
{
10
    /**
11
     * Set up a GET and POST route, for an operation that contains a form.
12
     */
13
    protected function formRoutes(string $operationName, bool $routesHaveIdSegment, string $segment, string $routeName, string $controller): void
14
    {
15
        $secondSegment = $routesHaveIdSegment ? '/{id}/' : '/';
16
        $thirdSegment = Str::of($operationName)->kebab();
17
        $getFormMethod = 'get'.$operationName.'Form';
18
        $postFormMethod = 'post'.$operationName.'Form';
19
20
        Route::get($segment.$secondSegment.$thirdSegment, [
21
            'as'        => $routeName.'.'.$getFormMethod,
22
            'uses'      => $controller.'@'.$getFormMethod,
23
            'operation' => $operationName,
24
        ]);
25
        Route::post($segment.$secondSegment.$thirdSegment, [
26
            'as'        => $routeName.'.'.$postFormMethod,
27
            'uses'      => $controller.'@'.$postFormMethod,
28
            'operation' => $operationName,
29
        ]);
30
    }
31
32
    /**
33
     * Set up the default configurations, save actions and buttons
34
     * for a standard operation that contains a form.
35
     */
36
    protected function formDefaults(string $operationName, string $buttonStack = 'line', array $buttonMeta = []): void
37
    {
38
        // Access
39
        $this->crud->allowAccess($operationName);
40
41
        // Config
42
        $this->crud->operation($operationName, function () use ($operationName) {
43
            // if the backpack.operations.{operationName} config exists, use that one
44
            // otherwise, use the generic backpack.operations.form config
45
            if (config()->has('backpack.operations.'.$operationName)) {
46
                $this->crud->loadDefaultOperationSettingsFromConfig();
47
            } else {
48
                $this->crud->loadDefaultOperationSettingsFromConfig('backpack.operations.form');
49
            }
50
51
            // add a reasonable "save and back" save action
52
            $this->crud->addSaveAction([
53
                'name' => 'save_and_back',
54
                'visible' => function ($crud) use ($operationName) {
55
                    return $crud->hasAccess($operationName);
56
                },
57
                'redirect' => function ($crud, $request, $itemId = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $itemId is not used and could be removed. ( Ignorable by Annotation )

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

57
                'redirect' => function ($crud, $request, /** @scrutinizer ignore-unused */ $itemId = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
                    return $request->request->has('_http_referrer') ? $request->request->get('_http_referrer') : $crud->route;
59
                },
60
                'button_text' => trans('backpack::crud.save_action_save_and_back'),
61
            ]);
62
        });
63
64
        // Default Button
65
        $this->crud->operation(['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) {
66
            $this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta);
67
        });
68
    }
69
70
    /**
71
     * Method to handle the GET request and display the View with a Backpack form.
72
     */
73
    public function formView(?int $id = null): \Illuminate\Contracts\View\View
74
    {
75
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
76
            // Get entry ID from Request (makes sure its the last ID for nested resources)
77
            $this->data['id'] = $this->crud->getCurrentEntryId() ?? $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...
78
            $this->data['entry'] = $this->crud->getEntryWithLocale($this->data['id']);
79
        }
80
81
        $this->crud->setOperationSetting('fields', $this->crud->fields());
82
83
        $this->data['crud'] = $this->crud;
84
        $this->data['title'] = $this->crud->getTitle() ?? Str::of($this->crud->getCurrentOperation())->headline();
85
        $this->data['operation'] = $this->crud->getCurrentOperation();
86
87
        if ($this->crud->getOperationSetting('save_actions')) {
88
            $this->data['saveAction'] = $this->crud->getSaveAction();
89
        }
90
91
        $this->data['formAction'] = $this->crud->getOperationSetting('form_action');
92
        $this->data['formMethod'] = $this->crud->getOperationSetting('form_method');
93
94
        return view($this->crud->getOperationSetting('view') ?? 'crud::inc.form_page', $this->data);
95
    }
96
97
    /**
98
     * Method to handle the POST request and save the form.
99
     * It performs the validation, authorization and redirect according to the save action.
100
     * But it does NOT save the data. That should be done in the given $formLogic callback.
101
     *
102
     * @return array|\Illuminate\Http\RedirectResponse
103
     */
104
    public function formAction(?int $id = null, callable $formLogic)
105
    {
106
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
107
            // Get entry ID from Request (makes sure its the last ID for nested resources)
108
            $id = $this->crud->getCurrentEntryId() ?? $id;
109
            $entry = $this->crud->getEntryWithLocale($id);
110
        }
111
112
        // execute the FormRequest authorization and validation, if one is required
113
        $request = $this->crud->validateRequest();
114
        $request = $this->crud->getStrippedSaveRequest($request);
115
116
        // register any Model Events defined on fields
117
        $this->crud->registerFieldEvents();
118
119
        // perform the actual logic, that developers give in a callback
120
        ($formLogic)($request ?? null, $entry ?? null);
121
122
        // save the redirect choice for next time
123
        $this->crud->setSaveAction();
124
125
        return $this->crud->performSaveAction($id);
126
    }
127
}
128