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
01:31
created

UniquePageCrudController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 233
Duplicated Lines 7.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
c 4
b 1
f 1
lcom 1
cbo 6
dl 18
loc 233
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
B setup() 0 24 2
A uniqueEdit() 0 13 2
A update() 0 6 1
A setRoute() 0 4 1
A buttons() 0 7 1
A createMissingPage() 0 18 2
A uniqueRevisions() 9 9 1
A restoreUniqueRevision() 9 9 1
A uniqueSetup() 0 9 1
A getSaveAction() 0 12 1
A setSaveAction() 0 4 1
B addDefaultPageFields() 0 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Backpack\PageManager\app\Http\Controllers\Admin;
4
5
use App\UniquePages;
6
use Backpack\PageManager\app\TraitReflections;
7
use Backpack\CRUD\app\Http\Controllers\CrudController;
8
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions;
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 cannot 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
     * 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
    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...
69
    {
70
        $this->setRoute($slug);
71
72
        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...
73
    }
74
75
    /**
76
     * Set the crud route.
77
     *
78
     * @param $slug
79
     */
80
    public function setRoute($slug)
81
    {
82
        $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug);
83
    }
84
85
    /**
86
     * Populate the update form with basic fields that all pages need.
87
     *
88
     * @param Model $page
89
     */
90
    public function addDefaultPageFields($page)
91
    {
92
        $fields = [
93
            [
94
                'name' => 'buttons',
95
                'type' => 'custom_html',
96
                'value' => $this->buttons($page),
97
            ],
98
            [
99
                'name' => 'template',
100
                'type' => 'hidden',
101
            ],
102
            [
103
                'name' => 'name',
104
            ],
105
            [
106
                'name' => 'title',
107
            ],
108
            [
109
                'name' => 'slug',
110
                'type' => 'hidden',
111
            ],
112
        ];
113
114
        $this->crud->addFields($fields);
115
    }
116
117
    /**
118
     * Build the buttons html for the edit form.
119
     *
120
     * @param Model $page
121
     * @return string
122
     */
123
    public function buttons($page)
124
    {
125
        $openButton = $page->getOpenButton();
126
        $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]);
127
128
        return $openButton.' '.$revisionsButton;
129
    }
130
131
    /**
132
     * Create the page by slug.
133
     *
134
     * @param $slug
135
     * @return mixed
136
     */
137
    public function createMissingPage($slug)
138
    {
139
        $slugs = $this->getUniqueSlugs();
140
141
        if (! $slugs->has($slug)) {
142
            abort(404);
143
        }
144
145
        $page = $slugs->pull($slug);
146
        $model = $this->crud->model;
147
148
        return $model::create([
149
            'template' => $page,
150
            'name' => camel_case($page),
151
            'title' => camel_case($page),
152
            'slug' => $slug,
153
        ]);
154
    }
155
156
    /**
157
     * Display the revisions for specified resource.
158
     *
159
     * @param $slug
160
     * @param $id
161
     * @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...
162
     */
163 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...
164
    {
165
        $model = $this->crud->model;
166
        $entry = $model::findBySlugOrFail($slug);
167
168
        $this->uniqueSetup($entry);
169
170
        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...
171
    }
172
173
    /**
174
     * Restore a specific revision for the specified resource.
175
     *
176
     * Used via AJAX in the revisions view
177
     *
178
     * @param string $slug
179
     * @param int $id
180
     *
181
     * @return JSON Response containing the new revision that was created from the update
182
     * @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...
183
     */
184 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...
185
    {
186
        $model = $this->crud->model;
187
        $entry = $model::findBySlugOrFail($slug);
188
189
        $this->uniqueSetup($entry);
190
191
        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...
192
    }
193
194
    /**
195
     * Setup the controller for the entry.
196
     *
197
     * @param $entry
198
     */
199
    protected function uniqueSetup($entry)
200
    {
201
        $this->setRoute($entry->slug);
202
203
        $this->addDefaultPageFields($entry);
204
        $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), '');
205
206
        $this->{$entry->template}();
207
    }
208
209
    /*
210
    |--------------------------------------------------------------------------
211
    | SaveActions overrides
212
    |--------------------------------------------------------------------------
213
    */
214
215
    /**
216
     * Overrides trait version to remove 'save_and_back' and 'save_and_new'.
217
     *
218
     * @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...
219
     */
220
    public function getSaveAction()
221
    {
222
        $saveCurrent = [
223
            'value' => $this->getSaveActionButtonName('save_and_back'),
224
            'label' => $this->getSaveActionButtonName('save_and_back'),
225
        ];
226
227
        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...
228
            'active' => $saveCurrent,
229
            'options' => [],
230
        ];
231
    }
232
233
    /**
234
     * Override trait version to not update the session variable.
235
     *
236
     * @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...
237
     */
238
    public function setSaveAction($forceSaveAction = null)
239
    {
240
        // do nothing to preserve session value for other crud
241
    }
242
}
243