Skip to content

Commit 1a073cd

Browse files
committed
Merge branch 'QA'
This merge includes phpcs fixes Signed-off-by: William Desportes <[email protected]>
2 parents 6de19ae + 49731d2 commit 1a073cd

File tree

10 files changed

+79
-20
lines changed

10 files changed

+79
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [Unreleased] -
4+
* Fix for PHP deprecations messages about implode for php 7.4+ (#258)
5+
* Parse CHECK keyword on table definition (#264)
6+
* Parse truncate statement (#221)
7+
38
## [5.0.0] - 2019-05-09
49

510
* Drop support for PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7.0 and HHVM

src/Components/AlterOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private static function checkIfColumnDefinitionKeyword($tokenValue)
339339
'COMMENT',
340340
'DEFAULT',
341341
'CHARACTER SET',
342-
'COLLATE'
342+
'COLLATE',
343343
];
344344
// Since these options can be used for
345345
// both table as well as a specific column in the table

src/Components/CreateDefinition.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class CreateDefinition extends Component
8080
'VIRTUAL' => 10,
8181
'PERSISTENT' => 11,
8282
'STORED' => 11,
83+
'CHECK' => [
84+
12,
85+
'expr',
86+
['parenthesesDelimited' => true],
87+
]
8388
// Common entries.
8489
//
8590
// NOTE: Some of the common options are not in the same order which

src/Components/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
433433
public static function build($component, array $options = [])
434434
{
435435
if (is_array($component)) {
436-
return implode($component, ', ');
436+
return implode(', ', $component);
437437
}
438438

439439
if ($component->expr !== '' && ! is_null($component->expr)) {

src/Components/ExpressionArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ public static function build($component, array $options = [])
122122
$ret[] = $frag::build($frag);
123123
}
124124

125-
return implode($ret, ', ');
125+
return implode(', ', $ret);
126126
}
127127
}

src/Statement.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,15 @@ public function parse(Parser $parser, TokensList $list)
320320
}
321321

322322
// Checking if this is the beginning of a clause.
323-
if (! empty(Parser::$KEYWORD_PARSERS[$token->value]) && $list->idx < $list->count) {
324-
$class = Parser::$KEYWORD_PARSERS[$token->value]['class'];
325-
$field = Parser::$KEYWORD_PARSERS[$token->value]['field'];
326-
if (! empty(Parser::$KEYWORD_PARSERS[$token->value]['options'])) {
327-
$options = Parser::$KEYWORD_PARSERS[$token->value]['options'];
323+
// Fix Issue #221: As `truncate` is not a keyword
324+
// but it might be the beginning of a statement of truncate,
325+
// so let the value use the keyword field for truncate type.
326+
$token_value = in_array($token->keyword, ['TRUNCATE']) ? $token->keyword : $token->value;
327+
if (! empty(Parser::$KEYWORD_PARSERS[$token_value]) && $list->idx < $list->count) {
328+
$class = Parser::$KEYWORD_PARSERS[$token_value]['class'];
329+
$field = Parser::$KEYWORD_PARSERS[$token_value]['field'];
330+
if (! empty(Parser::$KEYWORD_PARSERS[$token_value]['options'])) {
331+
$options = Parser::$KEYWORD_PARSERS[$token_value]['options'];
328332
}
329333
}
330334

src/Statements/TruncateStatement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ class TruncateStatement extends Statement
3333
* @var Expression
3434
*/
3535
public $table;
36+
37+
/**
38+
* Special build method for truncate statement as Statement::build would return empty string.
39+
*
40+
* @return string
41+
*/
42+
public function build()
43+
{
44+
return 'TRUNCATE TABLE ' . $this->table . ';';
45+
}
3646
}

src/Utils/CLI.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function runHighlight()
7878

7979
return 0;
8080
}
81-
if (!isset($params['q'])) {
81+
if (! isset($params['q'])) {
8282
if ($stdIn = $this->readStdin()) {
8383
$params['q'] = $stdIn;
8484
}
@@ -134,7 +134,7 @@ public function runLint()
134134
if (isset($params['c'])) {
135135
Context::load($params['c']);
136136
}
137-
if (!isset($params['q'])) {
137+
if (! isset($params['q'])) {
138138
if ($stdIn = $this->readStdin()) {
139139
$params['q'] = $stdIn;
140140
}
@@ -190,7 +190,7 @@ public function runTokenize()
190190

191191
return 0;
192192
}
193-
if (!isset($params['q'])) {
193+
if (! isset($params['q'])) {
194194
if ($stdIn = $this->readStdin()) {
195195
$params['q'] = $stdIn;
196196
}
@@ -218,7 +218,8 @@ public function runTokenize()
218218
return 1;
219219
}
220220

221-
private function readStdin() {
221+
private function readStdin()
222+
{
222223
stream_set_blocking(STDIN, false);
223224
$stdin = stream_get_contents(STDIN);
224225
// restore-default block-mode setting

tests/Components/CreateDefinitionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,20 @@ public function testBuild()
6464
CreateDefinition::build($parser->statements[0]->fields[1])
6565
);
6666
}
67+
68+
public function testBuild2()
69+
{
70+
$parser = new Parser(
71+
'CREATE TABLE `payment` (' .
72+
'-- snippet' . "\n" .
73+
'`customer_id` smallint(5) unsigned NOT NULL,' .
74+
'`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL CHECK (json_valid(customer_data)),' .
75+
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' .
76+
') ENGINE=InnoDB"'
77+
);
78+
$this->assertEquals(
79+
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE',
80+
CreateDefinition::build($parser->statements[0]->fields[2])
81+
);
82+
}
6783
}

tests/Utils/CLITest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,39 @@ public function tokenizeParams()
220220
*/
221221
public function testStdinPipe($cmd, $result)
222222
{
223-
exec ($cmd, $out, $ret);
223+
exec($cmd, $out, $ret);
224224
$this->assertSame($result, $ret);
225225
}
226226

227227
public function stdinParams()
228228
{
229-
$binPath = PHP_BINARY .' '. dirname(__DIR__,2 ). '/bin/';
229+
$binPath = PHP_BINARY . ' ' . dirname(__DIR__, 2) . '/bin/';
230230

231231
return [
232-
['echo "SELECT 1" | '. $binPath .'highlight-query', 0],
233-
['echo "invalid query" | '. $binPath .'highlight-query', 0],
234-
['echo "SELECT 1" | '. $binPath .'lint-query', 0],
235-
['echo "invalid query" | '. $binPath .'lint-query', 10],
236-
['echo "SELECT 1" | '. $binPath .'tokenize-query', 0],
237-
['echo "invalid query" | '. $binPath .'tokenize-query', 0],
232+
[
233+
'echo "SELECT 1" | ' . $binPath . 'highlight-query',
234+
0,
235+
],
236+
[
237+
'echo "invalid query" | ' . $binPath . 'highlight-query',
238+
0,
239+
],
240+
[
241+
'echo "SELECT 1" | ' . $binPath . 'lint-query',
242+
0,
243+
],
244+
[
245+
'echo "invalid query" | ' . $binPath . 'lint-query',
246+
10,
247+
],
248+
[
249+
'echo "SELECT 1" | ' . $binPath . 'tokenize-query',
250+
0,
251+
],
252+
[
253+
'echo "invalid query" | ' . $binPath . 'tokenize-query',
254+
0,
255+
],
238256
];
239257
}
240258
}

0 commit comments

Comments
 (0)