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 (#61)
by Oliver
02:35
created

UniquePageCrudController::setSaveAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Backpack\PageManager\app\Http\Controllers\Admin;
4
5
use App\UniquePages;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions;
8
use Backpack\PageManager\app\TraitReflections;
9
10
class UniquePageCrudController extends CrudController
11
{
12
    use SaveActions;
13
    use UniquePages;
14
    use TraitReflections;
15
16
    public function setup()
17
    {
18
        parent::__construct();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__construct() instead of setup()). Are you sure this is correct? If so, you might want to change this to $this->__construct().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
19
20
        $modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page');
21
22
        $this->checkForTemplatesAndUniquePagesNotDistinct();
23
24
        /*
25
        |--------------------------------------------------------------------------
26
        | BASIC CRUD INFORMATION
27
        |--------------------------------------------------------------------------
28
        */
29
        $this->crud->setModel($modelClass);
30
        // don't set route or entity names here. these depend on the page you are editing
31
32
        // unique pages can not be created nor deleted
33
        $this->crud->denyAccess('create');
34
        $this->crud->denyAccess('delete');
35
36
        if (config('backpack.pagemanager.unique_page_revisions')) {
37
            $this->crud->allowAccess('revisions');
38
        }
39
40
    }
41
42
    /**
43
     * As we want to edit pages by slug we need a new edit function
44
     *
45
     * @param string $slug the page slug
46
     * @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...
47
     */
48
    public function uniqueEdit($slug)
49
    {
50
        $model = $this->crud->model;
51
        $entry = $model::findBySlug($slug);
52
53
        if (! $entry) {
54
            $entry = $this->createMissingPage($slug);
55
        }
56
57
        $this->uniqueSetup($entry);
58
59
        return parent::edit($entry->id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (edit() instead of uniqueEdit()). Are you sure this is correct? If so, you might want to change this to $this->edit().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
60
    }
61
62
    public function update($slug, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
63
    {
64
        $this->setRoute($slug);
65
66
        return parent::updateCrud();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (updateCrud() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->updateCrud().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
67
    }
68
69
    public function setRoute($slug)
70
    {
71
        $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug);
72
    }
73
74
    /**
75
     * Populate the update form with basic fields, that all pages need.
76
     *
77
     * @param Model $page the page entity
78
     */
79
    public function addDefaultPageFields($page)
80
    {
81
        $this->crud->addField([
82
            'name' => 'template',
83
            'type' => 'hidden',
84
        ]);
85
        $this->crud->addField([
86
            'name' => 'name',
87
            'type' => 'hidden',
88
        ]);
89
        $this->crud->addField([
90
            'name' => 'title',
91
            'type' => 'hidden',
92
        ]);
93
        $this->crud->addField([
94
            'name' => 'slug',
95
            'type' => 'hidden',
96
        ]);
97
98
        $this->crud->addField([
99
            'name' => 'open',
100
            'type' => 'custom_html',
101
            'value' => $this->buttons($page)
102
        ]);
103
    }
104
105
    public function buttons($page)
106
    {
107
        $openButton = $page->getOpenButton();
108
        $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]);
109
        return $openButton .' '.$revisionsButton;
110
    }
111
112
    public function createMissingPage($slug)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
113
    {
114
        $pages = collect($this->getUniquePages());
115
116
        $slugs = $pages->mapWithKeys(function($page) {
117
            return [str_slug($page->name) => $page->name];
118
        });
119
120
        if (! $page = $slugs->pull($slug)) {
121
            abort(404);
122
        }
123
124
        $model = $this->crud->model;
125
126
        return $model::create([
127
            'template' => $page,
128
            'name' => camel_case($page),
129
            'title' => camel_case($page),
130
            'slug' => $slug,
131
        ]);
132
    }
133
134 View Code Duplication
    public function uniqueRevisions($slug, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
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...
135
    {
136
        $model = $this->crud->model;
137
        $entry = $model::findBySlugOrFail($slug);
138
139
        $this->uniqueSetup($entry);
140
141
        return parent::listRevisions($entry->id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (listRevisions() instead of uniqueRevisions()). Are you sure this is correct? If so, you might want to change this to $this->listRevisions().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
142
    }
143
144 View Code Duplication
    public function restoreUniqueRevision($slug, $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...
145
    {
146
        $model = $this->crud->model;
147
        $entry = $model::findBySlugOrFail($slug);
148
149
        $this->uniqueSetup($entry);
150
151
        return parent::restoreRevision($id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (restoreRevision() instead of restoreUniqueRevision()). Are you sure this is correct? If so, you might want to change this to $this->restoreRevision().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
152
    }
153
154
    protected function uniqueSetup($entry)
155
    {
156
        $this->setRoute($entry->slug);
157
158
        $this->addDefaultPageFields($entry);
159
        $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), '');
160
161
        $this->{$entry->template}();
162
    }
163
164
    /*
165
    |--------------------------------------------------------------------------
166
    | SaveActions overrides
167
    |--------------------------------------------------------------------------
168
    */
169
170
    /**
171
     * Overrides trait version to remove.
172
     */
173
    public function getSaveAction()
174
    {
175
        $saveCurrent = [
176
            'value' => $this->getSaveActionButtonName('save_and_back'),
177
            'label' => $this->getSaveActionButtonName('save_and_back'),
178
        ];
179
180
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('active' =>... 'options' => array()); (array<string,array>) is incompatible with the return type of the parent method Backpack\CRUD\app\Http\C...ntroller::getSaveAction of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
181
            'active' => $saveCurrent,
182
            'options' => [],
183
        ];
184
    }
185
186
    public function setSaveAction($forceSaveAction = null)
187
    {
188
        // do nothing to preserve session value for other crud
189
    }
190
}
191