TransactionStatement::parse()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Statements;
6
7
use PhpMyAdmin\SqlParser\Parser;
8
use PhpMyAdmin\SqlParser\Statement;
9
use PhpMyAdmin\SqlParser\TokensList;
10
11
/**
12
 * Transaction statement.
13
 */
14
class TransactionStatement extends Statement
15
{
16
    /**
17
     * START TRANSACTION and BEGIN.
18
     */
19
    public const TYPE_BEGIN = 1;
20
21
    /**
22
     * COMMIT and ROLLBACK.
23
     */
24
    public const TYPE_END = 2;
25
26
    /**
27
     * The type of this query.
28
     */
29
    public int|null $type = null;
30
31
    /**
32
     * The list of statements in this transaction.
33
     *
34
     * @var Statement[]|null
35
     */
36
    public array|null $statements = null;
37
38
    /**
39
     * The ending transaction statement which may be a `COMMIT` or a `ROLLBACK`.
40
     */
41
    public TransactionStatement|null $end = null;
42
43
    /**
44
     * Options for this query.
45
     *
46
     * @var array<string, int|array<int, int|string>>
47
     * @psalm-var array<string, (positive-int|array{positive-int, ('var'|'var='|'expr'|'expr=')})>
48
     */
49
    public static array $statementOptions = [
50
        'START TRANSACTION' => 1,
51
        'BEGIN' => 1,
52
        'COMMIT' => 1,
53
        'ROLLBACK' => 1,
54
        'WITH CONSISTENT SNAPSHOT' => 2,
55
        'WORK' => 2,
56
        'AND NO CHAIN' => 3,
57
        'AND CHAIN' => 3,
58
        'RELEASE' => 4,
59
        'NO RELEASE' => 4,
60
    ];
61
62
    /**
63
     * @param Parser     $parser the instance that requests parsing
64
     * @param TokensList $list   the list of tokens to be parsed
65
     */
66 26
    public function parse(Parser $parser, TokensList $list): void
67
    {
68 26
        parent::parse($parser, $list);
69
70
        // Checks the type of this query.
71 26
        if ($this->options->has('START TRANSACTION') || $this->options->has('BEGIN')) {
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

71
        if ($this->options->/** @scrutinizer ignore-call */ has('START TRANSACTION') || $this->options->has('BEGIN')) {

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...
72 22
            $this->type = self::TYPE_BEGIN;
73 22
        } elseif ($this->options->has('COMMIT') || $this->options->has('ROLLBACK')) {
74 20
            $this->type = self::TYPE_END;
75
        }
76
    }
77
78 6
    public function build(): string
79
    {
80 6
        $ret = $this->options->build();
81 6
        if ($this->type === self::TYPE_BEGIN) {
82 6
            foreach ($this->statements as $statement) {
83
                /*
84
                 * @var SelectStatement $statement
85
                 */
86 6
                $ret .= ';' . $statement->build();
87
            }
88
89 6
            $ret .= ';';
90 6
            if ($this->end !== null) {
91 4
                $ret .= $this->end->build();
92
            }
93
        }
94
95 6
        return $ret;
96
    }
97
}
98