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 Access |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Set an operation as having access using the Settings API. |
||
| 11 | * |
||
| 12 | * @param string $operation |
||
| 13 | * |
||
| 14 | * @return bool |
||
| 15 | */ |
||
| 16 | public function allowAccess($operation) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Disable the access to a certain operation, or the current one. |
||
| 27 | * |
||
| 28 | * @param bool $operation [description] |
||
| 29 | * |
||
| 30 | * @return [type] [description] |
||
| 31 | */ |
||
| 32 | public function denyAccess($operation) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Check if a operation is allowed for a Crud Panel. Return false if not. |
||
| 43 | * |
||
| 44 | * @param string $operation |
||
| 45 | * |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public function hasAccess($operation) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check if any operations are allowed for a Crud Panel. Return false if not. |
||
| 55 | * |
||
| 56 | * @param array $operation_array |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function hasAccessToAny($operation_array) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Check if all operations are allowed for a Crud Panel. Return false if not. |
||
| 73 | * |
||
| 74 | * @param array $operation_array Permissions. |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function hasAccessToAll($operation_array) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Check if a operation is allowed for a Crud Panel. Fail if not. |
||
| 91 | * |
||
| 92 | * @param string $operation |
||
| 93 | * |
||
| 94 | * @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function hasAccessOrFail($operation) |
||
| 106 | } |
||
| 107 |
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.