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:22
created

UniquePageCrudController::addDefaultPageFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
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
9
class UniquePageCrudController extends CrudController
10
{
11
    use SaveActions;
12
    use UniquePages;
13
14
    public function setup()
15
    {
16
        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...
17
18
        $modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page');
19
20
        /*
21
        |--------------------------------------------------------------------------
22
        | BASIC CRUD INFORMATION
23
        |--------------------------------------------------------------------------
24
        */
25
        $this->crud->setModel($modelClass);
26
27
        $this->crud->denyAccess('create');
28
        $this->crud->denyAccess('delete');
29
    }
30
31
    public function uniqueEdit($slug)
32
    {
33
        $model = $this->crud->model;
34
        $entry = $model::findBySlug($slug);
35
36
        if (! $entry) {
37
            $entry = $this->createMissingPage($slug);
38
        }
39
40
        $this->data['entry'] = $entry;
41
42
        $this->addDefaultPageFields($entry);
43
44
        $this->setRoute($slug);
45
        $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), '');
46
47
        $this->{$entry->template}();
48
49
        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...
50
    }
51
52
    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...
53
    {
54
        $this->setRoute($slug);
55
56
        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...
57
    }
58
59
    public function setRoute($slug)
60
    {
61
        $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug);
62
    }
63
64
    /**
65
     * Populate the update form with basic fields, that all pages need.
66
     *
67
     * @param Model $page the page entity
68
     */
69
    public function addDefaultPageFields($page)
70
    {
71
        $this->crud->addField([
72
            'name' => 'template',
73
            'type' => 'hidden',
74
        ]);
75
        $this->crud->addField([
76
            'name' => 'name',
77
            'type' => 'hidden',
78
        ]);
79
        $this->crud->addField([
80
            'name' => 'title',
81
            'type' => 'hidden',
82
        ]);
83
        $this->crud->addField([
84
            'name' => 'slug',
85
            'type' => 'hidden',
86
        ]);
87
88
        $this->crud->addField([
89
            'name' => 'open',
90
            'type' => 'custom_html',
91
            'value' => $page->getOpenButton(),
92
        ]);
93
    }
94
95
    public function getUniquePages()
96
    {
97
        $pages_trait = new \ReflectionClass('App\UniquePages');
98
        $pages = $pages_trait->getMethods(\ReflectionMethod::IS_PRIVATE);
99
100
        return collect($pages)->pluck('name');
101
    }
102
103
    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...
104
    {
105
        $pages = collect($this->getUniquePages());
106
107
        $slugs = $pages->mapWithKeys(function ($page) {
108
            return [str_slug($page) => $page];
109
        });
110
111
        if (! $page = $slugs->pull($slug)) {
112
            abort(404);
113
        }
114
115
        $model = $this->crud->model;
116
117
        return $model::create([
118
            'template' => $page,
119
            'name' => camel_case($page),
120
            'title' => camel_case($page),
121
            'slug' => $slug,
122
        ]);
123
    }
124
125
    /*
126
    |--------------------------------------------------------------------------
127
    | BASIC CRUD INFORMATION
128
    |--------------------------------------------------------------------------
129
    */
130
131
    /**
132
     * Overrides trait version to remove.
133
     */
134
    public function getSaveAction()
135
    {
136
        $saveCurrent = [
137
            'value' => $this->getSaveActionButtonName('save_and_back'),
138
            'label' => $this->getSaveActionButtonName('save_and_back'),
139
        ];
140
141
        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...
142
            'active' => $saveCurrent,
143
            'options' => [],
144
        ];
145
    }
146
147
    public function setSaveAction($forceSaveAction = null)
148
    {
149
        // do nothing to preserve session value for other crud
150
    }
151
}
152