Skip to content

Commit 49e122f

Browse files
committed
Split up build() into a static and instance method
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 8b37c13 commit 49e122f

File tree

8 files changed

+45
-90
lines changed

8 files changed

+45
-90
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Cannot assign new offset to array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string\\.$#"
5-
count: 3
6-
path: src/Components/AlterOperation.php
7-
83
-
94
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\AlterOperation\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
105
count: 1
@@ -15,11 +10,6 @@ parameters:
1510
count: 1
1611
path: src/Components/AlterOperation.php
1712

18-
-
19-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
20-
count: 3
21-
path: src/Components/AlterOperation.php
22-
2313
-
2414
message: "#^Cannot access an offset on array\\<int\\<0, max\\>, mixed\\>\\|static\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\)\\.$#"
2515
count: 1
@@ -500,11 +490,6 @@ parameters:
500490
count: 2
501491
path: src/Statements/CreateStatement.php
502492

503-
-
504-
message: "#^Cannot assign new offset to array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string\\.$#"
505-
count: 4
506-
path: src/Statements/CreateStatement.php
507-
508493
-
509494
message: "#^Cannot call method build\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
510495
count: 1
@@ -550,11 +535,6 @@ parameters:
550535
count: 1
551536
path: src/Statements/CreateStatement.php
552537

553-
-
554-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
555-
count: 4
556-
path: src/Statements/CreateStatement.php
557-
558538
-
559539
message: "#^Argument of an invalid type array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\|null supplied for foreach, only iterables are supported\\.$#"
560540
count: 1

psalm-baseline.xml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,6 @@
10601060
<code><![CDATA[$this->withers]]></code>
10611061
</PossiblyNullArrayOffset>
10621062
</file>
1063-
<file src="src/TokensList.php">
1064-
<RedundantConditionGivenDocblockType>
1065-
<code>is_array($list)</code>
1066-
</RedundantConditionGivenDocblockType>
1067-
</file>
10681063
<file src="src/Tools/ContextGenerator.php">
10691064
<PossiblyNullArgument>
10701065
<code><![CDATA[$options['keywords']]]></code>
@@ -1329,41 +1324,12 @@
13291324
<InvalidArgument>
13301325
<code>$find[$k]</code>
13311326
</InvalidArgument>
1332-
<MixedArgument>
1333-
<code><![CDATA[$list->tokens[$j]]]></code>
1334-
</MixedArgument>
1335-
<MixedArgumentTypeCoercion>
1336-
<code>$newList</code>
1337-
<code>$newList</code>
1338-
</MixedArgumentTypeCoercion>
1339-
<MixedArrayAccess>
1340-
<code><![CDATA[$list->tokens[$i]]]></code>
1341-
<code><![CDATA[$list->tokens[$j]]]></code>
1342-
<code><![CDATA[$list->tokens[$j]]]></code>
1343-
</MixedArrayAccess>
1344-
<MixedAssignment>
1345-
<code>$newList[]</code>
1346-
<code>$newList[]</code>
1347-
</MixedAssignment>
1348-
<MixedPropertyFetch>
1349-
<code><![CDATA[$list->tokens[$i]->type]]></code>
1350-
<code><![CDATA[$list->tokens[$j]->type]]></code>
1351-
</MixedPropertyFetch>
13521327
<PossiblyInvalidArgument>
1353-
<code>$list</code>
13541328
<code><![CDATA[$pattern['value_str']]]></code>
13551329
</PossiblyInvalidArgument>
13561330
<PossiblyInvalidOperand>
13571331
<code><![CDATA[$pattern['flags']]]></code>
13581332
</PossiblyInvalidOperand>
1359-
<PossiblyInvalidPropertyFetch>
1360-
<code><![CDATA[$list->count]]></code>
1361-
<code><![CDATA[$list->tokens]]></code>
1362-
</PossiblyInvalidPropertyFetch>
1363-
<UndefinedPropertyFetch>
1364-
<code><![CDATA[$list->count]]></code>
1365-
<code><![CDATA[$list->tokens]]></code>
1366-
</UndefinedPropertyFetch>
13671333
</file>
13681334
<file src="tests/Builder/AlterStatementTest.php">
13691335
<PossiblyUnusedMethod>

src/Components/AlterOperation.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,8 @@ final class AlterOperation implements Component
258258

259259
/**
260260
* Unparsed tokens.
261-
*
262-
* @var Token[]|string
263261
*/
264-
public $unknown = [];
262+
public TokensList $unknown;
265263

266264
/**
267265
* @param OptionsArray $options options of alter operation
@@ -273,12 +271,12 @@ public function __construct(
273271
$options = null,
274272
$field = null,
275273
$partitions = null,
276-
$unknown = []
274+
array $unknown = []
277275
) {
278276
$this->partitions = $partitions;
279277
$this->options = $options;
280278
$this->field = $field;
281-
$this->unknown = $unknown;
279+
$this->unknown = new TokensList($unknown);
282280
}
283281

284282
/**
@@ -525,7 +523,7 @@ public function build(): string
525523
$ret .= $this->field . ' ';
526524
}
527525

528-
$ret .= $afterFieldsOptions . TokensList::build($this->unknown);
526+
$ret .= $afterFieldsOptions . $this->unknown->build();
529527

530528
if (isset($this->partitions)) {
531529
$ret .= PartitionDefinition::buildAll($this->partitions);

src/Statements/CreateStatement.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ class CreateStatement extends Statement
390390
* For views, it is the select statement that creates the view.
391391
* Used by `CREATE FUNCTION`, `CREATE PROCEDURE` and `CREATE VIEW`.
392392
*
393-
* @var Token[]|string
393+
* @var Token[]
394394
*/
395-
public $body = [];
395+
public array $body = [];
396396

397397
public function build(): string
398398
{
@@ -476,7 +476,7 @@ public function build(): string
476476
. $this->options->build() . ' '
477477
. $this->name->build() . ' '
478478
. $fields . ' AS ' . $builtStatement
479-
. (! empty($this->body) ? TokensList::build($this->body) : '') . ' '
479+
. TokensList::buildFromArray($this->body) . ' '
480480
. ($this->entityOptions?->build() ?? '');
481481
}
482482

@@ -486,7 +486,7 @@ public function build(): string
486486
. $this->name->build() . ' '
487487
. $this->entityOptions->build() . ' '
488488
. 'ON ' . $this->table->build() . ' '
489-
. 'FOR EACH ROW ' . TokensList::build($this->body);
489+
. 'FOR EACH ROW ' . TokensList::buildFromArray($this->body);
490490
}
491491

492492
if ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) {
@@ -500,13 +500,13 @@ public function build(): string
500500
. $this->name->build() . ' '
501501
. ParameterDefinition::buildAll($this->parameters) . ' '
502502
. $tmp . ' ' . $this->entityOptions->build() . ' '
503-
. TokensList::build($this->body);
503+
. TokensList::buildFromArray($this->body);
504504
}
505505

506506
return 'CREATE '
507507
. $this->options->build() . ' '
508508
. $this->name->build() . ' '
509-
. TokensList::build($this->body);
509+
. TokensList::buildFromArray($this->body);
510510
}
511511

512512
/**

src/TokensList.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use function count;
1010
use function in_array;
1111
use function is_array;
12-
use function is_string;
1312

1413
/**
1514
* Defines an array of tokens and utility functions to iterate through it.
@@ -44,24 +43,22 @@ public function __construct(public array $tokens = [])
4443

4544
/**
4645
* Builds an array of tokens by merging their raw value.
47-
*
48-
* @param string|Token[]|TokensList $list the tokens to be built
4946
*/
50-
public static function build($list): string
47+
public function build(): string
5148
{
52-
if (is_string($list)) {
53-
return $list;
54-
}
55-
56-
if ($list instanceof self) {
57-
$list = $list->tokens;
58-
}
49+
return static::buildFromArray($this->tokens);
50+
}
5951

52+
/**
53+
* Builds an array of tokens by merging their raw value.
54+
*
55+
* @param Token[] $list the tokens to be built
56+
*/
57+
public static function buildFromArray(array $list): string
58+
{
6059
$ret = '';
61-
if (is_array($list)) {
62-
foreach ($list as $tok) {
63-
$ret .= $tok->token;
64-
}
60+
foreach ($list as $token) {
61+
$ret .= $token->token;
6562
}
6663

6764
return $ret;

src/Utils/Tokens.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,14 @@ public static function replaceTokens(
6464
$isList = $list instanceof TokensList;
6565

6666
// Parsing the tokens.
67-
if (! $isList) {
67+
if (! $list instanceof TokensList) {
6868
$list = Lexer::getTokens($list);
6969
}
7070

7171
/**
7272
* The list to be returned.
73-
*
74-
* @var Token[]
7573
*/
76-
$newList = [];
74+
$newList = new TokensList();
7775

7876
/**
7977
* The length of the find pattern is calculated only once.
@@ -148,6 +146,6 @@ public static function replaceTokens(
148146
}
149147
}
150148

151-
return $isList ? new TokensList($newList) : TokensList::build($newList);
149+
return $isList ? $newList : $newList->build();
152150
}
153151
}

tests/Builder/CreateStatementTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMyAdmin\SqlParser\Parser;
1414
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
1515
use PhpMyAdmin\SqlParser\Tests\TestCase;
16+
use PhpMyAdmin\SqlParser\Token;
1617
use PhpMyAdmin\SqlParser\TokensList;
1718
use PHPUnit\Framework\Attributes\DataProvider;
1819

@@ -524,7 +525,7 @@ public function testBuilderCreateProcedure(): void
524525

525526
$this->assertSame(
526527
'SELECT _var',
527-
TokensList::build($stmt->body)
528+
TokensList::buildFromArray($stmt->body)
528529
);
529530
}
530531

@@ -652,7 +653,7 @@ public function testBuilderCreateFunction(): void
652653
. ' RETURN TRUE;' . "\n"
653654
. ' END IF;' . "\n"
654655
. 'END',
655-
TokensList::build($stmt->body)
656+
TokensList::buildFromArray($stmt->body)
656657
);
657658
}
658659

@@ -664,7 +665,21 @@ public function testBuilderTrigger(): void
664665
$stmt->name = new Expression('ins_sum');
665666
$stmt->entityOptions = new OptionsArray(['BEFORE', 'INSERT']);
666667
$stmt->table = new Expression('account');
667-
$stmt->body = 'SET @sum = @sum + NEW.amount';
668+
$stmt->body = [
669+
new Token('SET', Token::TYPE_KEYWORD),
670+
new Token(' ', Token::TYPE_WHITESPACE),
671+
new Token('@sum', Token::TYPE_NONE),
672+
new Token(' ', Token::TYPE_WHITESPACE),
673+
new Token('=', Token::TYPE_OPERATOR),
674+
new Token(' ', Token::TYPE_WHITESPACE),
675+
new Token('@sum', Token::TYPE_NONE),
676+
new Token(' ', Token::TYPE_WHITESPACE),
677+
new Token('+', Token::TYPE_OPERATOR),
678+
new Token(' ', Token::TYPE_WHITESPACE),
679+
new Token('NEW', Token::TYPE_KEYWORD),
680+
new Token('.', Token::TYPE_OPERATOR),
681+
new Token('amount', Token::TYPE_NONE),
682+
];
668683

669684
$this->assertEquals(
670685
'CREATE TRIGGER ins_sum BEFORE INSERT ON account ' .

tests/Lexer/TokensListTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function setUp(): void
4545
public function testBuild(): void
4646
{
4747
$list = new TokensList($this->tokens);
48-
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', TokensList::build($list));
48+
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', $list->build());
49+
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', TokensList::buildFromArray($this->tokens));
4950
}
5051

5152
public function testAdd(): void

0 commit comments

Comments
 (0)