Completed
Pull Request — develop (#3255)
by Michael
83:40 queued 22:31
created

Type   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 288
Duplicated Lines 0 %

Test Coverage

Coverage 65.85%

Importance

Changes 0
Metric Value
wmc 19
eloc 75
dl 0
loc 288
ccs 27
cts 41
cp 0.6585
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHPValue() 0 3 1
A __construct() 0 2 1
A convertToDatabaseValue() 0 3 1
A getBindingType() 0 3 1
A getTypesMap() 0 3 1
A hasType() 0 3 1
A overrideType() 0 11 3
A addType() 0 7 2
A getType() 0 10 3
A canRequireSQLConversion() 0 3 1
A requiresSQLCommentHint() 0 3 1
A convertToPHPValueSQL() 0 3 1
A convertToDatabaseValueSQL() 0 3 1
A getMappedDatabaseTypes() 0 3 1
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\DBALException;
8
use function end;
9
use function explode;
10
use function get_class;
11
use function str_replace;
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
 * @author Roman Borschel <[email protected]>
19
 * @author Benjamin Eberlei <[email protected]>
20
 * @since  2.0
21
 */
22
abstract class Type
23
{
24
    const TARRAY = 'array';
25
    const SIMPLE_ARRAY = 'simple_array';
26
    const JSON_ARRAY = 'json_array';
27
    const JSON = 'json';
28
    const BIGINT = 'bigint';
29
    const BOOLEAN = 'boolean';
30
    const DATETIME = 'datetime';
31
    const DATETIME_IMMUTABLE = 'datetime_immutable';
32
    const DATETIMETZ = 'datetimetz';
33
    const DATETIMETZ_IMMUTABLE = 'datetimetz_immutable';
34
    const DATE = 'date';
35
    const DATE_IMMUTABLE = 'date_immutable';
36
    const TIME = 'time';
37
    const TIME_IMMUTABLE = 'time_immutable';
38
    const DECIMAL = 'decimal';
39
    const INTEGER = 'integer';
40
    const OBJECT = 'object';
41
    const SMALLINT = 'smallint';
42
    const STRING = 'string';
43
    const TEXT = 'text';
44
    const BINARY = 'binary';
45
    const BLOB = 'blob';
46
    const FLOAT = 'float';
47
    const GUID = 'guid';
48
    const DATEINTERVAL = 'dateinterval';
49
50
    /**
51
     * Map of already instantiated type objects. One instance per type (flyweight).
52
     *
53
     * @var array
54
     */
55
    private static $_typeObjects = [];
56
57
    /**
58
     * The map of supported doctrine mapping types.
59
     *
60
     * @var array
61
     */
62
    private static $_typesMap = [
63
        self::TARRAY => ArrayType::class,
64
        self::SIMPLE_ARRAY => SimpleArrayType::class,
65
        self::JSON_ARRAY => JsonArrayType::class,
66
        self::JSON => JsonType::class,
67
        self::OBJECT => ObjectType::class,
68
        self::BOOLEAN => BooleanType::class,
69
        self::INTEGER => IntegerType::class,
70
        self::SMALLINT => SmallIntType::class,
71
        self::BIGINT => BigIntType::class,
72
        self::STRING => StringType::class,
73
        self::TEXT => TextType::class,
74
        self::DATETIME => DateTimeType::class,
75
        self::DATETIME_IMMUTABLE => DateTimeImmutableType::class,
76
        self::DATETIMETZ => DateTimeTzType::class,
77
        self::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
78
        self::DATE => DateType::class,
79
        self::DATE_IMMUTABLE => DateImmutableType::class,
80
        self::TIME => TimeType::class,
81
        self::TIME_IMMUTABLE => TimeImmutableType::class,
82
        self::DECIMAL => DecimalType::class,
83
        self::FLOAT => FloatType::class,
84
        self::BINARY => BinaryType::class,
85
        self::BLOB => BlobType::class,
86
        self::GUID => GuidType::class,
87
        self::DATEINTERVAL => DateIntervalType::class,
88
    ];
89
90
    /**
91
     * Prevents instantiation and forces use of the factory method.
92
     */
93 60
    final private function __construct()
94
    {
95 60
    }
96
97
    /**
98
     * Converts a value from its PHP representation to its database representation
99
     * of this type.
100
     *
101
     * @param mixed                                     $value    The value to convert.
102
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform.
103
     *
104
     * @return mixed The database representation of the value.
105
     */
106 256
    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

106
    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...
107
    {
108 256
        return $value;
109
    }
110
111
    /**
112
     * Converts a value from its database representation to its PHP representation
113
     * of this type.
114
     *
115
     * @param mixed                                     $value    The value to convert.
116
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform.
117
     *
118
     * @return mixed The PHP representation of the value.
119
     */
120 80
    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

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

267
    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...
268
    {
269
        return $sqlExpr;
270
    }
271
272
    /**
273
     * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
274
     *
275
     * @param string                                    $sqlExpr
276
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
277
     *
278 16
     * @return string
279
     */
280 16
    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

280
    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...
281
    {
282
        return $sqlExpr;
283
    }
284
285
    /**
286
     * Gets an array of database types that map to this Doctrine type.
287
     *
288
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
289
     *
290
     * @return array
291 16
     */
292
    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

292
    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...
293 16
    {
294
        return [];
295
    }
296
297
    /**
298
     * If this Doctrine Type maps to an already mapped database type,
299
     * reverse schema engineering can't tell them apart. You need to mark
300
     * one of those types as commented, which will have Doctrine use an SQL
301
     * comment to typehint the actual Doctrine Type.
302
     *
303
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
304 16
     *
305
     * @return bool
306 16
     */
307
    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

307
    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...
308
    {
309
        return false;
310
    }
311
}
312