Test Failed
Pull Request — master (#283)
by Wilmer
04:17
created

Column::buildCommentString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Exception;
8
use Yiisoft\Db\Schema\Builder\AbstractColumn;
9
10
/**
11
 * Provides a convenient way to create column schema for use with {@see `\Yiisoft\Db\Mysql\Schema`}
12
 * for MySQL, MariaDB.
13
 *
14
 * It has methods for specifying the properties of a column, such as its type, size, default value, and whether it
15
 * is nullable or not. It also provides a method for creating a column schema based on the specified properties.
16
 *
17
 * For example, the following code creates a column schema for an integer column:
18
 *
19
 * ```php
20
 * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
21
 * ```
22
 *
23
 * Provides a fluent interface, which means that the methods can be chained together to create a column schema with
24
 * many properties in a single line of code.
25
 */
26
final class Column extends AbstractColumn
27
{
28
    /**
29
     * Builds the unsigned string for column. Defaults to unsupported.
30
     *
31
     * @return string A string containing `UNSIGNED` keyword.
32
     */
33 17
    protected function buildUnsignedString(): string
34
    {
35 17
        return $this->isUnsigned() ? ' UNSIGNED' : '';
36
    }
37
38
    /**
39
     * Builds the comment specification for the column.
40
     *
41
     * @throws Exception
42
     *
43
     * @return string A string containing the `COMMENT` keyword and the comment itself.
44
     */
45 17
    protected function buildCommentString(): string
46
    {
47 17
        if ($this->getComment() === null) {
48 15
            return '';
49
        }
50
51 2
        return ' COMMENT ' . (string) (new Quoter('`', '`'))->quoteValue($this->getComment());
52
    }
53
54 17
    public function buildString(): string
55
    {
56 17
        $format = match ($this->getTypeCategory()) {
57 17
            self::TYPE_CATEGORY_PK => '{type}{length}{comment}{check}{append}',
58 17
            self::TYPE_CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}',
59 17
            self::TYPE_CATEGORY_UUID => '{type}{notnull}{unique}{default}{check}{comment}{append}',
60 17
            self::TYPE_CATEGORY_UUID_PK => '{type}{notnull}{default}{check}{comment}{append}',
61 17
            default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}',
62 17
        };
63
64 17
        return $this->buildCompleteString($format);
65
    }
66
}
67