Passed
Push — task/laravel-breadcrumbs ( 3beccb...a96280 )
by Yonathan
10:46 queued 10s
created

TalentCloudCrudTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 12
c 0
b 0
f 0
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A isColumnNullable() 0 16 2
1
<?php
2
3
namespace App\Traits;
4
5
use Backpack\CRUD\app\Models\Traits\CrudTrait;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Config;
8
9
trait TalentCloudCrudTrait
10
{
11
    use CrudTrait;
12
13
    /**
14
     * Override for existing isColumnNullable static function on the CrudTrait. Current
15
     * implementation doesn't allow for other custom column types outside of what's defined in
16
     * Backpack.
17
     *
18
     * @param string $column_name Name of the column to check.
19
     *
20
     * @return boolean
1 ignored issue
show
introduced by
Method \App\Traits\TalentCloudCrudTrait::isColumnNullable() has useless @return annotation.
Loading history...
21
     */
22
    public static function isColumnNullable(string $column_name) : bool
23
    {
24
        // Create an instance of the model to be able to get the table name.
25
        $instance = new static();
26
        $conn = DB::connection($instance->getConnectionName());
27
        $table = Config::get('database.connections.' . Config::get('database.default') . '.prefix') . $instance->getTable();
28
        // MongoDB columns are alway nullable.
29
        if ($conn->getConfig()['driver'] === 'mongodb') {
30
            return true;
31
        }
32
        // Register the enum, json, jsonb, and citext column types, because Doctrine doesn't support it.
33
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
34
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('json', 'json_array');
35
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('jsonb', 'json_array');
36
        $conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('citext', 'string');
37
        return !$conn->getDoctrineColumn($table, $column_name)->getNotnull();
38
    }
39
}
40