Laravel-Backpack /
CRUD
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Backpack\CRUD\app\Models\Traits; |
||||
| 4 | |||||
| 5 | use DB; |
||||
| 6 | use Illuminate\Support\Facades\Config; |
||||
| 7 | |||||
| 8 | /* |
||||
| 9 | |-------------------------------------------------------------------------- |
||||
| 10 | | Methods for working with the Enum column in MySQL. |
||||
| 11 | |-------------------------------------------------------------------------- |
||||
| 12 | */ |
||||
| 13 | trait HasEnumFields |
||||
| 14 | { |
||||
| 15 | public static function getPossibleEnumValues($field_name) |
||||
| 16 | { |
||||
| 17 | $instance = new static(); // create an instance of the model to be able to get the table name |
||||
| 18 | |||||
| 19 | $connection = $instance->getConnection(); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 20 | |||||
| 21 | $table_prefix = Config::get('database.connections.'.$connection->getName().'.prefix'); |
||||
| 22 | |||||
| 23 | try { |
||||
| 24 | $select = app()->version() < 10 ? |
||||
|
0 ignored issues
–
show
The method
version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 25 | DB::raw('SHOW COLUMNS FROM `'.$table_prefix.$instance->getTable().'` WHERE Field = "'.$field_name.'"') : |
||||
|
0 ignored issues
–
show
It seems like
getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 26 | DB::raw('SHOW COLUMNS FROM `'.$table_prefix.$instance->getTable().'` WHERE Field = "'.$field_name.'"')->getValue($connection->getQueryGrammar()); |
||||
| 27 | |||||
| 28 | $type = $connection->select($select)[0]->Type; |
||||
| 29 | } catch (\Exception $e) { |
||||
| 30 | abort(500, 'Enum field type is not supported - it only works on MySQL. Please use select_from_array instead.', ['developer-error-exception']); |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | preg_match('/^enum\((.*)\)$/', $type, $matches); |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 34 | $enum = []; |
||||
| 35 | foreach (explode(',', $matches[1]) as $value) { |
||||
| 36 | $enum[] = trim($value, "'"); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | return $enum; |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | public static function getEnumValuesAsAssociativeArray($field_name) |
||||
| 43 | { |
||||
| 44 | $instance = new static(); |
||||
| 45 | $enum_values = $instance->getPossibleEnumValues($field_name); |
||||
| 46 | |||||
| 47 | $array = array_flip($enum_values); |
||||
| 48 | |||||
| 49 | foreach ($array as $key => $value) { |
||||
| 50 | $array[$key] = $key; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | return $array; |
||||
| 54 | } |
||||
| 55 | } |
||||
| 56 |