Skip to content

Commit 7f84694

Browse files
committed
Fix TransactionStatement::build() "Call to a member function build() on null"
Signed-off-by: William Desportes <williamdes@wdes.fr>
1 parent 049ed2d commit 7f84694

File tree

6 files changed

+214
-8
lines changed

6 files changed

+214
-8
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,6 @@ parameters:
815815
count: 1
816816
path: src/Statements/TransactionStatement.php
817817

818-
-
819-
message: "#^Cannot call method build\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Statements\\\\TransactionStatement\\|null\\.$#"
820-
count: 1
821-
path: src/Statements/TransactionStatement.php
822-
823818
-
824819
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
825820
count: 4

psalm-baseline.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,7 @@
11121112
<PossiblyNullIterator occurrences="1">
11131113
<code>$this-&gt;statements</code>
11141114
</PossiblyNullIterator>
1115-
<PossiblyNullReference occurrences="2">
1116-
<code>build</code>
1115+
<PossiblyNullReference occurrences="1">
11171116
<code>has</code>
11181117
</PossiblyNullReference>
11191118
</file>

src/Statements/TransactionStatement.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public function build()
9696
$ret .= ';' . $statement->build();
9797
}
9898

99-
$ret .= ';' . $this->end->build();
99+
$ret .= ';';
100+
if ($this->end !== null) {
101+
$ret .= $this->end->build();
102+
}
100103
}
101104

102105
return $ret;

tests/Parser/TransactionStatementTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44

55
namespace PhpMyAdmin\SqlParser\Tests\Parser;
66

7+
use PhpMyAdmin\SqlParser\Parser;
78
use PhpMyAdmin\SqlParser\Tests\TestCase;
89

910
class TransactionStatementTest extends TestCase
1011
{
12+
public function testBuildWithoutEnd(): void
13+
{
14+
$data = $this->getData('parser/parseTransaction4');
15+
$parser = new Parser($data['query']);
16+
$stmt = $parser->statements[0];
17+
$this->assertEquals(
18+
'START TRANSACTION;SET time_zone = "+00:00";',
19+
$stmt->build()
20+
);
21+
}
22+
1123
/**
1224
* @dataProvider transactionProvider
1325
*/
@@ -25,6 +37,7 @@ public function transactionProvider(): array
2537
['parser/parseTransaction'],
2638
['parser/parseTransaction2'],
2739
['parser/parseTransaction3'],
40+
['parser/parseTransaction4'],
2841
['parser/parseTransactionErr1'],
2942
];
3043
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
START TRANSACTION;
3+
SET time_zone = "+00:00";
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
{
2+
"query": "\nSTART TRANSACTION;\nSET time_zone = \"+00:00\";\n",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"str": "\nSTART TRANSACTION;\nSET time_zone = \"+00:00\";\n",
6+
"len": 46,
7+
"last": 46,
8+
"list": {
9+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
10+
"tokens": [
11+
{
12+
"@type": "PhpMyAdmin\\SqlParser\\Token",
13+
"token": "\n",
14+
"value": " ",
15+
"keyword": null,
16+
"type": 3,
17+
"flags": 0,
18+
"position": 0
19+
},
20+
{
21+
"@type": "PhpMyAdmin\\SqlParser\\Token",
22+
"token": "START TRANSACTION",
23+
"value": "START TRANSACTION",
24+
"keyword": "START TRANSACTION",
25+
"type": 1,
26+
"flags": 7,
27+
"position": 1
28+
},
29+
{
30+
"@type": "PhpMyAdmin\\SqlParser\\Token",
31+
"token": ";",
32+
"value": ";",
33+
"keyword": null,
34+
"type": 9,
35+
"flags": 0,
36+
"position": 18
37+
},
38+
{
39+
"@type": "PhpMyAdmin\\SqlParser\\Token",
40+
"token": "\n",
41+
"value": " ",
42+
"keyword": null,
43+
"type": 3,
44+
"flags": 0,
45+
"position": 19
46+
},
47+
{
48+
"@type": "PhpMyAdmin\\SqlParser\\Token",
49+
"token": "SET",
50+
"value": "SET",
51+
"keyword": "SET",
52+
"type": 1,
53+
"flags": 11,
54+
"position": 20
55+
},
56+
{
57+
"@type": "PhpMyAdmin\\SqlParser\\Token",
58+
"token": " ",
59+
"value": " ",
60+
"keyword": null,
61+
"type": 3,
62+
"flags": 0,
63+
"position": 23
64+
},
65+
{
66+
"@type": "PhpMyAdmin\\SqlParser\\Token",
67+
"token": "time_zone",
68+
"value": "time_zone",
69+
"keyword": null,
70+
"type": 0,
71+
"flags": 0,
72+
"position": 24
73+
},
74+
{
75+
"@type": "PhpMyAdmin\\SqlParser\\Token",
76+
"token": " ",
77+
"value": " ",
78+
"keyword": null,
79+
"type": 3,
80+
"flags": 0,
81+
"position": 33
82+
},
83+
{
84+
"@type": "PhpMyAdmin\\SqlParser\\Token",
85+
"token": "=",
86+
"value": "=",
87+
"keyword": null,
88+
"type": 2,
89+
"flags": 2,
90+
"position": 34
91+
},
92+
{
93+
"@type": "PhpMyAdmin\\SqlParser\\Token",
94+
"token": " ",
95+
"value": " ",
96+
"keyword": null,
97+
"type": 3,
98+
"flags": 0,
99+
"position": 35
100+
},
101+
{
102+
"@type": "PhpMyAdmin\\SqlParser\\Token",
103+
"token": "\"+00:00\"",
104+
"value": "+00:00",
105+
"keyword": null,
106+
"type": 7,
107+
"flags": 2,
108+
"position": 36
109+
},
110+
{
111+
"@type": "PhpMyAdmin\\SqlParser\\Token",
112+
"token": ";",
113+
"value": ";",
114+
"keyword": null,
115+
"type": 9,
116+
"flags": 0,
117+
"position": 44
118+
},
119+
{
120+
"@type": "PhpMyAdmin\\SqlParser\\Token",
121+
"token": "\n",
122+
"value": " ",
123+
"keyword": null,
124+
"type": 3,
125+
"flags": 0,
126+
"position": 45
127+
},
128+
{
129+
"@type": "PhpMyAdmin\\SqlParser\\Token",
130+
"token": null,
131+
"value": null,
132+
"keyword": null,
133+
"type": 9,
134+
"flags": 0,
135+
"position": null
136+
}
137+
],
138+
"count": 14,
139+
"idx": 14
140+
},
141+
"delimiter": ";",
142+
"delimiterLen": 1,
143+
"strict": false,
144+
"errors": []
145+
},
146+
"parser": {
147+
"@type": "PhpMyAdmin\\SqlParser\\Parser",
148+
"list": {
149+
"@type": "@1"
150+
},
151+
"statements": [
152+
{
153+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\TransactionStatement",
154+
"type": 1,
155+
"statements": [
156+
{
157+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\SetStatement",
158+
"options": {
159+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
160+
"options": []
161+
},
162+
"end_options": null,
163+
"set": [
164+
{
165+
"@type": "PhpMyAdmin\\SqlParser\\Components\\SetOperation",
166+
"column": "time_zone",
167+
"value": "\"+00:00\""
168+
}
169+
],
170+
"first": 2,
171+
"last": 10
172+
}
173+
],
174+
"end": null,
175+
"options": {
176+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
177+
"options": {
178+
"1": "START TRANSACTION"
179+
}
180+
},
181+
"first": 0,
182+
"last": 1
183+
}
184+
],
185+
"brackets": 0,
186+
"strict": false,
187+
"errors": []
188+
},
189+
"errors": {
190+
"lexer": [],
191+
"parser": []
192+
}
193+
}

0 commit comments

Comments
 (0)