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 |
||
| 7 | trait Buttons |
||
| 8 | { |
||
| 9 | // ------------ |
||
| 10 | // BUTTONS |
||
| 11 | // ------------ |
||
| 12 | |||
| 13 | // TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Add a button to the CRUD table view. |
||
| 17 | * |
||
| 18 | * @param string $stack Where should the button be visible? Options: top, line, bottom. |
||
| 19 | * @param string $name The name of the button. Unique. |
||
| 20 | * @param string $type Type of button: view or model_function. |
||
| 21 | * @param string $content The HTML for the button. |
||
| 22 | * @param bool|string $position Position on the stack: beginning or end. If false, the position will be |
||
| 23 | * 'beginning' for the line stack or 'end' otherwise. |
||
| 24 | * @param bool $replaceExisting True if a button with the same name on the given stack should be replaced. |
||
| 25 | * |
||
| 26 | * @return \Backpack\CRUD\PanelTraits\CrudButton The new CRUD button. |
||
| 27 | */ |
||
| 28 | 203 | public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true) |
|
| 29 | { |
||
| 30 | 203 | if ($position == false) { |
|
| 31 | 203 | switch ($stack) { |
|
| 32 | 203 | case 'line': |
|
| 33 | 1 | $position = 'beginning'; |
|
| 34 | 1 | break; |
|
| 35 | |||
| 36 | default: |
||
| 37 | 203 | $position = 'end'; |
|
| 38 | 203 | break; |
|
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | 203 | if ($replaceExisting) { |
|
| 43 | 203 | $this->removeButton($name, $stack); |
|
| 44 | } |
||
| 45 | |||
| 46 | 203 | $button = new CrudButton($stack, $name, $type, $content); |
|
| 47 | 203 | switch ($position) { |
|
| 48 | 203 | case 'beginning': |
|
| 49 | 2 | $this->buttons->prepend($button); |
|
|
|
|||
| 50 | 2 | break; |
|
| 51 | |||
| 52 | default: |
||
| 53 | 203 | $this->buttons->push($button); |
|
| 54 | 203 | break; |
|
| 55 | } |
||
| 56 | |||
| 57 | 203 | return $button; |
|
| 58 | } |
||
| 59 | |||
| 60 | public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false) |
||
| 64 | |||
| 65 | 1 | public function addButtonFromView($stack, $name, $view, $position = false) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return Collection |
||
| 74 | */ |
||
| 75 | public function buttons() |
||
| 79 | |||
| 80 | 203 | public function initButtons() |
|
| 81 | { |
||
| 82 | 203 | $this->buttons = collect(); |
|
| 83 | |||
| 84 | // line stack |
||
| 85 | 203 | $this->addButton('line', 'show', 'view', 'crud::buttons.show', 'end'); |
|
| 86 | 203 | $this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
|
| 87 | 203 | $this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end'); |
|
| 88 | 203 | $this->addButton('line', 'clone', 'view', 'crud::buttons.clone', 'end'); |
|
| 89 | 203 | $this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end'); |
|
| 90 | |||
| 91 | // top stack |
||
| 92 | 203 | $this->addButton('top', 'create', 'view', 'crud::buttons.create'); |
|
| 93 | 203 | $this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder'); |
|
| 94 | 203 | } |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Modify the attributes of a button. |
||
| 98 | * |
||
| 99 | * @param string $name The button name. |
||
| 100 | * @param array $modifications The attributes and their new values. |
||
| 101 | * |
||
| 102 | * @return CrudButton The button that has suffered the changes, for daisychaining methods. |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function modifyButton($name, $modifications = null) |
|
| 105 | { |
||
| 106 | /** |
||
| 107 | * @var CrudButton|null |
||
| 108 | */ |
||
| 109 | $button = $this->buttons()->firstWhere('name', $name); |
||
| 110 | |||
| 111 | if (! $button) { |
||
| 112 | abort(500, 'CRUD Button "'.$name.'" not found. Please check the button exists before you modify it.'); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (is_array($modifications)) { |
||
| 116 | foreach ($modifications as $key => $value) { |
||
| 117 | $button->{$key} = $value; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $button; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Remove a button from the CRUD panel. |
||
| 126 | * |
||
| 127 | * @param string $name Button name. |
||
| 128 | * @param string $stack Optional stack name. |
||
| 129 | */ |
||
| 130 | 203 | public function removeButton($name, $stack = null) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param array $names Button names |
||
| 139 | * @param string|null $stack Optional stack name. |
||
| 140 | */ |
||
| 141 | 2 | public function removeButtons($names, $stack = null) |
|
| 142 | { |
||
| 143 | 2 | if (! empty($names)) { |
|
| 144 | 2 | foreach ($names as $name) { |
|
| 145 | 2 | $this->removeButton($name, $stack); |
|
| 146 | } |
||
| 147 | } |
||
| 148 | 2 | } |
|
| 149 | |||
| 150 | 1 | public function removeAllButtons() |
|
| 154 | |||
| 155 | 2 | public function removeAllButtonsFromStack($stack) |
|
| 161 | |||
| 162 | 3 | public function removeButtonFromStack($name, $stack) |
|
| 168 | } |
||
| 169 | |||
| 185 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: