Passed
Push — master ( b16987...8b6d77 )
by Maurício
03:49 queued 13s
created

AlterOperation::parse()   F

Complexity

Conditions 56
Paths 36

Size

Total Lines 208
Code Lines 110

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 109
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 56
eloc 110
c 2
b 0
f 0
nc 36
nop 3
dl 0
loc 208
ccs 109
cts 109
cp 1
crap 56
rs 3.3333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @var OptionsArray
23
     */
24
    public $options;
25
26
    /**
27
     * The altered field.
28
     *
29
     * @var Expression|string|null
30
     */
31
    public $field;
32
33
    /**
34
     * The partitions.
35
     *
36
     * @var PartitionDefinition[]|null
37
     */
38
    public $partitions;
39
40
    /**
41
     * @param OptionsArray               $options    options of alter operation
42
     * @param Expression|string|null     $field      altered field
43
     * @param PartitionDefinition[]|null $partitions partitions definition found in the operation
44
     * @param Token[]                    $unknown    unparsed tokens found at the end of operation
45
     */
46 186
    public function __construct(
47
        OptionsArray|null $options = null,
48
        Expression|string|null $field = null,
49
        array|null $partitions = null,
50
        public array $unknown = [],
51
    ) {
52 186
        $this->partitions = $partitions;
53 186
        $this->options = $options;
54 186
        $this->field = $field;
55 186
        $this->unknown = $unknown;
56
    }
57
58 24
    public function build(): string
59
    {
60
        // Specific case of RENAME COLUMN that insert the field between 2 options.
61 24
        $afterFieldsOptions = new OptionsArray();
62 24
        if ($this->options->has('RENAME') && $this->options->has('COLUMN')) {
63 8
            $afterFieldsOptions = clone $this->options;
64 8
            $afterFieldsOptions->remove('RENAME');
65 8
            $afterFieldsOptions->remove('COLUMN');
66 8
            $this->options->remove('TO');
67
        }
68
69 24
        $ret = $this->options . ' ';
70 24
        if (isset($this->field) && ($this->field !== '')) {
71 18
            $ret .= $this->field . ' ';
72
        }
73
74 24
        $ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown);
75
76 24
        if (isset($this->partitions)) {
77 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

77
            $ret .= PartitionDefinitions::buildAll(/** @scrutinizer ignore-type */ $this->partitions);
Loading history...
78
        }
79
80 24
        return trim($ret);
81
    }
82
83
    public function __toString(): string
84
    {
85
        return $this->build();
86
    }
87
}
88