Passed
Branch master (66090f)
by Wilmer
38:34 queued 29:57
created

ColumnSchemaBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 42
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildUnsignedString() 0 3 2
A __construct() 0 3 1
A buildCommentString() 0 4 2
A asString() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Exception;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Schema\AbstractColumnSchemaBuilder;
10
use Yiisoft\Db\Schema\QuoterInterface;
11
12
/**
13
 * The class ColumnSchemaBuilder for Mysql database.
14
 */
15
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
16
{
17
    /**
18
     *  @psalm-param string[]|int[]|int|string|null $length column size or precision definition.
19
     */
20 13
    public function __construct(string $type, array|int|string|null $length, private QuoterInterface $quoter)
21
    {
22 13
        parent::__construct($type, $length);
23
    }
24
25
    /**
26
     * Builds the unsigned string for column. Defaults to unsupported.
27
     *
28
     * @return string a string containing UNSIGNED keyword.
29
     */
30 9
    protected function buildUnsignedString(): string
31
    {
32 9
        return $this->isUnsigned() ? ' UNSIGNED' : '';
33
    }
34
35
    /**
36
     * Builds the comment specification for the column.
37
     *
38
     * @throws Exception|InvalidConfigException
39
     *
40
     * @return string a string containing the COMMENT keyword and the comment itself.
41
     */
42 9
    protected function buildCommentString(): string
43
    {
44 9
        return $this->getComment() !== null ? ' COMMENT '
45 9
            . (string) $this->quoter->quoteValue($this->getComment()) : '';
46
    }
47
48 9
    public function asString(): string
49
    {
50 9
        $format = match ($this->getTypeCategory()) {
51 9
            self::CATEGORY_PK => '{type}{length}{comment}{check}{append}',
52 9
            self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{check}',
53 9
            default => '{type}{length}{notnull}{default}{unique}{comment}{append}{check}',
54 9
        };
55
56 9
        return $this->buildCompleteString($format);
57
    }
58
}
59