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 | trait CrudTrait |
||
| 11 | { |
||
| 12 | /* |
||
| 13 | |-------------------------------------------------------------------------- |
||
| 14 | | Methods for ENUM and SELECT crud fields. |
||
| 15 | |-------------------------------------------------------------------------- |
||
| 16 | */ |
||
| 17 | |||
| 18 | public static function getPossibleEnumValues($field_name) |
||
| 34 | |||
| 35 | public static function getEnumValuesAsAssociativeArray($field_name) |
||
| 48 | |||
| 49 | public static function isColumnNullable($column_name) |
||
| 64 | |||
| 65 | /* |
||
| 66 | |-------------------------------------------------------------------------- |
||
| 67 | | Methods for Fake Fields functionality (used in PageManager). |
||
| 68 | |-------------------------------------------------------------------------- |
||
| 69 | */ |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add fake fields as regular attributes, even though they are stored as JSON. |
||
| 73 | * |
||
| 74 | * @param array $columns - the database columns that contain the JSONs |
||
| 75 | */ |
||
| 76 | public function addFakes($columns = ['extras']) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return the entity with fake fields as attributes. |
||
| 99 | * |
||
| 100 | * @param array $columns - the database columns that contain the JSONs |
||
| 101 | * |
||
| 102 | * @return Model |
||
| 103 | */ |
||
| 104 | public function withFakes($columns = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Determine if this fake column should be json_decoded. |
||
| 119 | * |
||
| 120 | * @param $column string fake column name |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function shouldDecodeFake($column) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Determine if this fake column should get json_encoded or not. |
||
| 130 | * |
||
| 131 | * @param $column string fake column name |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function shouldEncodeFake($column) |
||
| 138 | |||
| 139 | /* |
||
| 140 | |-------------------------------------------------------------------------- |
||
| 141 | | Methods for storing uploaded files (used in CRUD). |
||
| 142 | |-------------------------------------------------------------------------- |
||
| 143 | */ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Handle file upload and DB storage for a file: |
||
| 147 | * - on CREATE |
||
| 148 | * - stores the file at the destination path |
||
| 149 | * - generates a name |
||
| 150 | * - stores the full path in the DB; |
||
| 151 | * - on UPDATE |
||
| 152 | * - if the value is null, deletes the file and sets null in the DB |
||
| 153 | * - if the value is different, stores the different file and updates DB value. |
||
| 154 | * |
||
| 155 | * @param [type] $value Value for that column sent from the input. |
||
| 156 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
| 157 | * @param [type] $disk Filesystem disk used to store files. |
||
| 158 | * @param [type] $destination_path Path in disk where to store the files. |
||
| 159 | */ |
||
| 160 | public function uploadFileToDisk($value, $attribute_name, $disk, $destination_path) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Handle multiple file upload and DB storage: |
||
| 194 | * - if files are sent |
||
| 195 | * - stores the files at the destination path |
||
| 196 | * - generates random names |
||
| 197 | * - stores the full path in the DB, as JSON array; |
||
| 198 | * - if a hidden input is sent to clear one or more files |
||
| 199 | * - deletes the file |
||
| 200 | * - removes that file from the DB. |
||
| 201 | * |
||
| 202 | * @param [type] $value Value for that column sent from the input. |
||
| 203 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
| 204 | * @param [type] $disk Filesystem disk used to store files. |
||
| 205 | * @param [type] $destination_path Path in disk where to store the files. |
||
| 206 | */ |
||
| 207 | public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) |
||
| 243 | |||
| 244 | /* |
||
| 245 | |-------------------------------------------------------------------------- |
||
| 246 | | Methods for working with translatable models. |
||
| 247 | |-------------------------------------------------------------------------- |
||
| 248 | */ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the attributes that were casted in the model. |
||
| 252 | * Used for translations because Spatie/Laravel-Translatable |
||
| 253 | * overwrites the getCasts() method. |
||
| 254 | * |
||
| 255 | * @return [type] [description] |
||
| 256 | */ |
||
| 257 | public function getCastedAttributes() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Check if a model is translatable. |
||
| 264 | * All translation adaptors must have the translationEnabledForModel() method. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function translationEnabled() |
||
| 276 | } |
||
| 277 |
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.