Type   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 96
c 1
b 0
f 0
dl 0
loc 152
ccs 0
cts 117
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A registerCustomTypes() 0 22 4
A getCustomTypes() 0 12 2
B getTypeCategories() 0 84 1
1
<?php
2
3
namespace CodexShaper\DBM\Database\Types;
4
5
use CodexShaper\DBM\Database\Schema\SchemaManager;
6
use Doctrine\DBAL\Types\Type as DoctrineType;
7
8
abstract class Type extends DoctrineType
9
{
10
    /**
11
     * Get type name.
12
     *
13
     * @return string
14
     */
15
    public function getName()
16
    {
17
        return static::NAME;
0 ignored issues
show
Bug introduced by
The constant CodexShaper\DBM\Database\Types\Type::NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
    }
19
20
    /**
21
     * Register Custom type.
22
     *
23
     * @return void
24
     */
25
    public static function registerCustomTypes()
26
    {
27
        $platform = SchemaManager::getInstance()->getDatabasePlatform();
28
        $platformName = ucfirst($platform->getName());
29
30
        $customTypes = array_merge(
31
            static::getCustomTypes('Common'),
32
            static::getCustomTypes($platformName)
33
        );
34
35
        foreach ($customTypes as $type) {
36
            $name = $type::NAME;
37
38
            if (static::hasType($name)) {
39
                static::overrideType($name, $type);
40
            } else {
41
                static::addType($name, $type);
42
            }
43
44
            $dbType = defined("{$type}::DBTYPE") ? $type::DBTYPE : $name;
45
46
            $platform->registerDoctrineTypeMapping($dbType, $name);
47
        }
48
    }
49
50
    /**
51
     * Get custom types.
52
     *
53
     * @param string $platformName
54
     *
55
     * @return array
56
     */
57
    protected static function getCustomTypes($platformName)
58
    {
59
        $customPlatformDir = __DIR__.DIRECTORY_SEPARATOR.$platformName.DIRECTORY_SEPARATOR;
60
61
        $customTypes = [];
62
63
        foreach (glob($customPlatformDir.'*.php') as $file) {
64
            $className = basename($file, '.php');
65
            $customTypes[] = __NAMESPACE__.'\\'.$platformName.'\\'.$className;
66
        }
67
68
        return $customTypes;
69
    }
70
71
    /**
72
     * Get Type categories.
73
     *
74
     * @return array
75
     */
76
    public static function getTypeCategories()
77
    {
78
        return [
79
            'numbers' => [
80
                'boolean',
81
                'tinyint',
82
                'smallint',
83
                'mediumint',
84
                'integer',
85
                'int',
86
                'bigint',
87
                'decimal',
88
                'numeric',
89
                'money',
90
                'float',
91
                'real',
92
                'double',
93
                'double precision',
94
            ],
95
            'strings' => [
96
                'char',
97
                'character',
98
                'varchar',
99
                'character varying',
100
                'string',
101
                'guid',
102
                'uuid',
103
                'tinytext',
104
                'text',
105
                'mediumtext',
106
                'longtext',
107
                'tsquery',
108
                'tsvector',
109
                'xml',
110
            ],
111
            'datetime' => [
112
                'date',
113
                'datetime',
114
                'year',
115
                'time',
116
                'timetz',
117
                'timestamp',
118
                'timestamptz',
119
                'datetimetz',
120
                'dateinterval',
121
                'interval',
122
            ],
123
            'lists' => [
124
                'enum',
125
                'set',
126
                'simple_array',
127
                'array',
128
                'json',
129
                'jsonb',
130
                'json_array',
131
            ],
132
            'binary' => [
133
                'bit',
134
                'bit varying',
135
                'binary',
136
                'varbinary',
137
                'tinyblob',
138
                'blob',
139
                'mediumblob',
140
                'longblob',
141
                'bytea',
142
            ],
143
            'network' => [
144
                'cidr',
145
                'inet',
146
                'macaddr',
147
                'txid_snapshot',
148
            ],
149
            'geometry' => [
150
                'geometry',
151
                'point',
152
                'linestring',
153
                'polygon',
154
                'multipoint',
155
                'multilinestring',
156
                'multipolygon',
157
                'geometrycollection',
158
            ],
159
            'objects' => ['object'],
160
        ];
161
    }
162
}
163