Failed Conditions
Pull Request — master (#3354)
by Michael
39:06 queued 17:31
created

Type::getTypeRegistry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
cc 2
nc 2
nop 0
crap 2.0625
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 end;
10
use function explode;
11
use function get_class;
12
use function str_replace;
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 379
    final public function __construct()
85
    {
86 379
    }
87
88
    /**
89
     * Converts a value from its PHP representation to its database representation
90
     * 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 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

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 384
        return $value;
100
    }
101
102
    /**
103
     * 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 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

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

287
    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...
288
    {
289 24
        return $sqlExpr;
290
    }
291
292
    /**
293
     * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
294
     *
295
     * @param string           $sqlExpr
296
     * @param AbstractPlatform $platform
297
     *
298
     * @return string
299
     */
300 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

300
    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...
301
    {
302 24
        return $sqlExpr;
303
    }
304
305
    /**
306
     * Gets an array of database types that map to this Doctrine type.
307
     *
308
     * @return string[]
309
     */
310 2388
    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

310
    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...
311
    {
312 2388
        return [];
313
    }
314
315
    /**
316
     * If this Doctrine Type maps to an already mapped database type,
317
     * reverse schema engineering can't tell them apart. You need to mark
318
     * one of those types as commented, which will have Doctrine use an SQL
319
     * comment to typehint the actual Doctrine Type.
320
     *
321
     * @return bool
322
     */
323 22153
    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

323
    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...
324
    {
325 22153
        return false;
326
    }
327
}
328