Failed Conditions
Pull Request — develop (#3348)
by Sergei
125:02 queued 59:58
created

Column   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Test Coverage

Coverage 97.47%

Importance

Changes 0
Metric Value
wmc 38
eloc 91
dl 0
loc 321
ccs 77
cts 79
cp 0.9747
rs 9.36
c 0
b 0
f 0

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setOptions() 0 18 3
A getPlatformOption() 0 3 1
A getScale() 0 3 1
A setScale() 0 5 1
A setFixed() 0 5 1
A setPlatformOptions() 0 5 1
A getFixed() 0 3 1
A getPlatformOptions() 0 3 1
A setType() 0 5 1
A setComment() 0 5 1
A getCustomSchemaOptions() 0 3 1
A getLength() 0 3 1
A getAutoincrement() 0 3 1
A hasCustomSchemaOption() 0 3 1
A getUnsigned() 0 3 1
A getColumnDefinition() 0 3 1
A toArray() 0 16 1
A getNotnull() 0 3 1
A getDefault() 0 3 1
A setUnsigned() 0 5 1
A getCustomSchemaOption() 0 3 1
A setDefault() 0 5 1
A getComment() 0 3 1
A setNotnull() 0 5 1
A getType() 0 3 1
A getPrecision() 0 3 1
A setPlatformOption() 0 5 1
A hasPlatformOption() 0 3 1
A setPrecision() 0 6 1
A setAutoincrement() 0 5 1
A setCustomSchemaOptions() 0 5 1
A getName() 0 6 1
A setLength() 0 5 1
A setColumnDefinition() 0 5 1
A setCustomSchemaOption() 0 5 1
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Types\Type;
6
use const E_USER_DEPRECATED;
7
use function array_merge;
8
use function assert;
9
use function is_string;
10
use function method_exists;
11
use function sprintf;
12
use function trigger_error;
13
14
/**
15
 * Object representation of a database column.
16
 */
17
class Column extends AbstractAsset
18
{
19
    /** @var Type */
20
    protected $_type;
21
22
    /** @var int|null */
23
    protected $_length = null;
24
25
    /** @var int */
26
    protected $_precision = 10;
27
28
    /** @var int */
29
    protected $_scale = 0;
30
31
    /** @var bool */
32
    protected $_unsigned = false;
33
34
    /** @var bool */
35
    protected $_fixed = false;
36
37
    /** @var bool */
38
    protected $_notnull = true;
39
40
    /** @var string|null */
41
    protected $_default = null;
42
43
    /** @var bool */
44
    protected $_autoincrement = false;
45
46
    /** @var mixed[] */
47
    protected $_platformOptions = [];
48
49
    /** @var string|null */
50
    protected $_columnDefinition = null;
51
52
    /** @var string|null */
53
    protected $_comment = null;
54
55
    /** @var mixed[] */
56
    protected $_customSchemaOptions = [];
57
58
    /**
59
     * Creates a new Column.
60
     *
61
     * @param mixed[] $options
62
     */
63 17350
    public function __construct(string $columnName, Type $type, array $options = [])
64
    {
65 17350
        $this->_setName($columnName);
66 17350
        $this->setType($type);
67 17350
        $this->setOptions($options);
68 17350
    }
69
70
    public function getName() : string
71
    {
72
        $name = parent::getName();
73
        assert(is_string($name));
74
75 17350
        return $name;
76
    }
77 17350
78 16889
    /**
79 16889
     * @param mixed[] $options
80
     */
81 2930
    public function setOptions(array $options) : Column
82
    {
83 2
        foreach ($options as $name => $value) {
84 2930
            $method = 'set' . $name;
85 2930
            if (! method_exists($this, $method)) {
86
                // next major: throw an exception
87 2930
                @trigger_error(sprintf(
88
                    'The "%s" column option is not supported,' .
89 16888
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
90
                    $name
91
                ), E_USER_DEPRECATED);
92 17350
93
                continue;
94
            }
95
            $this->$method($value);
96
        }
97
98 17350
        return $this;
99
    }
100 17350
101
    public function setType(Type $type) : Column
102 17350
    {
103
        $this->_type = $type;
104
105
        return $this;
106
    }
107
108
    public function setLength(?int $length) : Column
109
    {
110 16128
        $this->_length = $length;
111
112 16128
        return $this;
113 16103
    }
114
115 15887
    public function setPrecision(?int $precision) : Column
116
    {
117
        // defaults to 10 when no valid precision is given.
118 16128
        $this->_precision = $precision ?? 10;
119
120
        return $this;
121
    }
122
123
    public function setScale(?int $scale) : Column
124
    {
125
        $this->_scale = $scale ?? 0;
126 15909
127
        return $this;
128 15909
    }
129 14773
130
    public function setUnsigned(bool $unsigned) : Column
131
    {
132 15909
        $this->_unsigned = $unsigned;
133
134 15909
        return $this;
135
    }
136
137
    public function setFixed(bool $fixed) : Column
138
    {
139
        $this->_fixed = $fixed;
140
141
        return $this;
142 15908
    }
143
144 15908
    public function setNotnull(bool $notnull) : Column
145 14706
    {
146
        $this->_notnull = $notnull;
147
148 15908
        return $this;
149
    }
150 15908
151
    /**
152
     * @param mixed $default
153
     */
154
    public function setDefault($default) : Column
155
    {
156
        $this->_default = $default;
157
158 15890
        return $this;
159
    }
160 15890
161
    /**
162 15890
     * @param mixed[] $platformOptions
163
     */
164
    public function setPlatformOptions(array $platformOptions) : Column
165
    {
166
        $this->_platformOptions = $platformOptions;
167
168
        return $this;
169
    }
170 16075
171
    /**
172 16075
     * @param mixed $value
173
     */
174 16075
    public function setPlatformOption(string $name, $value) : Column
175
    {
176
        $this->_platformOptions[$name] = $value;
177
178
        return $this;
179
    }
180
181
    public function setColumnDefinition(string $value) : Column
182 17084
    {
183
        $this->_columnDefinition = $value;
184 17084
185
        return $this;
186 17084
    }
187
188
    public function getType() : Type
189
    {
190
        return $this->_type;
191
    }
192
193
    public function getLength() : ?int
194 15925
    {
195
        return $this->_length;
196 15925
    }
197
198 15925
    public function getPrecision() : int
199
    {
200
        return $this->_precision;
201
    }
202
203
    public function getScale() : int
204
    {
205
        return $this->_scale;
206 12822
    }
207
208 12822
    public function getUnsigned() : bool
209
    {
210 12822
        return $this->_unsigned;
211
    }
212
213
    public function getFixed() : bool
214
    {
215
        return $this->_fixed;
216
    }
217
218
    public function getNotnull() : bool
219 15649
    {
220
        return $this->_notnull;
221 15649
    }
222
223 15649
    public function getDefault() : ?string
224
    {
225
        return $this->_default;
226
    }
227
228
    /**
229
     * @return mixed[]
230
     */
231 9055
    public function getPlatformOptions() : array
232
    {
233 9055
        return $this->_platformOptions;
234
    }
235 9055
236
    public function hasPlatformOption(string $name) : bool
237
    {
238
        return isset($this->_platformOptions[$name]);
239
    }
240
241 17142
    /**
242
     * @return mixed
243 17142
     */
244
    public function getPlatformOption(string $name)
245
    {
246
        return $this->_platformOptions[$name];
247
    }
248
249 15093
    public function getColumnDefinition() : ?string
250
    {
251 15093
        return $this->_columnDefinition;
252
    }
253
254
    public function getAutoincrement() : bool
255
    {
256
        return $this->_autoincrement;
257 15083
    }
258
259 15083
    public function setAutoincrement(bool $flag) : Column
260
    {
261
        $this->_autoincrement = $flag;
262
263
        return $this;
264
    }
265 15083
266
    public function setComment(?string $comment) : Column
267 15083
    {
268
        $this->_comment = $comment;
269
270
        return $this;
271
    }
272
273 15213
    public function getComment() : ?string
274
    {
275 15213
        return $this->_comment;
276
    }
277
278
    /**
279
     * @param mixed $value
280
     */
281 15177
    public function setCustomSchemaOption(string $name, $value) : Column
282
    {
283 15177
        $this->_customSchemaOptions[$name] = $value;
284
285
        return $this;
286
    }
287
288
    public function hasCustomSchemaOption(string $name) : bool
289 15252
    {
290
        return isset($this->_customSchemaOptions[$name]);
291 15252
    }
292
293
    /**
294
     * @return mixed
295
     */
296
    public function getCustomSchemaOption(string $name)
297 15405
    {
298
        return $this->_customSchemaOptions[$name];
299 15405
    }
300
301
    /**
302
     * @param mixed[] $customSchemaOptions
303
     */
304
    public function setCustomSchemaOptions(array $customSchemaOptions) : Column
305 15980
    {
306
        $this->_customSchemaOptions = $customSchemaOptions;
307 15980
308
        return $this;
309
    }
310
311
    /**
312
     * @return mixed[]
313
     */
314
    public function getCustomSchemaOptions() : array
315 17025
    {
316
        return $this->_customSchemaOptions;
317 17025
    }
318
319
    /**
320
     * @return mixed[]
321
     */
322
    public function toArray() : array
323
    {
324
        return array_merge([
325 14364
            'name'          => $this->_name,
326
            'type'          => $this->_type,
327 14364
            'default'       => $this->_default,
328
            'notnull'       => $this->_notnull,
329
            'length'        => $this->_length,
330
            'precision'     => $this->_precision,
331
            'scale'         => $this->_scale,
332
            'fixed'         => $this->_fixed,
333
            'unsigned'      => $this->_unsigned,
334
            'autoincrement' => $this->_autoincrement,
335
            'columnDefinition' => $this->_columnDefinition,
336
            'comment' => $this->_comment,
337
        ], $this->_platformOptions, $this->_customSchemaOptions);
338
    }
339
}
340