Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

HasEnumFields::getPossibleEnumValues()   A
last analyzed

Complexity

Conditions 4
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 12
nop 1
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
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
It seems like getConnection() 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 ignore-call  annotation

19
        /** @scrutinizer ignore-call */ 
20
        $connection = $instance->getConnection();
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
introduced by
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 ignore-call  annotation

24
            $select = app()->/** @scrutinizer ignore-call */ version() < 10 ?
Loading history...
25
                        DB::raw('SHOW COLUMNS FROM `'.$table_prefix.$instance->getTable().'` WHERE Field = "'.$field_name.'"') :
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

25
                        DB::raw('SHOW COLUMNS FROM `'.$table_prefix.$instance->/** @scrutinizer ignore-call */ getTable().'` WHERE Field = "'.$field_name.'"') :
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
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
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