Failed Conditions
Pull Request — master (#3354)
by Michael
12:22
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
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\ParameterType;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use function end;
9
use function explode;
10
use function str_replace;
11
12
/**
13
 * The base class for so-called Doctrine mapping types.
14
 *
15
 * A Type object is obtained by calling the static {@link getType()} method.
16
 */
17
abstract class Type
18
{
19
    public const TARRAY               = 'array';
20
    public const SIMPLE_ARRAY         = 'simple_array';
21
    public const JSON_ARRAY           = 'json_array';
22
    public const JSON                 = 'json';
23
    public const BIGINT               = 'bigint';
24
    public const BOOLEAN              = 'boolean';
25
    public const DATETIME             = 'datetime';
26
    public const DATETIME_IMMUTABLE   = 'datetime_immutable';
27
    public const DATETIMETZ           = 'datetimetz';
28
    public const DATETIMETZ_IMMUTABLE = 'datetimetz_immutable';
29
    public const DATE                 = 'date';
30
    public const DATE_IMMUTABLE       = 'date_immutable';
31
    public const TIME                 = 'time';
32
    public const TIME_IMMUTABLE       = 'time_immutable';
33
    public const DECIMAL              = 'decimal';
34
    public const INTEGER              = 'integer';
35
    public const OBJECT               = 'object';
36
    public const SMALLINT             = 'smallint';
37
    public const STRING               = 'string';
38
    public const TEXT                 = 'text';
39
    public const BINARY               = 'binary';
40
    public const BLOB                 = 'blob';
41
    public const FLOAT                = 'float';
42
    public const GUID                 = 'guid';
43
    public const DATEINTERVAL         = 'dateinterval';
44
45
    /**
46
     * The map of supported doctrine mapping types.
47
     */
48
    private const BUILTIN_TYPES_MAP = [
49
        self::TARRAY               => ArrayType::class,
50
        self::SIMPLE_ARRAY         => SimpleArrayType::class,
51
        self::JSON_ARRAY           => JsonArrayType::class,
52
        self::JSON                 => JsonType::class,
53
        self::OBJECT               => ObjectType::class,
54
        self::BOOLEAN              => BooleanType::class,
55
        self::INTEGER              => IntegerType::class,
56
        self::SMALLINT             => SmallIntType::class,
57
        self::BIGINT               => BigIntType::class,
58
        self::STRING               => StringType::class,
59
        self::TEXT                 => TextType::class,
60
        self::DATETIME             => DateTimeType::class,
61
        self::DATETIME_IMMUTABLE   => DateTimeImmutableType::class,
62
        self::DATETIMETZ           => DateTimeTzType::class,
63
        self::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
64
        self::DATE                 => DateType::class,
65
        self::DATE_IMMUTABLE       => DateImmutableType::class,
66
        self::TIME                 => TimeType::class,
67
        self::TIME_IMMUTABLE       => TimeImmutableType::class,
68
        self::DECIMAL              => DecimalType::class,
69
        self::FLOAT                => FloatType::class,
70
        self::BINARY               => BinaryType::class,
71
        self::BLOB                 => BlobType::class,
72
        self::GUID                 => GuidType::class,
73
        self::DATEINTERVAL         => DateIntervalType::class,
74
    ];
75
76
    /** @var TypeRegistry|null */
77
    private static $typeRegistry;
78
79
    /**
80
     * @internal Do not instantiate directly - use the factory method instead.
81
     */
82 211
    final public function __construct()
83
    {
84 211
    }
85
86
    /**
87
     * Converts a value from its PHP representation to its database representation
88
     * of this type.
89
     *
90
     * @param mixed            $value    The value to convert.
91
     * @param AbstractPlatform $platform The currently used database platform.
92
     *
93
     * @return mixed The database representation of the value.
94
     */
95 384
    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

95
    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...
96
    {
97 384
        return $value;
98
    }
99
100
    /**
101
     * Converts a value from its database representation to its PHP representation
102
     * of this type.
103
     *
104
     * @param mixed            $value    The value to convert.
105
     * @param AbstractPlatform $platform The currently used database platform.
106
     *
107
     * @return mixed The PHP representation of the value.
108
     */
109 120
    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

109
    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...
110
    {
111 120
        return $value;
112
    }
113
114
    /**
115
     * Gets the default length of this type.
116
     *
117
     * @deprecated Rely on information provided by the platform instead.
118
     *
119
     * @return int|null
120
     */
121
    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

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

280
    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...
281
    {
282 24
        return $sqlExpr;
283
    }
284
285
    /**
286
     * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
287
     *
288
     * @param string           $sqlExpr
289
     * @param AbstractPlatform $platform
290
     *
291
     * @return string
292
     */
293 24
    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

293
    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...
294
    {
295 24
        return $sqlExpr;
296
    }
297
298
    /**
299
     * Gets an array of database types that map to this Doctrine type.
300
     *
301
     * @return string[]
302
     */
303 2376
    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

303
    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...
304
    {
305 2376
        return [];
306
    }
307
308
    /**
309
     * If this Doctrine Type maps to an already mapped database type,
310
     * reverse schema engineering can't tell them apart. You need to mark
311
     * one of those types as commented, which will have Doctrine use an SQL
312
     * comment to typehint the actual Doctrine Type.
313
     *
314
     * @return bool
315
     */
316 22141
    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

316
    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...
317
    {
318 22141
        return false;
319
    }
320
}
321