Skip to content

Commit 8d77322

Browse files
Merge pull request #504 from kamil-tekiela/TokenList
Refactor TokenList
2 parents d4581b8 + c154f5c commit 8d77322

File tree

11 files changed

+43
-438
lines changed

11 files changed

+43
-438
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 45 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
@@ -830,26 +810,6 @@ parameters:
830810
count: 2
831811
path: src/Utils/Table.php
832812

833-
-
834-
message: "#^Binary operation \"&\" between int\\|string and int results in an error\\.$#"
835-
count: 1
836-
path: src/Utils/Tokens.php
837-
838-
-
839-
message: "#^Cannot cast mixed to string\\.$#"
840-
count: 1
841-
path: src/Utils/Tokens.php
842-
843-
-
844-
message: "#^Parameter \\#1 \\$string1 of function strcasecmp expects string, int\\|string given\\.$#"
845-
count: 1
846-
path: src/Utils/Tokens.php
847-
848-
-
849-
message: "#^Parameter \\#2 \\$pattern of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Tokens\\:\\:match\\(\\) expects array\\<string, int\\|string\\|null\\>, PhpMyAdmin\\\\SqlParser\\\\Token given\\.$#"
850-
count: 1
851-
path: src/Utils/Tokens.php
852-
853813
-
854814
message: "#^Cannot call method __toString\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
855815
count: 2
@@ -960,11 +920,6 @@ parameters:
960920
count: 1
961921
path: tests/Utils/TableTest.php
962922

963-
-
964-
message: "#^Parameter \\#2 \\$find of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Tokens\\:\\:replaceTokens\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>, array\\<array\\<string, string\\>\\> given\\.$#"
965-
count: 1
966-
path: tests/Utils/TokensTest.php
967-
968923
-
969924
message: "#^Expression \"\\$str1\\[\\$i\\]\" on a separate line does not do anything\\.$#"
970925
count: 1

psalm-baseline.xml

Lines changed: 0 additions & 54 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>
@@ -1325,46 +1320,6 @@
13251320
<code>has</code>
13261321
</PossiblyNullReference>
13271322
</file>
1328-
<file src="src/Utils/Tokens.php">
1329-
<InvalidArgument>
1330-
<code>$find[$k]</code>
1331-
</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>
1352-
<PossiblyInvalidArgument>
1353-
<code>$list</code>
1354-
<code><![CDATA[$pattern['value_str']]]></code>
1355-
</PossiblyInvalidArgument>
1356-
<PossiblyInvalidOperand>
1357-
<code><![CDATA[$pattern['flags']]]></code>
1358-
</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>
1367-
</file>
13681323
<file src="tests/Builder/AlterStatementTest.php">
13691324
<PossiblyUnusedMethod>
13701325
<code>provideBuilderForRenameColumn</code>
@@ -1803,15 +1758,6 @@
18031758
<code>getForeignKeysProvider</code>
18041759
</PossiblyUnusedMethod>
18051760
</file>
1806-
<file src="tests/Utils/TokensTest.php">
1807-
<InvalidArgument>
1808-
<code>$find</code>
1809-
</InvalidArgument>
1810-
<PossiblyUnusedMethod>
1811-
<code>matchProvider</code>
1812-
<code>replaceTokensProvider</code>
1813-
</PossiblyUnusedMethod>
1814-
</file>
18151761
<file src="tests/benchmarks/UtfStringBench.php">
18161762
<MissingConstructor>
18171763
<code>$testContents</code>

src/Components/AlterOperation.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,6 @@ final class AlterOperation implements Component
256256
*/
257257
public $partitions;
258258

259-
/**
260-
* Unparsed tokens.
261-
*
262-
* @var Token[]|string
263-
*/
264-
public $unknown = [];
265-
266259
/**
267260
* @param OptionsArray $options options of alter operation
268261
* @param Expression|string|null $field altered field
@@ -273,7 +266,7 @@ public function __construct(
273266
$options = null,
274267
$field = null,
275268
$partitions = null,
276-
$unknown = []
269+
public array $unknown = []
277270
) {
278271
$this->partitions = $partitions;
279272
$this->options = $options;
@@ -525,7 +518,7 @@ public function build(): string
525518
$ret .= $this->field . ' ';
526519
}
527520

528-
$ret .= $afterFieldsOptions . TokensList::build($this->unknown);
521+
$ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown);
529522

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

src/Lexer.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,6 @@ class Lexer extends Core
144144
*/
145145
public $delimiterLen;
146146

147-
/**
148-
* Gets the tokens list parsed by a new instance of a lexer.
149-
*
150-
* @param string|UtfString $str the query to be lexed
151-
* @param bool $strict whether strict mode should be
152-
* enabled or not
153-
* @param string $delimiter the delimiter to be used
154-
*/
155-
public static function getTokens($str, $strict = false, $delimiter = null): TokensList
156-
{
157-
$lexer = new self($str, $strict, $delimiter);
158-
159-
return $lexer->list;
160-
}
161-
162147
/**
163148
* @param string|UtfString $str the query to be lexed
164149
* @param bool $strict whether strict mode should be

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/Statements/WithStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,6 @@ private function getSubTokenList(TokensList $list): ParserException|TokensList
330330

331331
$length = $list->idx - $idx;
332332

333-
return new TokensList(array_slice($list->tokens, $idx, $length), $length);
333+
return new TokensList(array_slice($list->tokens, $idx, $length));
334334
}
335335
}

src/TokensList.php

Lines changed: 14 additions & 30 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.
@@ -20,13 +19,6 @@
2019
*/
2120
class TokensList implements ArrayAccess
2221
{
23-
/**
24-
* The array of tokens.
25-
*
26-
* @var Token[]
27-
*/
28-
public $tokens = [];
29-
3022
/**
3123
* The count of tokens.
3224
*
@@ -42,39 +34,31 @@ class TokensList implements ArrayAccess
4234
public $idx = 0;
4335

4436
/**
45-
* @param Token[] $tokens the initial array of tokens
46-
* @param int $count the count of tokens in the initial array
37+
* @param Token[] $tokens The array of tokens.
4738
*/
48-
public function __construct(array $tokens = [], $count = -1)
39+
public function __construct(public array $tokens = [])
4940
{
50-
if ($tokens === []) {
51-
return;
52-
}
41+
$this->count = count($tokens);
42+
}
5343

54-
$this->tokens = $tokens;
55-
$this->count = $count === -1 ? count($tokens) : $count;
44+
/**
45+
* Builds an array of tokens by merging their raw value.
46+
*/
47+
public function build(): string
48+
{
49+
return static::buildFromArray($this->tokens);
5650
}
5751

5852
/**
5953
* Builds an array of tokens by merging their raw value.
6054
*
61-
* @param string|Token[]|TokensList $list the tokens to be built
55+
* @param Token[] $list the tokens to be built
6256
*/
63-
public static function build($list): string
57+
public static function buildFromArray(array $list): string
6458
{
65-
if (is_string($list)) {
66-
return $list;
67-
}
68-
69-
if ($list instanceof self) {
70-
$list = $list->tokens;
71-
}
72-
7359
$ret = '';
74-
if (is_array($list)) {
75-
foreach ($list as $tok) {
76-
$ret .= $tok->token;
77-
}
60+
foreach ($list as $token) {
61+
$ret .= $token->token;
7862
}
7963

8064
return $ret;

0 commit comments

Comments
 (0)