Failed Conditions
Pull Request — develop (#3348)
by Sergei
72:11 queued 07:06
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
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 15420
    public function __construct(string $columnName, Type $type, array $options = [])
64
    {
65 15420
        $this->_setName($columnName);
66 15420
        $this->setType($type);
67 15420
        $this->setOptions($options);
68 15420
    }
69
70
    public function getName() : string
71
    {
72
        $name = parent::getName();
73
        assert(is_string($name));
74
75 15420
        return $name;
76
    }
77 15420
78 7939
    /**
79 7939
     * @param mixed[] $options
80
     */
81 46
    public function setOptions(array $options) : Column
82
    {
83 46
        foreach ($options as $name => $value) {
84 46
            $method = 'set' . $name;
85 46
            if (! method_exists($this, $method)) {
86
                // next major: throw an exception
87 46
                @trigger_error(sprintf(
88
                    'The "%s" column option is not supported,' .
89 7916
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
90
                    $name
91
                ), E_USER_DEPRECATED);
92 15420
93
                continue;
94
            }
95
            $this->$method($value);
96
        }
97
98 15420
        return $this;
99
    }
100 15420
101
    public function setType(Type $type) : Column
102 15420
    {
103
        $this->_type = $type;
104
105
        return $this;
106
    }
107
108
    public function setLength(?int $length) : Column
109
    {
110 3045
        $this->_length = $length;
111
112 3045
        return $this;
113 2356
    }
114
115 1034
    public function setPrecision(?int $precision) : Column
116
    {
117
        // defaults to 10 when no valid precision is given.
118 3045
        $this->_precision = $precision ?? 10;
119
120
        return $this;
121
    }
122
123
    public function setScale(?int $scale) : Column
124
    {
125
        $this->_scale = $scale ?? 0;
126 1671
127
        return $this;
128 1671
    }
129 1090
130
    public function setUnsigned(bool $unsigned) : Column
131
    {
132 1671
        $this->_unsigned = $unsigned;
133
134 1671
        return $this;
135
    }
136
137
    public function setFixed(bool $fixed) : Column
138
    {
139
        $this->_fixed = $fixed;
140
141
        return $this;
142 1648
    }
143
144 1648
    public function setNotnull(bool $notnull) : Column
145 1067
    {
146
        $this->_notnull = $notnull;
147
148 1648
        return $this;
149
    }
150 1648
151
    /**
152
     * @param mixed $default
153
     */
154
    public function setDefault($default) : Column
155
    {
156
        $this->_default = $default;
157
158 1222
        return $this;
159
    }
160 1222
161
    /**
162 1222
     * @param mixed[] $platformOptions
163
     */
164
    public function setPlatformOptions(array $platformOptions) : Column
165
    {
166
        $this->_platformOptions = $platformOptions;
167
168
        return $this;
169
    }
170 1820
171
    /**
172 1820
     * @param mixed $value
173
     */
174 1820
    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 9294
    {
183
        $this->_columnDefinition = $value;
184 9294
185
        return $this;
186 9294
    }
187
188
    public function getType() : Type
189
    {
190
        return $this->_type;
191
    }
192
193
    public function getLength() : ?int
194 2037
    {
195
        return $this->_length;
196 2037
    }
197
198 2037
    public function getPrecision() : int
199
    {
200
        return $this->_precision;
201
    }
202
203
    public function getScale() : int
204
    {
205
        return $this->_scale;
206 170
    }
207
208 170
    public function getUnsigned() : bool
209
    {
210 170
        return $this->_unsigned;
211
    }
212
213
    public function getFixed() : bool
214
    {
215
        return $this->_fixed;
216
    }
217
218
    public function getNotnull() : bool
219 517
    {
220
        return $this->_notnull;
221 517
    }
222
223 517
    public function getDefault() : ?string
224
    {
225
        return $this->_default;
226
    }
227
228
    /**
229
     * @return mixed[]
230
     */
231 13
    public function getPlatformOptions() : array
232
    {
233 13
        return $this->_platformOptions;
234
    }
235 13
236
    public function hasPlatformOption(string $name) : bool
237
    {
238
        return isset($this->_platformOptions[$name]);
239
    }
240
241 10544
    /**
242
     * @return mixed
243 10544
     */
244
    public function getPlatformOption(string $name)
245
    {
246
        return $this->_platformOptions[$name];
247
    }
248
249 70
    public function getColumnDefinition() : ?string
250
    {
251 70
        return $this->_columnDefinition;
252
    }
253
254
    public function getAutoincrement() : bool
255
    {
256
        return $this->_autoincrement;
257 46
    }
258
259 46
    public function setAutoincrement(bool $flag) : Column
260
    {
261
        $this->_autoincrement = $flag;
262
263
        return $this;
264
    }
265 46
266
    public function setComment(?string $comment) : Column
267 46
    {
268
        $this->_comment = $comment;
269
270
        return $this;
271
    }
272
273 71
    public function getComment() : ?string
274
    {
275 71
        return $this->_comment;
276
    }
277
278
    /**
279
     * @param mixed $value
280
     */
281 91
    public function setCustomSchemaOption(string $name, $value) : Column
282
    {
283 91
        $this->_customSchemaOptions[$name] = $value;
284
285
        return $this;
286
    }
287
288
    public function hasCustomSchemaOption(string $name) : bool
289 239
    {
290
        return isset($this->_customSchemaOptions[$name]);
291 239
    }
292
293
    /**
294
     * @return mixed
295
     */
296
    public function getCustomSchemaOption(string $name)
297 860
    {
298
        return $this->_customSchemaOptions[$name];
299 860
    }
300
301
    /**
302
     * @param mixed[] $customSchemaOptions
303
     */
304
    public function setCustomSchemaOptions(array $customSchemaOptions) : Column
305 3140
    {
306
        $this->_customSchemaOptions = $customSchemaOptions;
307 3140
308
        return $this;
309
    }
310
311
    /**
312
     * @return mixed[]
313
     */
314
    public function getCustomSchemaOptions() : array
315 7821
    {
316
        return $this->_customSchemaOptions;
317 7821
    }
318
319
    /**
320
     * @return mixed[]
321
     */
322
    public function toArray() : array
323
    {
324
        return array_merge([
325 48
            'name'          => $this->_name,
326
            'type'          => $this->_type,
327 48
            '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