Passed
Push — develop ( 09565d...6daf93 )
by Sergei
03:16
created

Type::createTypeRegistry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Types;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\ParameterType;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use function array_map;
11
use function get_class;
12
13
/**
14
 * The base class for so-called Doctrine mapping types.
15
 *
16
 * A Type object is obtained by calling the static {@link getType()} method.
17
 */
18
abstract class Type
19
{
20
    /** @deprecated Use {@see DefaultTypes::BIGINT} instead. */
21
    public const BIGINT = Types::BIGINT;
22
23
    /** @deprecated Use {@see DefaultTypes::BINARY} instead. */
24
    public const BINARY = Types::BINARY;
25
26
    /** @deprecated Use {@see DefaultTypes::BLOB} instead. */
27
    public const BLOB = Types::BLOB;
28
29
    /** @deprecated Use {@see DefaultTypes::BOOLEAN} instead. */
30
    public const BOOLEAN = Types::BOOLEAN;
31
32
    /** @deprecated Use {@see DefaultTypes::DATE_MUTABLE} instead. */
33
    public const DATE = Types::DATE_MUTABLE;
34
35
    /** @deprecated Use {@see DefaultTypes::DATE_IMMUTABLE} instead. */
36
    public const DATE_IMMUTABLE = Types::DATE_IMMUTABLE;
37
38
    /** @deprecated Use {@see DefaultTypes::DATEINTERVAL} instead. */
39
    public const DATEINTERVAL = Types::DATEINTERVAL;
40
41
    /** @deprecated Use {@see DefaultTypes::DATETIME_MUTABLE} instead. */
42
    public const DATETIME = Types::DATETIME_MUTABLE;
43
44
    /** @deprecated Use {@see DefaultTypes::DATETIME_IMMUTABLE} instead. */
45
    public const DATETIME_IMMUTABLE = Types::DATETIME_IMMUTABLE;
46
47
    /** @deprecated Use {@see DefaultTypes::DATETIMETZ_MUTABLE} instead. */
48
    public const DATETIMETZ = Types::DATETIMETZ_MUTABLE;
49
50
    /** @deprecated Use {@see DefaultTypes::DATETIMETZ_IMMUTABLE} instead. */
51
    public const DATETIMETZ_IMMUTABLE = Types::DATETIMETZ_IMMUTABLE;
52
53
    /** @deprecated Use {@see DefaultTypes::DECIMAL} instead. */
54
    public const DECIMAL = Types::DECIMAL;
55
56
    /** @deprecated Use {@see DefaultTypes::FLOAT} instead. */
57
    public const FLOAT = Types::FLOAT;
58
59
    /** @deprecated Use {@see DefaultTypes::GUID} instead. */
60
    public const GUID = Types::GUID;
61
62
    /** @deprecated Use {@see DefaultTypes::INTEGER} instead. */
63
    public const INTEGER = Types::INTEGER;
64
65
    /** @deprecated Use {@see DefaultTypes::JSON} instead. */
66
    public const JSON = Types::JSON;
67
68
    /** @deprecated Use {@see DefaultTypes::JSON_ARRAY} instead. */
69
    public const JSON_ARRAY = Types::JSON_ARRAY;
70
71
    /** @deprecated Use {@see DefaultTypes::OBJECT} instead. */
72
    public const OBJECT = Types::OBJECT;
73
74
    /** @deprecated Use {@see DefaultTypes::SIMPLE_ARRAY} instead. */
75
    public const SIMPLE_ARRAY = Types::SIMPLE_ARRAY;
76
77
    /** @deprecated Use {@see DefaultTypes::SMALLINT} instead. */
78
    public const SMALLINT = Types::SMALLINT;
79
80
    /** @deprecated Use {@see DefaultTypes::STRING} instead. */
81
    public const STRING = Types::STRING;
82
83
    /** @deprecated Use {@see DefaultTypes::ARRAY} instead. */
84
    public const TARRAY = Types::ARRAY;
85
86
    /** @deprecated Use {@see DefaultTypes::TEXT} instead. */
87
    public const TEXT = Types::TEXT;
88
89
    /** @deprecated Use {@see DefaultTypes::TIME_MUTABLE} instead. */
90
    public const TIME = Types::TIME_MUTABLE;
91
92
    /** @deprecated Use {@see DefaultTypes::TIME_IMMUTABLE} instead. */
93
    public const TIME_IMMUTABLE = Types::TIME_IMMUTABLE;
94
95
    /**
96
     * The map of supported doctrine mapping types.
97
     */
98
    private const BUILTIN_TYPES_MAP = [
99
        Types::ARRAY                => ArrayType::class,
100
        Types::BIGINT               => BigIntType::class,
101
        Types::BINARY               => BinaryType::class,
102
        Types::BLOB                 => BlobType::class,
103
        Types::BOOLEAN              => BooleanType::class,
104
        Types::DATE_MUTABLE         => DateType::class,
105
        Types::DATE_IMMUTABLE       => DateImmutableType::class,
106
        Types::DATEINTERVAL         => DateIntervalType::class,
107
        Types::DATETIME_MUTABLE     => DateTimeType::class,
108
        Types::DATETIME_IMMUTABLE   => DateTimeImmutableType::class,
109
        Types::DATETIMETZ_MUTABLE   => DateTimeTzType::class,
110
        Types::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
111
        Types::DECIMAL              => DecimalType::class,
112
        Types::FLOAT                => FloatType::class,
113
        Types::GUID                 => GuidType::class,
114
        Types::INTEGER              => IntegerType::class,
115
        Types::JSON                 => JsonType::class,
116
        Types::JSON_ARRAY           => JsonArrayType::class,
117
        Types::OBJECT               => ObjectType::class,
118
        Types::SIMPLE_ARRAY         => SimpleArrayType::class,
119
        Types::SMALLINT             => SmallIntType::class,
120
        Types::STRING               => StringType::class,
121
        Types::TEXT                 => TextType::class,
122
        Types::TIME_MUTABLE         => TimeType::class,
123
        Types::TIME_IMMUTABLE       => TimeImmutableType::class,
124
    ];
125
126
    /** @var TypeRegistry|null */
127
    private static $typeRegistry;
128
129
    /**
130
     * @internal Do not instantiate directly - use {@see Type::addType()} method instead.
131
     */
132 29177
    final public function __construct()
133
    {
134 29177
    }
135
136
    /**
137
     * Converts a value from its PHP representation to its database representation
138
     * of this type.
139
     *
140
     * @param mixed            $value    The value to convert.
141
     * @param AbstractPlatform $platform The currently used database platform.
142
     *
143
     * @return mixed The database representation of the value.
144
     */
145 27482
    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

145
    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...
146
    {
147 27482
        return $value;
148
    }
149
150
    /**
151
     * Converts a value from its database representation to its PHP representation
152
     * of this type.
153
     *
154
     * @param mixed            $value    The value to convert.
155
     * @param AbstractPlatform $platform The currently used database platform.
156
     *
157
     * @return mixed The PHP representation of the value.
158
     */
159 27422
    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

159
    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...
160
    {
161 27422
        return $value;
162
    }
163
164
    /**
165
     * Gets the SQL declaration snippet for a field of this type.
166
     *
167
     * @param mixed[]          $fieldDeclaration The field declaration.
168
     * @param AbstractPlatform $platform         The currently used database platform.
169
     *
170
     * @return string
171
     */
172
    abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
173
174
    /**
175
     * Gets the name of this type.
176
     *
177
     * @return string
178
     *
179
     * @todo Needed?
180
     */
181
    abstract public function getName();
182
183
    /**
184
     * @internal This method is only to be used within DBAL for forward compatibility purposes. Do not use directly.
185
     */
186 32388
    final public static function getTypeRegistry() : TypeRegistry
187
    {
188 32388
        if (self::$typeRegistry === null) {
189
            self::$typeRegistry = self::createTypeRegistry();
190
        }
191
192 32388
        return self::$typeRegistry;
193
    }
194
195
    private static function createTypeRegistry() : TypeRegistry
196
    {
197
        $registry = new TypeRegistry();
198
199
        foreach (self::BUILTIN_TYPES_MAP as $name => $class) {
200
            $registry->register($name, new $class());
201
        }
202
203
        return $registry;
204
    }
205
206
    /**
207
     * Factory method to create type instances.
208
     * Type instances are implemented as flyweights.
209
     *
210
     * @param string $name The name of the type (as returned by getName()).
211
     *
212
     * @return \Doctrine\DBAL\Types\Type
213
     *
214
     * @throws DBALException
215
     */
216 32363
    public static function getType($name)
217
    {
218 32363
        return self::getTypeRegistry()->get($name);
219
    }
220
221
    /**
222
     * Adds a custom type to the type map.
223
     *
224
     * @param string $name      The name of the type. This should correspond to what getName() returns.
225
     * @param string $className The class name of the custom type.
226
     *
227
     * @return void
228
     *
229
     * @throws DBALException
230
     */
231 29165
    public static function addType($name, $className)
232
    {
233 29165
        self::getTypeRegistry()->register($name, new $className());
234 29165
    }
235
236
    /**
237
     * Checks if exists support for a type.
238
     *
239
     * @param string $name The name of the type.
240
     *
241
     * @return bool TRUE if type is supported; FALSE otherwise.
242
     */
243 29442
    public static function hasType($name)
244
    {
245 29442
        return self::getTypeRegistry()->has($name);
246
    }
247
248
    /**
249
     * Overrides an already defined type to use a different implementation.
250
     *
251
     * @param string $name
252
     * @param string $className
253
     *
254
     * @return void
255
     *
256
     * @throws DBALException
257
     */
258
    public static function overrideType($name, $className)
259
    {
260
        self::getTypeRegistry()->override($name, new $className());
261
    }
262
263
    /**
264
     * Gets the (preferred) binding type for values of this type that
265
     * can be used when binding parameters to prepared statements.
266
     *
267
     * This method should return one of the {@link \Doctrine\DBAL\ParameterType} constants.
268
     *
269
     * @return int
270
     */
271 30876
    public function getBindingType()
272
    {
273 30876
        return ParameterType::STRING;
274
    }
275
276
    /**
277
     * Gets the types array map which holds all registered types and the corresponding
278
     * type class
279
     *
280
     * @return string[]
281
     */
282 31699
    public static function getTypesMap()
283
    {
284 31699
        return array_map(
285
            static function (Type $type) : string {
286 31699
                return get_class($type);
287 670
            },
288 31699
            self::getTypeRegistry()->getMap()
289
        );
290
    }
291
292
    /**
293
     * Does working with this column require SQL conversion functions?
294
     *
295
     * This is a metadata function that is required for example in the ORM.
296
     * Usage of {@link convertToDatabaseValueSQL} and
297
     * {@link convertToPHPValueSQL} works for any type and mostly
298
     * does nothing. This method can additionally be used for optimization purposes.
299
     *
300
     * @return bool
301
     */
302 1751
    public function canRequireSQLConversion()
303
    {
304 1751
        return false;
305
    }
306
307
    /**
308
     * Modifies the SQL expression (identifier, parameter) to convert to a database value.
309
     *
310
     * @param string $sqlExpr
311
     *
312
     * @return string
313
     */
314 1751
    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

314
    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...
315
    {
316 1751
        return $sqlExpr;
317
    }
318
319
    /**
320
     * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
321
     *
322
     * @param string           $sqlExpr
323
     * @param AbstractPlatform $platform
324
     *
325
     * @return string
326
     */
327 1751
    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

327
    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...
328
    {
329 1751
        return $sqlExpr;
330
    }
331
332
    /**
333
     * Gets an array of database types that map to this Doctrine type.
334
     *
335
     * @return string[]
336
     */
337 30038
    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

337
    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...
338
    {
339 30038
        return [];
340
    }
341
342
    /**
343
     * If this Doctrine Type maps to an already mapped database type,
344
     * reverse schema engineering can't tell them apart. You need to mark
345
     * one of those types as commented, which will have Doctrine use an SQL
346
     * comment to typehint the actual Doctrine Type.
347
     *
348
     * @return bool
349
     */
350 31658
    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

350
    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...
351
    {
352 31658
        return false;
353
    }
354
}
355