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 |
||
| 9 | trait CrudTrait |
||
| 10 | { |
||
| 11 | /* |
||
| 12 | |-------------------------------------------------------------------------- |
||
| 13 | | Methods for ENUM and SELECT crud fields. |
||
| 14 | |-------------------------------------------------------------------------- |
||
| 15 | */ |
||
| 16 | |||
| 17 | public static function getPossibleEnumValues($field_name) |
||
| 29 | |||
| 30 | public static function getEnumValuesAsAssociativeArray($field_name) |
||
| 43 | |||
| 44 | public static function isColumnNullable($column_name) |
||
| 57 | |||
| 58 | /* |
||
| 59 | |-------------------------------------------------------------------------- |
||
| 60 | | Methods for Fake Fields functionality (used in PageManager). |
||
| 61 | |-------------------------------------------------------------------------- |
||
| 62 | */ |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Add fake fields as regular attributes, even though they are stored as JSON. |
||
| 66 | * |
||
| 67 | * @param array $columns - the database columns that contain the JSONs |
||
| 68 | */ |
||
| 69 | public function addFakes($columns = ['extras']) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Return the entity with fake fields as attributes. |
||
| 88 | * |
||
| 89 | * @param array $columns - the database columns that contain the JSONs |
||
| 90 | * |
||
| 91 | * @return Model |
||
| 92 | */ |
||
| 93 | public function withFakes($columns = []) |
||
| 105 | |||
| 106 | /* |
||
| 107 | |-------------------------------------------------------------------------- |
||
| 108 | | Methods for storing uploaded files (used in CRUD). |
||
| 109 | |-------------------------------------------------------------------------- |
||
| 110 | */ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Handle file upload and DB storage for a file: |
||
| 114 | * - on CREATE |
||
| 115 | * - stores the file at the destination path |
||
| 116 | * - generates a name |
||
| 117 | * - stores the full path in the DB; |
||
| 118 | * - on UPDATE |
||
| 119 | * - if the value is null, deletes the file and sets null in the DB |
||
| 120 | * - if the value is different, stores the different file and updates DB value. |
||
| 121 | * |
||
| 122 | * @param [type] $value Value for that column sent from the input. |
||
| 123 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
| 124 | * @param [type] $disk Filesystem disk used to store files. |
||
| 125 | * @param [type] $destination_path Path in disk where to store the files. |
||
| 126 | */ |
||
| 127 | public function uploadFileToDisk($value, $attribute_name, $disk, $destination_path) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Handle multiple file upload and DB storage: |
||
| 162 | * - if files are sent |
||
| 163 | * - stores the files at the destination path |
||
| 164 | * - generates random names |
||
| 165 | * - stores the full path in the DB, as JSON array; |
||
| 166 | * - if a hidden input is sent to clear one or more files |
||
| 167 | * - deletes the file |
||
| 168 | * - removes that file from the DB. |
||
| 169 | * |
||
| 170 | * @param [type] $value Value for that column sent from the input. |
||
| 171 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
| 172 | * @param [type] $disk Filesystem disk used to store files. |
||
| 173 | * @param [type] $destination_path Path in disk where to store the files. |
||
| 174 | */ |
||
| 175 | public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) |
||
| 211 | |||
| 212 | /* |
||
| 213 | |-------------------------------------------------------------------------- |
||
| 214 | | Methods for working with translatable models. |
||
| 215 | |-------------------------------------------------------------------------- |
||
| 216 | */ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the attributes that were casted in the model. |
||
| 220 | * Used for translations because Spatie/Laravel-Translatable |
||
| 221 | * overwrites the getCasts() method. |
||
| 222 | * |
||
| 223 | * @return [type] [description] |
||
| 224 | */ |
||
| 225 | public function getCastedAttributes() : array |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check if a model is translatable. |
||
| 232 | * All translation adaptors must have the translationEnabledForModel() method. |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function translationEnabled() |
||
| 244 | } |
||
| 245 |
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.