Completed
Pull Request — master (#3354)
by Michael
61:13
created

Type::createTypeRegistry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\ParameterType;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use function array_map;
9
use function get_class;
10
use function str_replace;
11
use function strrpos;
12
use function substr;
13
14
/**
15
 * The base class for so-called Doctrine mapping types.
16
 *
17
 * A Type object is obtained by calling the static {@link getType()} method.
18
 */
19
abstract class Type
20
{
21
    public const BIGINT               = 'bigint';
22
    public const BINARY               = 'binary';
23
    public const BLOB                 = 'blob';
24
    public const BOOLEAN              = 'boolean';
25
    public const DATE                 = 'date';
26
    public const DATE_IMMUTABLE       = 'date_immutable';
27
    public const DATEINTERVAL         = 'dateinterval';
28
    public const DATETIME             = 'datetime';
29
    public const DATETIME_IMMUTABLE   = 'datetime_immutable';
30
    public const DATETIMETZ           = 'datetimetz';
31
    public const DATETIMETZ_IMMUTABLE = 'datetimetz_immutable';
32
    public const DECIMAL              = 'decimal';
33
    public const FLOAT                = 'float';
34
    public const GUID                 = 'guid';
35
    public const INTEGER              = 'integer';
36
    public const JSON                 = 'json';
37
    public const JSON_ARRAY           = 'json_array';
38
    public const OBJECT               = 'object';
39
    public const SIMPLE_ARRAY         = 'simple_array';
40
    public const SMALLINT             = 'smallint';
41
    public const STRING               = 'string';
42
    public const TARRAY               = 'array';
43
    public const TEXT                 = 'text';
44
    public const TIME                 = 'time';
45
    public const TIME_IMMUTABLE       = 'time_immutable';
46
47
    /**
48
     * The map of supported doctrine mapping types.
49
     */
50
    private const BUILTIN_TYPES_MAP = [
51
        self::BIGINT               => BigIntType::class,
52
        self::BINARY               => BinaryType::class,
53
        self::BLOB                 => BlobType::class,
54
        self::BOOLEAN              => BooleanType::class,
55
        self::DATE                 => DateType::class,
56
        self::DATE_IMMUTABLE       => DateImmutableType::class,
57
        self::DATEINTERVAL         => DateIntervalType::class,
58
        self::DATETIME             => DateTimeType::class,
59
        self::DATETIME_IMMUTABLE   => DateTimeImmutableType::class,
60
        self::DATETIMETZ           => DateTimeTzType::class,
61
        self::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
62
        self::DECIMAL              => DecimalType::class,
63
        self::FLOAT                => FloatType::class,
64
        self::GUID                 => GuidType::class,
65
        self::INTEGER              => IntegerType::class,
66
        self::JSON                 => JsonType::class,
67
        self::JSON_ARRAY           => JsonArrayType::class,
68
        self::OBJECT               => ObjectType::class,
69
        self::SIMPLE_ARRAY         => SimpleArrayType::class,
70
        self::SMALLINT             => SmallIntType::class,
71
        self::STRING               => StringType::class,
72
        self::TARRAY               => ArrayType::class,
73
        self::TEXT                 => TextType::class,
74
        self::TIME                 => TimeType::class,
75
        self::TIME_IMMUTABLE       => TimeImmutableType::class,
76
    ];
77
78
    /** @var TypeRegistry|null */
79
    private static $typeRegistry;
80
81
    /**
82
     * @internal Do not instantiate directly - use {@see Type::addType()} method instead.
83
     */
84
    final public function __construct()
85
    {
86
    }
87
88 37539
    /**
89
     * Converts a value from its PHP representation to its database representation
90 37539
     * of this type.
91
     *
92
     * @param mixed            $value    The value to convert.
93
     * @param AbstractPlatform $platform The currently used database platform.
94
     *
95
     * @return mixed The database representation of the value.
96
     */
97
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

97
    public function convertToDatabaseValue($value, /** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        return $value;
100
    }
101 39234
102
    /**
103 39234
     * Converts a value from its database representation to its PHP representation
104
     * of this type.
105
     *
106
     * @param mixed            $value    The value to convert.
107
     * @param AbstractPlatform $platform The currently used database platform.
108
     *
109
     * @return mixed The PHP representation of the value.
110
     */
111
    public function convertToPHPValue($value, AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    public function convertToPHPValue($value, /** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        return $value;
114
    }
115 35659
116
    /**
117 35659
     * Gets the default length of this type.
118
     *
119
     * @deprecated Rely on information provided by the platform instead.
120
     *
121
     * @return int|null
122
     */
123
    public function getDefaultLength(AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

123
    public function getDefaultLength(/** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125
        return null;
126
    }
127
128
    /**
129
     * Gets the SQL declaration snippet for a field of this type.
130
     *
131
     * @param mixed[]          $fieldDeclaration The field declaration.
132
     * @param AbstractPlatform $platform         The currently used database platform.
133
     *
134
     * @return string
135
     */
136
    abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
137
138
    /**
139
     * Gets the name of this type.
140
     *
141
     * @return string
142
     *
143
     * @todo Needed?
144
     */
145
    abstract public function getName();
146
147
    /**
148
     * @internal This method is only to be used within DBAL for forward compatibility purposes. Do not use directly.
149
     */
150
    final public static function getTypeRegistry() : TypeRegistry
151
    {
152
        if (self::$typeRegistry === null) {
153
            self::$typeRegistry = self::createTypeRegistry();
154
        }
155
156
        return self::$typeRegistry;
157
    }
158
159
    private static function createTypeRegistry() : TypeRegistry
160
    {
161 42822
        $registry = new TypeRegistry();
162
163 42822
        foreach (self::BUILTIN_TYPES_MAP as $name => $class) {
164 37539
            $registry->register($name, new $class());
165
        }
166
167 37539
        return $registry;
168
    }
169
170 42822
    /**
171
     * Factory method to create type instances.
172
     * Type instances are implemented as flyweights.
173
     *
174
     * @param string $name The name of the type (as returned by getName()).
175
     *
176
     * @return \Doctrine\DBAL\Types\Type
177
     *
178
     * @throws DBALException
179
     */
180
    public static function getType($name)
181
    {
182
        return self::getTypeRegistry()->get($name);
183 37553
    }
184
185 37553
    /**
186
     * Adds a custom type to the type map.
187
     *
188
     * @param string $name      The name of the type. This should correspond to what getName() returns.
189 37553
     * @param string $className The class name of the custom type.
190 37553
     *
191
     * @return void
192
     *
193
     * @throws DBALException
194
     */
195
    public static function addType($name, $className)
196
    {
197
        self::getTypeRegistry()->register($name, new $className());
198
    }
199 37852
200
    /**
201 37852
     * Checks if exists support for a type.
202
     *
203
     * @param string $name The name of the type.
204
     *
205
     * @return bool TRUE if type is supported; FALSE otherwise.
206
     */
207
    public static function hasType($name)
208
    {
209
        return self::getTypeRegistry()->has($name);
210
    }
211
212
    /**
213
     * Overrides an already defined type to use a different implementation.
214
     *
215
     * @param string $name
216
     * @param string $className
217
     *
218
     * @return void
219
     *
220
     * @throws DBALException
221
     */
222
    public static function overrideType($name, $className)
223
    {
224
        self::getTypeRegistry()->override($name, new $className());
225
    }
226
227
    /**
228
     * Gets the (preferred) binding type for values of this type that
229
     * can be used when binding parameters to prepared statements.
230
     *
231
     * This method should return one of the {@link \Doctrine\DBAL\ParameterType} constants.
232
     *
233
     * @return int
234
     */
235 39247
    public function getBindingType()
236
    {
237 39247
        return ParameterType::STRING;
238
    }
239
240
    /**
241
     * Gets the types array map which holds all registered types and the corresponding
242
     * type class
243
     *
244
     * @return string[]
245
     */
246 41323
    public static function getTypesMap()
247
    {
248 41323
        return array_map(
249
            static function (Type $type) : string {
250
                return get_class($type);
251
            },
252
            self::getTypeRegistry()->getMap()
253
        );
254
    }
255
256
    /**
257
     * @deprecated Relying on string representation is discouraged and will be removed in DBAL 3.0.
258
     *
259
     * @return string
260
     */
261
    public function __toString()
262
    {
263
        $type     = static::class;
264
        $position = strrpos($type, '\\');
265
266
        if ($position !== false) {
267
            $type = substr($type, $position);
268
        }
269
270
        return str_replace('Type', '', $type);
271
    }
272
273
    /**
274
     * Does working with this column require SQL conversion functions?
275
     *
276
     * This is a metadata function that is required for example in the ORM.
277
     * Usage of {@link convertToDatabaseValueSQL} and
278 1052
     * {@link convertToPHPValueSQL} works for any type and mostly
279
     * does nothing. This method can additionally be used for optimization purposes.
280 1052
     *
281
     * @return bool
282
     */
283
    public function canRequireSQLConversion()
284
    {
285
        return false;
286
    }
287
288
    /**
289
     * Modifies the SQL expression (identifier, parameter) to convert to a database value.
290 1052
     *
291
     * @param string $sqlExpr
292 1052
     *
293
     * @return string
294
     */
295
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

295
    public function convertToDatabaseValueSQL($sqlExpr, /** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
296
    {
297
        return $sqlExpr;
298
    }
299
300
    /**
301
     * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
302
     *
303 1052
     * @param string           $sqlExpr
304
     * @param AbstractPlatform $platform
305 1052
     *
306
     * @return string
307
     */
308
    public function convertToPHPValueSQL($sqlExpr, $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

308
    public function convertToPHPValueSQL($sqlExpr, /** @scrutinizer ignore-unused */ $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
309
    {
310
        return $sqlExpr;
311
    }
312
313 38462
    /**
314
     * Gets an array of database types that map to this Doctrine type.
315 38462
     *
316
     * @return string[]
317
     */
318
    public function getMappedDatabaseTypes(AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

318
    public function getMappedDatabaseTypes(/** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
319
    {
320
        return [];
321
    }
322
323
    /**
324
     * If this Doctrine Type maps to an already mapped database type,
325
     * reverse schema engineering can't tell them apart. You need to mark
326 41203
     * one of those types as commented, which will have Doctrine use an SQL
327
     * comment to typehint the actual Doctrine Type.
328 41203
     *
329
     * @return bool
330
     */
331
    public function requiresSQLCommentHint(AbstractPlatform $platform)
0 ignored issues
show
Unused Code introduced by
The parameter $platform is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

331
    public function requiresSQLCommentHint(/** @scrutinizer ignore-unused */ AbstractPlatform $platform)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
332
    {
333
        return false;
334
    }
335
}
336