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) |
||
45 | { |
||
46 | // create an instance of the model to be able to get the table name |
||
47 | $instance = new static(); |
||
48 | |||
49 | $conn = DB::connection($instance->getConnectionName()); |
||
50 | $table = Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable(); |
||
51 | |||
52 | // register the enum column type, because Doctrine doesn't support it |
||
53 | $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
||
54 | |||
55 | return ! $conn->getDoctrineColumn($table, $column_name)->getNotnull(); |
||
56 | } |
||
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']) |
||
70 | { |
||
71 | foreach ($columns as $key => $column) { |
||
72 | $column_contents = $this->{$column}; |
||
73 | |||
74 | if (! is_object($this->{$column})) { |
||
75 | $column_contents = json_decode($this->{$column}); |
||
76 | } |
||
77 | |||
78 | if (count($column_contents)) { |
||
79 | foreach ($column_contents as $fake_field_name => $fake_field_value) { |
||
80 | $this->setAttribute($fake_field_name, $fake_field_value); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | } |
||
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 = []) |
||
94 | { |
||
95 | $model = '\\'.get_class($this); |
||
96 | |||
97 | if (! count($columns)) { |
||
98 | $columns = (property_exists($model, 'fakeColumns')) ? $this->fakeColumns : ['extras']; |
||
99 | } |
||
100 | |||
101 | $this->addFakes($columns); |
||
102 | |||
103 | return $this; |
||
104 | } |
||
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) |
||
128 | { |
||
129 | $request = \Request::instance(); |
||
130 | |||
131 | // if a new file is uploaded, delete the file from the disk |
||
132 | if ($request->hasFile($attribute_name) && |
||
133 | $this->{$attribute_name} && |
||
134 | $this->{$attribute_name} != null) { |
||
135 | \Storage::disk($disk)->delete($this->{$attribute_name}); |
||
136 | $this->attributes[$attribute_name] = null; |
||
137 | } |
||
138 | |||
139 | // if the file input is empty, delete the file from the disk |
||
140 | if (is_null($value) && $this->{$attribute_name} != null) { |
||
141 | \Storage::disk($disk)->delete($this->{$attribute_name}); |
||
142 | $this->attributes[$attribute_name] = null; |
||
143 | } |
||
144 | |||
145 | // if a new file is uploaded, store it on disk and its filename in the database |
||
146 | View Code Duplication | if ($request->hasFile($attribute_name) && $request->file($attribute_name)->isValid()) { |
|
147 | // 1. Generate a new file name |
||
148 | $file = $request->file($attribute_name); |
||
149 | $new_file_name = md5($file->getClientOriginalName().time()).'.'.$file->getClientOriginalExtension(); |
||
150 | |||
151 | // 2. Move the new file to the correct path |
||
152 | $file_path = $file->storeAs($destination_path, $new_file_name, $disk); |
||
153 | |||
154 | // 3. Save the complete path to the database |
||
155 | $this->attributes[$attribute_name] = $file_path; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Handle multiple file upload and DB storage: |
||
161 | * - if files are sent |
||
162 | * - stores the files at the destination path |
||
163 | * - generates random names |
||
164 | * - stores the full path in the DB, as JSON array; |
||
165 | * - if a hidden input is sent to clear one or more files |
||
166 | * - deletes the file |
||
167 | * - removes that file from the DB. |
||
168 | * |
||
169 | * @param [type] $value Value for that column sent from the input. |
||
170 | * @param [type] $attribute_name Model attribute name (and column in the db). |
||
171 | * @param [type] $disk Filesystem disk used to store files. |
||
172 | * @param [type] $destination_path Path in disk where to store the files. |
||
173 | */ |
||
174 | public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) |
||
210 | |||
211 | /* |
||
212 | |-------------------------------------------------------------------------- |
||
213 | | Methods for working with translatable models. |
||
214 | |-------------------------------------------------------------------------- |
||
215 | */ |
||
216 | |||
217 | /** |
||
218 | * Get the attributes that were casted in the model. |
||
219 | * Used for translations because Spatie/Laravel-Translatable |
||
220 | * overwrites the getCasts() method. |
||
221 | * |
||
222 | * @return [type] [description] |
||
223 | */ |
||
224 | public function getCastedAttributes() |
||
228 | |||
229 | /** |
||
230 | * Check if a model is translatable. |
||
231 | * All translation adaptors must have the translationEnabledForModel() method. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function translationEnabled() |
||
243 | } |
||
244 |
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.