AlterOperation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
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\Parsers\PartitionDefinitions;
9
use PhpMyAdmin\SqlParser\Token;
10
use PhpMyAdmin\SqlParser\TokensList;
11
12
use function trim;
13
14
/**
15
 * Parses an alter operation.
16
 */
17
final class AlterOperation implements Component
18
{
19
    /**
20
     * Options of this operation.
21
     */
22
    public OptionsArray|null $options = null;
23
24
    /**
25
     * The altered field.
26
     */
27
    public Expression|string|null $field = null;
28
29
    /**
30
     * The partitions.
31
     *
32
     * @var PartitionDefinition[]|null
33
     */
34
    public array|null $partitions = null;
35
36
    /**
37
     * @param OptionsArray|null          $options    options of alter operation
38
     * @param Expression|string|null     $field      altered field
39
     * @param PartitionDefinition[]|null $partitions partitions definition found in the operation
40
     * @param Token[]                    $unknown    unparsed tokens found at the end of operation
41
     */
42 236
    public function __construct(
43
        OptionsArray|null $options = null,
44
        Expression|string|null $field = null,
45
        array|null $partitions = null,
46
        public array $unknown = [],
47
    ) {
48 236
        $this->partitions = $partitions;
49 236
        $this->options = $options;
50 236
        $this->field = $field;
51 236
        $this->unknown = $unknown;
52
    }
53
54 48
    public function build(): string
55
    {
56
        // Specific case of RENAME COLUMN that insert the field between 2 options.
57 48
        $afterFieldsOptions = new OptionsArray();
58 48
        if ($this->options->has('RENAME') && $this->options->has('COLUMN')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

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

58
        if ($this->options->/** @scrutinizer ignore-call */ has('RENAME') && $this->options->has('COLUMN')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59 8
            $afterFieldsOptions = clone $this->options;
60 8
            $afterFieldsOptions->remove('RENAME');
61 8
            $afterFieldsOptions->remove('COLUMN');
62 8
            $this->options->remove('TO');
63
        }
64
65 48
        $ret = $this->options . ' ';
66 48
        if (isset($this->field) && ($this->field !== '')) {
67 18
            $ret .= $this->field . ' ';
68
        }
69
70 48
        $ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown);
71
72 48
        if (isset($this->partitions)) {
73 2
            $ret .= PartitionDefinitions::buildAll($this->partitions);
0 ignored issues
show
Bug introduced by
It seems like $this->partitions can also be of type null; however, parameter $component of PhpMyAdmin\SqlParser\Par...Definitions::buildAll() does only seem to accept array, 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

73
            $ret .= PartitionDefinitions::buildAll(/** @scrutinizer ignore-type */ $this->partitions);
Loading history...
74
        }
75
76 48
        return trim($ret);
77
    }
78
79
    public function __toString(): string
80
    {
81
        return $this->build();
82
    }
83
}
84