We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 |
||
| 10 | class UniquePageCrudController extends CrudController |
||
| 11 | { |
||
| 12 | use SaveActions; |
||
| 13 | use UniquePages; |
||
| 14 | use TraitReflections; |
||
| 15 | |||
| 16 | public function setup() |
||
| 17 | { |
||
| 18 | parent::__construct(); |
||
|
|
|||
| 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 | * As we want to edit pages by slug we need a new edit function. |
||
| 43 | * |
||
| 44 | * @param string $slug the page slug |
||
| 45 | * @return Response |
||
| 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); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function update($slug, $id) |
||
| 62 | { |
||
| 63 | $this->setRoute($slug); |
||
| 64 | |||
| 65 | return parent::updateCrud(); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function setRoute($slug) |
||
| 69 | { |
||
| 70 | $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Populate the update form with basic fields, that all pages need. |
||
| 75 | * |
||
| 76 | * @param Model $page the page entity |
||
| 77 | */ |
||
| 78 | public function addDefaultPageFields($page) |
||
| 79 | { |
||
| 80 | $this->crud->addField([ |
||
| 81 | 'name' => 'template', |
||
| 82 | 'type' => 'hidden', |
||
| 83 | ]); |
||
| 84 | $this->crud->addField([ |
||
| 85 | 'name' => 'name', |
||
| 86 | 'type' => 'hidden', |
||
| 87 | ]); |
||
| 88 | $this->crud->addField([ |
||
| 89 | 'name' => 'title', |
||
| 90 | 'type' => 'hidden', |
||
| 91 | ]); |
||
| 92 | $this->crud->addField([ |
||
| 93 | 'name' => 'slug', |
||
| 94 | 'type' => 'hidden', |
||
| 95 | ]); |
||
| 96 | |||
| 97 | $this->crud->addField([ |
||
| 98 | 'name' => 'open', |
||
| 99 | 'type' => 'custom_html', |
||
| 100 | 'value' => $this->buttons($page), |
||
| 101 | ]); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function buttons($page) |
||
| 105 | { |
||
| 106 | $openButton = $page->getOpenButton(); |
||
| 107 | $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]); |
||
| 108 | |||
| 109 | return $openButton.' '.$revisionsButton; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function createMissingPage($slug) |
||
| 133 | |||
| 134 | View Code Duplication | public function uniqueRevisions($slug, $id) |
|
| 143 | |||
| 144 | View Code Duplication | public function restoreUniqueRevision($slug, $id) |
|
| 153 | |||
| 154 | protected function uniqueSetup($entry) |
||
| 163 | |||
| 164 | /* |
||
| 165 | |-------------------------------------------------------------------------- |
||
| 166 | | SaveActions overrides |
||
| 167 | |-------------------------------------------------------------------------- |
||
| 168 | */ |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Overrides trait version to remove. |
||
| 172 | */ |
||
| 173 | public function getSaveAction() |
||
| 185 | |||
| 186 | public function setSaveAction($forceSaveAction = null) |
||
| 190 | } |
||
| 191 |
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:
The
getFirstName()method in theSoncalls the wrong method in the parent class.