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

Column::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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