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:
Complex classes like SaveActions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SaveActions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait SaveActions |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Get the developer's preference on what save action is the default one |
||
| 11 | * for the current operation. |
||
| 12 | * |
||
| 13 | * @return string |
||
| 14 | */ |
||
| 15 | public function getSaveActionDefaultForCurrentOperation() |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Get the save action with full fallback until default. |
||
| 22 | * |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public function getFallBackSaveAction() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Gets the save action that has the desired order. |
||
| 43 | * |
||
| 44 | * @param int $order |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function getSaveActionByOrder($order) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Allow the developer to register multiple save actions. |
||
| 56 | * |
||
| 57 | * @param array $saveActions |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function addSaveActions($saveActions) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Allow developers to register save action into CRUD. |
||
| 74 | * |
||
| 75 | * @param array $saveAction |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function addSaveAction(array $saveAction) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Replaces setting order or forces some default. |
||
| 101 | * |
||
| 102 | * @param string $saveAction |
||
| 103 | * @param int $wantedOrder |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function orderSaveAction(string $saveAction, int $wantedOrder) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Replace the current save actions with the ones provided. |
||
| 128 | * |
||
| 129 | * @param array $saveActions |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public function replaceSaveActions($saveActions) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Alias function of replaceSaveActions() for CRUD consistency. |
||
| 146 | * |
||
| 147 | * @param array $saveActions |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function setSaveActions($saveActions) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Allow the developer to remove multiple save actions from settings. |
||
| 157 | * |
||
| 158 | * @param array $saveActions |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function removeSaveActions(array $saveActions) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Allow the developer to remove a save action from settings. |
||
| 170 | * |
||
| 171 | * @param string $saveAction |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function removeSaveAction(string $saveAction) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Allow the developer to unset all save actions. |
||
| 185 | * |
||
| 186 | * @param string $saveAction |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function removeAllSaveActions() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Allows the developer to set save actions order. It could be ['action1','action2'] or ['action1' => 1, 'action2' => 2]. |
||
| 196 | * |
||
| 197 | * @param array $saveActions |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function orderSaveActions(array $saveActions) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return the ordered save actions to use in the crud panel. |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function getOrderedSaveActions() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the save actions that passed the visible callback. |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function getVisibleSaveActions() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Gets the current save action for this crud. |
||
| 249 | * |
||
| 250 | * @param array $saveOptions |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function getCurrentSaveAction($saveOptions) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Here we check for save action visibility and prepare the actions array for display. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getSaveAction() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Change the session variable that remembers what to do after the "Save" action. |
||
| 299 | * |
||
| 300 | * @param string|null $forceSaveAction |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | public function setSaveAction($forceSaveAction = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Redirect to the correct URL, depending on which save action has been selected. |
||
| 323 | * |
||
| 324 | * @param string $itemId |
||
| 325 | * |
||
| 326 | * @return \Illuminate\Http\Response |
||
| 327 | */ |
||
| 328 | public function performSaveAction($itemId = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * This functions register Backpack default save actions into CRUD. |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function setupDefaultSaveActions() |
||
| 419 | } |
||
| 420 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.