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) |
||
18 | { |
||
19 | $default_connection = Config::get('database.default'); |
||
20 | $table_prefix = Config::get('database.connections.'.$default_connection.'.prefix'); |
||
21 | |||
22 | $instance = new static(); // create an instance of the model to be able to get the table name |
||
23 | $type = DB::select(DB::raw('SHOW COLUMNS FROM `'.$table_prefix.$instance->getTable().'` WHERE Field = "'.$field_name.'"'))[0]->Type; |
||
24 | preg_match('/^enum\((.*)\)$/', $type, $matches); |
||
25 | $enum = []; |
||
26 | foreach (explode(',', $matches[1]) as $value) { |
||
27 | $enum[] = trim($value, "'"); |
||
28 | } |
||
29 | |||
30 | return $enum; |
||
31 | } |
||
32 | |||
33 | public static function getEnumValuesAsAssociativeArray($field_name) |
||
46 | |||
47 | public static function isColumnNullable($column_name) |
||
62 | |||
63 | /* |
||
64 | |-------------------------------------------------------------------------- |
||
65 | | Methods for Fake Fields functionality (used in PageManager). |
||
66 | |-------------------------------------------------------------------------- |
||
67 | */ |
||
68 | |||
69 | /** |
||
70 | * Add fake fields as regular attributes, even though they are stored as JSON. |
||
71 | * |
||
72 | * @param array $columns - the database columns that contain the JSONs |
||
73 | */ |
||
74 | public function addFakes($columns = ['extras']) |
||
75 | { |
||
76 | foreach ($columns as $key => $column) { |
||
77 | $column_contents = $this->{$column}; |
||
78 | |||
79 | if (! is_object($this->{$column})) { |
||
80 | $column_contents = json_decode($this->{$column}); |
||
81 | } |
||
82 | |||
83 | if ((is_array($column_contents) || $column_contents instanceof Traversable) && count($column_contents)) { |
||
84 | foreach ($column_contents as $fake_field_name => $fake_field_value) { |
||
85 | $this->setAttribute($fake_field_name, $fake_field_value); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return the entity with fake fields as attributes. |
||
93 | * |
||
94 | * @param array $columns - the database columns that contain the JSONs |
||
95 | * |
||
96 | * @return Model |
||
97 | */ |
||
98 | public function withFakes($columns = []) |
||
110 | |||
111 | /** |
||
112 | * Determine if this fake column should get json_encoded or not. |
||
113 | * |
||
114 | * @param $column string fake column name |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function shouldEncodeFake($column) |
||
121 | |||
122 | /* |
||
123 | |-------------------------------------------------------------------------- |
||
124 | | Methods for storing uploaded files (used in CRUD). |
||
125 | |-------------------------------------------------------------------------- |
||
126 | */ |
||
127 | |||
128 | /** |
||
129 | * Handle file upload and DB storage for a file: |
||
130 | * - on CREATE |
||
131 | * - stores the file at the destination path |
||
132 | * - generates a name |
||
133 | * - stores the full path in the DB; |
||
134 | * - on UPDATE |
||
135 | * - if the value is null, deletes the file and sets null in the DB |
||
136 | * - if the value is different, stores the different file and updates DB value. |
||
137 | * |
||
138 | * @param [type] $value Value for that column sent from the input. |
||
139 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
140 | * @param [type] $disk Filesystem disk used to store files. |
||
141 | * @param [type] $destination_path Path in disk where to store the files. |
||
142 | */ |
||
143 | public function uploadFileToDisk($value, $attribute_name, $disk, $destination_path) |
||
174 | |||
175 | /** |
||
176 | * Handle multiple file upload and DB storage: |
||
177 | * - if files are sent |
||
178 | * - stores the files at the destination path |
||
179 | * - generates random names |
||
180 | * - stores the full path in the DB, as JSON array; |
||
181 | * - if a hidden input is sent to clear one or more files |
||
182 | * - deletes the file |
||
183 | * - removes that file from the DB. |
||
184 | * |
||
185 | * @param [type] $value Value for that column sent from the input. |
||
186 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
187 | * @param [type] $disk Filesystem disk used to store files. |
||
188 | * @param [type] $destination_path Path in disk where to store the files. |
||
189 | */ |
||
190 | public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) |
||
226 | |||
227 | /* |
||
228 | |-------------------------------------------------------------------------- |
||
229 | | Methods for working with translatable models. |
||
230 | |-------------------------------------------------------------------------- |
||
231 | */ |
||
232 | |||
233 | /** |
||
234 | * Get the attributes that were casted in the model. |
||
235 | * Used for translations because Spatie/Laravel-Translatable |
||
236 | * overwrites the getCasts() method. |
||
237 | * |
||
238 | * @return [type] [description] |
||
239 | */ |
||
240 | public function getCastedAttributes() |
||
244 | |||
245 | /** |
||
246 | * Check if a model is translatable. |
||
247 | * All translation adaptors must have the translationEnabledForModel() method. |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function translationEnabled() |
||
259 | } |
||
260 |
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
Idable
provides a methodequalsId
that 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.