Failed Conditions
Pull Request — develop (#3348)
by Sergei
62:00
created

Column::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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