Completed
Push — master ( 578d3a...04a929 )
by Maurício
34s queued 14s
created

CreateDefinition   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 102
rs 10
ccs 26
cts 26
cp 1
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 28 7
A __construct() 0 15 3
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Context;
9
10
use function trim;
11
12
/**
13
 * Parses the create definition of a column or a key.
14
 *
15
 * Used for parsing `CREATE TABLE` statement.
16
 */
17
final class CreateDefinition implements Component
18
{
19
    /**
20
     * The name of the new column.
21
     *
22
     * @var string|null
23
     */
24
    public $name;
25
26
    /**
27
     * Whether this field is a constraint or not.
28
     *
29
     * @var bool|null
30
     */
31
    public $isConstraint;
32
33
    /**
34
     * The data type of thew new column.
35
     *
36
     * @var DataType|null
37
     */
38
    public $type;
39
40
    /**
41
     * The key.
42
     *
43
     * @var Key|null
44
     */
45
    public $key;
46
47
    /**
48
     * The table that is referenced.
49
     *
50
     * @var Reference|null
51
     */
52
    public $references;
53
54
    /**
55
     * The options of this field.
56
     *
57
     * @var OptionsArray|null
58
     */
59
    public $options;
60
61
    /**
62
     * @param string|null       $name         the name of the field
63
     * @param OptionsArray|null $options      the options of this field
64
     * @param DataType|Key|null $type         the data type of this field or the key
65
     * @param bool              $isConstraint whether this field is a constraint or not
66
     * @param Reference|null    $references   references
67
     */
68 118
    public function __construct(
69
        string|null $name = null,
70
        OptionsArray|null $options = null,
71
        DataType|Key|null $type = null,
72
        bool $isConstraint = false,
73
        Reference|null $references = null,
74
    ) {
75 118
        $this->name = $name;
76 118
        $this->options = $options;
77 118
        if ($type instanceof DataType) {
78 2
            $this->type = $type;
79 118
        } elseif ($type instanceof Key) {
80 2
            $this->key = $type;
81 2
            $this->isConstraint = $isConstraint;
82 2
            $this->references = $references;
83
        }
84
    }
85
86 36
    public function build(): string
87
    {
88 36
        $tmp = '';
89
90 36
        if ($this->isConstraint) {
91 4
            $tmp .= 'CONSTRAINT ';
92
        }
93
94 36
        if (isset($this->name) && ($this->name !== '')) {
95 34
            $tmp .= Context::escape($this->name) . ' ';
0 ignored issues
show
Bug introduced by
It seems like $this->name can also be of type null; however, parameter $str of PhpMyAdmin\SqlParser\Context::escape() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            $tmp .= Context::escape(/** @scrutinizer ignore-type */ $this->name) . ' ';
Loading history...
96
        }
97
98 36
        if (! empty($this->type)) {
99 30
            $this->type->lowercase = true;
100 30
            $tmp .= $this->type->build() . ' ';
101
        }
102
103 36
        if (! empty($this->key)) {
104 12
            $tmp .= $this->key . ' ';
105
        }
106
107 36
        if (! empty($this->references)) {
108 4
            $tmp .= 'REFERENCES ' . $this->references . ' ';
109
        }
110
111 36
        $tmp .= $this->options;
112
113 36
        return trim($tmp);
114
    }
115
116 28
    public function __toString(): string
117
    {
118 28
        return $this->build();
119
    }
120
}
121