CreateDefinition::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 5
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
    public string|null $name = null;
23
24
    /**
25
     * Whether this field is a constraint or not.
26
     */
27
    public bool|null $isConstraint = null;
28
29
    /**
30
     * The data type of thew new column.
31
     */
32
    public DataType|null $type = null;
33
34
    /**
35
     * The key.
36
     */
37
    public Key|null $key = null;
38
39
    /**
40
     * The table that is referenced.
41
     */
42
    public Reference|null $references = null;
43
44
    /**
45
     * The options of this field.
46
     */
47
    public OptionsArray|null $options = null;
48
49
    /**
50
     * @param string|null       $name         the name of the field
51
     * @param OptionsArray|null $options      the options of this field
52
     * @param DataType|Key|null $type         the data type of this field or the key
53
     * @param bool              $isConstraint whether this field is a constraint or not
54
     * @param Reference|null    $references   references
55
     */
56 118
    public function __construct(
57
        string|null $name = null,
58
        OptionsArray|null $options = null,
59
        DataType|Key|null $type = null,
60
        bool $isConstraint = false,
61
        Reference|null $references = null,
62
    ) {
63 118
        $this->name = $name;
64 118
        $this->options = $options;
65 118
        if ($type instanceof DataType) {
66 2
            $this->type = $type;
67 118
        } elseif ($type instanceof Key) {
68 2
            $this->key = $type;
69 2
            $this->isConstraint = $isConstraint;
70 2
            $this->references = $references;
71
        }
72
    }
73
74 36
    public function build(): string
75
    {
76 36
        $tmp = '';
77
78 36
        if ($this->isConstraint) {
79 4
            $tmp .= 'CONSTRAINT ';
80
        }
81
82 36
        if (isset($this->name) && ($this->name !== '')) {
83 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

83
            $tmp .= Context::escape(/** @scrutinizer ignore-type */ $this->name) . ' ';
Loading history...
84
        }
85
86 36
        if (! empty($this->type)) {
87 30
            $this->type->lowercase = true;
88 30
            $tmp .= $this->type->build() . ' ';
89
        }
90
91 36
        if (! empty($this->key)) {
92 12
            $tmp .= $this->key . ' ';
93
        }
94
95 36
        if (! empty($this->references)) {
96 4
            $tmp .= 'REFERENCES ' . $this->references . ' ';
97
        }
98
99 36
        $tmp .= $this->options;
100
101 36
        return trim($tmp);
102
    }
103
104 28
    public function __toString(): string
105
    {
106 28
        return $this->build();
107
    }
108
}
109