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
02:41 queued 01:20
created

UniquePageCrudController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 232
Duplicated Lines 7.76 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 23 2
A uniqueEdit() 0 13 2
A update() 0 6 1
A setRoute() 0 4 1
B addDefaultPageFields() 0 26 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

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