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 (#85)
by Cristian
07:16
created

PageCrudController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\PageManager\app\Http\Controllers\Admin;
4
5
use App\PageTemplates;
6
// VALIDATION: change the requests to match your own file names if you need form validation
7
use Backpack\CRUD\app\Http\Controllers\CrudController;
8
use Backpack\PageManager\app\Http\Requests\PageRequest;
9
10
class PageCrudController extends CrudController
11
{
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { create as traitCreate; }
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { edit as traitEdit; }
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\CloneOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
17
    use PageTemplates;
18
19
    public function setup()
20
    {
21
        $this->crud->setModel(config('backpack.pagemanager.page_model_class', 'Backpack\PageManager\app\Models\Page'));
22
        $this->crud->setRoute(config('backpack.base.route_prefix').'/page');
23
        $this->crud->setEntityNameStrings(trans('backpack::pagemanager.page'), trans('backpack::pagemanager.pages'));
24
    }
25
26
    protected function setupListOperation()
27
    {
28
        $this->crud->addColumn([
29
            'name' => 'name',
30
            'label' => trans('backpack::pagemanager.name'),
31
        ]);
32
        $this->crud->addColumn([
33
            'name' => 'template',
34
            'label' => trans('backpack::pagemanager.template'),
35
            'type' => 'model_function',
36
            'function_name' => 'getTemplateName',
37
        ]);
38
        $this->crud->addColumn([
39
            'name' => 'slug',
40
            'label' => trans('backpack::pagemanager.slug'),
41
        ]);
42
        $this->crud->addButtonFromModelFunction('line', 'open', 'getOpenButton', 'beginning');
0 ignored issues
show
Documentation introduced by
'beginning' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44
45
    // -----------------------------------------------
46
    // Overwrites of CrudController
47
    // -----------------------------------------------
48
49
    protected function setupCreateOperation()
50
    {
51
        // Note:
52
        // - default fields, that all templates are using, are set using $this->addDefaultPageFields();
53
        // - template-specific fields are set per-template, in the PageTemplates trait;
54
        
55
        $this->addDefaultPageFields(\Request::input('template'));
0 ignored issues
show
Bug introduced by
It seems like \Request::input('template') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array or null; however, Backpack\PageManager\app...:addDefaultPageFields() does only seem to accept false|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...
56
        $this->useTemplate(\Request::input('template'));
0 ignored issues
show
Bug introduced by
It seems like \Request::input('template') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array or null; however, Backpack\PageManager\app...ntroller::useTemplate() does only seem to accept false|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...
57
58
        $this->crud->setValidation(PageRequest::class);
59
    }
60
61
    protected function setupUpdateOperation()
62
    {
63
        // if the template in the GET parameter is missing, figure it out from the db
64
        $template = \Request::input('template') ?? $this->crud->getCurrentEntry()->template;
65
66
        $this->addDefaultPageFields($template);
67
        $this->useTemplate($template);
68
69
        $this->crud->setValidation(PageRequest::class);
70
    }
71
72
    // -----------------------------------------------
73
    // Methods that are particular to the PageManager.
74
    // -----------------------------------------------
75
76
    /**
77
     * Populate the create/update forms with basic fields, that all pages need.
78
     *
79
     * @param string $template The name of the template that should be used in the current form.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $template not be false|string?

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...
80
     */
81
    public function addDefaultPageFields($template = false)
82
    {
83
        $this->crud->addField([
84
            'name' => 'template',
85
            'label' => trans('backpack::pagemanager.template'),
86
            'type' => 'select_page_template',
87
            'view_namespace'  => 'pagemanager::fields',
88
            'options' => $this->getTemplatesArray(),
89
            'value' => $template,
90
            'allows_null' => false,
91
            'wrapperAttributes' => [
92
                'class' => 'form-group col-md-6',
93
            ],
94
        ]);
95
        $this->crud->addField([
96
            'name' => 'name',
97
            'label' => trans('backpack::pagemanager.page_name'),
98
            'type' => 'text',
99
            'wrapperAttributes' => [
100
                'class' => 'form-group col-md-6',
101
            ],
102
            // 'disabled' => 'disabled'
103
        ]);
104
        $this->crud->addField([
105
            'name' => 'title',
106
            'label' => trans('backpack::pagemanager.page_title'),
107
            'type' => 'text',
108
            // 'disabled' => 'disabled'
109
        ]);
110
        $this->crud->addField([
111
            'name' => 'slug',
112
            'label' => trans('backpack::pagemanager.page_slug'),
113
            'type' => 'text',
114
            'hint' => trans('backpack::pagemanager.page_slug_hint'),
115
            // 'disabled' => 'disabled'
116
        ]);
117
    }
118
119
    /**
120
     * Add the fields defined for a specific template.
121
     *
122
     * @param  string $template_name The name of the template that should be used in the current form.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $template_name not be false|string?

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...
123
     */
124
    public function useTemplate($template_name = false)
125
    {
126
        $templates = $this->getTemplates();
127
128
        // set the default template
129
        if ($template_name == false) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $template_name of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
130
            $template_name = $templates[0]->name;
131
        }
132
133
        // actually use the template
134
        if ($template_name) {
135
            $this->{$template_name}();
136
        }
137
    }
138
139
    /**
140
     * Get all defined templates.
141
     */
142
    public function getTemplates($template_name = false)
0 ignored issues
show
Unused Code introduced by
The parameter $template_name is not used and could be removed.

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

Loading history...
143
    {
144
        $templates_array = [];
0 ignored issues
show
Unused Code introduced by
$templates_array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
146
        $templates_trait = new \ReflectionClass('App\PageTemplates');
147
        $templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE);
148
149
        if (! count($templates)) {
150
            abort(503, trans('backpack::pagemanager.template_not_found'));
151
        }
152
153
        return $templates;
154
    }
155
156
    /**
157
     * Get all defined template as an array.
158
     *
159
     * Used to populate the template dropdown in the create/update forms.
160
     */
161
    public function getTemplatesArray()
162
    {
163
        $templates = $this->getTemplates();
164
165
        foreach ($templates as $template) {
166
            $templates_array[$template->name] = str_replace('_', ' ', title_case($template->name));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$templates_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $templates_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
167
        }
168
169
        return $templates_array;
0 ignored issues
show
Bug introduced by
The variable $templates_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
170
    }
171
}
172