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

Test Setup Failed
Pull Request — master (#3259)
by Cristian
16:32 queued 01:14
created

HasRelationshipFields::getConnectionWithExtraTypeMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits;
4
5
use DB;
6
use Illuminate\Database\Eloquent\Model;
7
use \Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
8
9
10
11
/*
12
|--------------------------------------------------------------------------
13
| Methods for working with relationships inside select/relationship fields.
14
|--------------------------------------------------------------------------
15
*/
16
trait HasRelationshipFields
17
{
18
19
    /**
20
     * Register aditional types in doctrine schema manager for the current connection.
21
     *
22
     * @return DB
23
     */
24
    public function getConnectionWithExtraTypeMappings()
25
    {
26
        $conn = DB::connection($this->getConnectionName());
27
28
        // register the enum, json and jsonb column type, because Doctrine doesn't support it
29
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
30
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('json', 'json_array');
31
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('jsonb', 'json_array');
32
33
        return $conn;
34
    }
35
36
    /**
37
     * Get the model's table name, with the prefix added from the configuration file.
38
     *
39
     * @return string Table name with prefix
40
     */
41
    public function getTableWithPrefix()
42
    {
43
        $prefix = $this->getConnection()->getTablePrefix();
44
        $tableName = $this->getTable();
45
46
        return $prefix.$tableName;
47
    }
48
49
    /**
50
     * Get the column type for a certain db column.
51
     *
52
     * @param  string $columnName Name of the column in the db table.
53
     * @return string             Db column type.
54
     */
55
    public function getColumnType($columnName)
56
    {
57
        $conn = $this->getConnectionWithExtraTypeMappings();
58
        $table = $this->getTableWithPrefix();
59
60
        return $conn->getSchemaBuilder()->getColumnType($table, $columnName);
61
    }
62
63
    /**
64
     * Checks if the given column name is nullable.
65
     *
66
     * @param string $column_name The name of the db column.
67
     * @return bool
68
     */
69
    public static function isColumnNullable($column_name)
70
    {
71
        // create an instance of the model to be able to get the table name
72
        $instance = new static();
73
74
        $conn = $instance->getConnectionWithExtraTypeMappings();
75
        $table = $instance->getTableWithPrefix();
76
77
        // MongoDB columns are alway nullable
78
        if (!in_array($conn->getConfig()['driver'], CRUD::getSqlDriverList()) {
79
            return true;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_RETURN on line 79 at column 12
Loading history...
80
        }
81
82
        try {
83
            // check if the column exists in the database
84
            $column = $conn->getDoctrineColumn($table, $column_name);
85
            // check for NOT NULL
86
            $notNull = $column->getNotnull();
87
            // return the value of nullable (aka the inverse of NOT NULL)
88
            return ! $notNull;
89
        } catch (\Exception $e) {
90
            return true;
91
        }
92
    }
93
}
94