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 Cristian
06:28 queued 04:35
created

UniquePageCrudController::uniqueEdit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Backpack\PageManager\app\Http\Controllers\Admin;
4
5
use App\UniquePages;
6
use Illuminate\Http\Request;
7
use Backpack\PageManager\app\TraitReflections;
8
use Backpack\CRUD\app\Http\Controllers\CrudController;
9
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions;
10
11
class UniquePageCrudController extends CrudController
12
{
13
    use SaveActions;
14
    use UniquePages;
15
    use TraitReflections;
16
17
    public function setup()
18
    {
19
        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...
20
21
        $modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page');
22
23
        $this->checkForTemplatesAndUniquePagesNotDistinct();
24
25
        /*
26
        |--------------------------------------------------------------------------
27
        | BASIC CRUD INFORMATION
28
        |--------------------------------------------------------------------------
29
        */
30
        $this->crud->setModel($modelClass);
31
        // Don't set route or entity names here. These depend on the page you are editing
32
33
        // unique pages cannot be created nor deleted
34
        $this->crud->denyAccess(['list', 'create', 'delete']);
35
36
        if (config('backpack.pagemanager.unique_page_revisions')) {
37
            $this->crud->allowAccess('revisions');
38
        }
39
    }
40
41
    /**
42
     * Edit the unique page retrieved by slug.
43
     *
44
     * @param string $slug
45
     * @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...
46
     */
47
    public function uniqueEdit($slug)
48
    {
49
        $model = $this->crud->model;
50
        $entry = $model::findBySlug($slug);
51
52
        if (! $entry) {
53
            $entry = $this->createMissingPage($slug);
54
        }
55
56
        $this->uniqueSetup($entry);
57
58
        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...
59
    }
60
61
    /**
62
     * Update the unique page.
63
     *
64
     * @param string $slug
65
     * @param int $id
66
     * @return \Illuminate\Http\RedirectResponse
67
     */
68 View Code Duplication
    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...
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...
69
    {
70
        $model = $this->crud->model;
71
        $entry = $model::findBySlugOrFail($slug);
72
73
        $this->uniqueSetup($entry);
74
75
        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...
76
    }
77
78
    /**
79
     * Set the crud route.
80
     *
81
     * @param $slug
82
     */
83
    public function setRoute($slug)
84
    {
85
        $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug);
86
    }
87
88
    /**
89
     * Populate the update form with basic fields that all pages need.
90
     *
91
     * @param Model $page
92
     */
93
    public function addDefaultPageFields($page)
94
    {
95
        $fields = [
96
            [
97
                'name'  => 'buttons',
98
                'type'  => 'custom_html',
99
                'value' => $this->buttons($page),
100
            ],
101
            [
102
                'name' => 'template',
103
                'type' => 'hidden',
104
            ],
105
            [
106
                'name' => 'name',
107
            ],
108
            [
109
                'name' => 'title',
110
            ],
111
            [
112
                'name' => 'slug',
113
                'type' => 'hidden',
114
            ],
115
        ];
116
117
        $this->crud->addFields($fields);
118
    }
119
120
    /**
121
     * Build the buttons html for the edit form.
122
     *
123
     * @param Model $page
124
     * @return string
125
     */
126
    public function buttons($page)
127
    {
128
        $openButton = $page->getOpenButton();
129
        $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]);
130
131
        return $openButton.' '.$revisionsButton;
132
    }
133
134
    /**
135
     * Create the page by slug.
136
     *
137
     * @param $slug
138
     * @return mixed
139
     */
140
    public function createMissingPage($slug)
141
    {
142
        $slugs = $this->getUniqueSlugs();
143
144
        if (! $slugs->has($slug)) {
145
            abort(404);
146
        }
147
148
        $page = $slugs->pull($slug);
149
        $model = $this->crud->model;
150
151
        return $model::create([
152
            'template' => $page,
153
            'name'     => camel_case($page),
154
            'title'    => camel_case($page),
155
            'slug'     => $slug,
156
        ]);
157
    }
158
159
    /**
160
     * Display the revisions for specified resource.
161
     *
162
     * @param $slug
163
     * @param $id
164
     * @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...
165
     */
166 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...
167
    {
168
        $model = $this->crud->model;
169
        $entry = $model::findBySlugOrFail($slug);
170
171
        $this->uniqueSetup($entry);
172
173
        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...
174
    }
175
176
    /**
177
     * Restore a specific revision for the specified resource.
178
     *
179
     * Used via AJAX in the revisions view
180
     *
181
     * @param string $slug
182
     * @param int $id
183
     *
184
     * @return JSON Response containing the new revision that was created from the update
185
     * @return HTTP 500 if the request did not contain the revision ID
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Illuminate\View\Vi...\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...
186
     */
187 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...
188
    {
189
        $model = $this->crud->model;
190
        $entry = $model::findBySlugOrFail($slug);
191
192
        $this->uniqueSetup($entry);
193
194
        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...
195
    }
196
197
    /**
198
     * Setup the controller for the entry.
199
     *
200
     * @param $entry
201
     */
202
    protected function uniqueSetup($entry)
203
    {
204
        $this->setRoute($entry->slug);
205
206
        $this->addDefaultPageFields($entry);
207
        $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), $this->crud->makeLabel($entry->template));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
208
209
        $this->{$entry->template}();
210
    }
211
212
    /*
213
    |--------------------------------------------------------------------------
214
    | SaveActions overrides
215
    |--------------------------------------------------------------------------
216
    */
217
218
    /**
219
     * Overrides trait version to remove 'save_and_back' and 'save_and_new'.
220
     *
221
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
222
     */
223
    public function getSaveAction()
224
    {
225
        $saveCurrent = [
226
            'value' => $this->getSaveActionButtonName('save_and_back'),
227
            'label' => $this->getSaveActionButtonName('save_and_back'),
228
        ];
229
230
        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...
231
            'active'  => $saveCurrent,
232
            'options' => [],
233
        ];
234
    }
235
236
    /**
237
     * Override trait version to not update the session variable.
238
     *
239
     * @param [type] $forceSaveAction [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
240
     */
241
    public function setSaveAction($forceSaveAction = null)
242
    {
243
        // do nothing to preserve session value for other crud
244
    }
245
}
246