Skip to content

Commit 2a796f9

Browse files
committed
Split up ParameterDefinition
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent a0d0cb5 commit 2a796f9

File tree

8 files changed

+155
-130
lines changed

8 files changed

+155
-130
lines changed

phpstan-baseline.neon

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ parameters:
230230
count: 2
231231
path: src/Components/Lists/OrderKeywords.php
232232

233+
-
234+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$name \\(string\\) does not accept mixed\\.$#"
235+
count: 1
236+
path: src/Components/Lists/ParameterDefinitions.php
237+
238+
-
239+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$name \\(string\\) in isset\\(\\) is not nullable\\.$#"
240+
count: 1
241+
path: src/Components/Lists/ParameterDefinitions.php
242+
243+
-
244+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$type \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
245+
count: 1
246+
path: src/Components/Lists/ParameterDefinitions.php
247+
233248
-
234249
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\LockExpression\\:\\:\\$table \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
235250
count: 1
@@ -295,24 +310,14 @@ parameters:
295310
count: 1
296311
path: src/Components/ParameterDefinition.php
297312

298-
-
299-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$name \\(string\\) does not accept mixed\\.$#"
300-
count: 1
301-
path: src/Components/ParameterDefinition.php
302-
303313
-
304314
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$name \\(string\\) does not accept string\\|null\\.$#"
305315
count: 1
306316
path: src/Components/ParameterDefinition.php
307317

308-
-
309-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$name \\(string\\) in isset\\(\\) is not nullable\\.$#"
310-
count: 1
311-
path: src/Components/ParameterDefinition.php
312-
313318
-
314319
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$type \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#"
315-
count: 2
320+
count: 1
316321
path: src/Components/ParameterDefinition.php
317322

318323
-
@@ -491,7 +496,7 @@ parameters:
491496
path: src/Statements/CreateStatement.php
492497

493498
-
494-
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>\\|null given\\.$#"
499+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Lists\\\\ParameterDefinitions\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>\\|null given\\.$#"
495500
count: 1
496501
path: src/Statements/CreateStatement.php
497502

psalm-baseline.xml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@
283283
<code>$options</code>
284284
</UnusedParam>
285285
</file>
286+
<file src="src/Components/Lists/ParameterDefinitions.php">
287+
<MixedAssignment>
288+
<code><![CDATA[$expr->name]]></code>
289+
</MixedAssignment>
290+
<PossiblyNullPropertyAssignmentValue>
291+
<code>DataType::parse($parser, $list)</code>
292+
</PossiblyNullPropertyAssignmentValue>
293+
<RedundantConditionGivenDocblockType>
294+
<code><![CDATA[isset($expr->name)]]></code>
295+
</RedundantConditionGivenDocblockType>
296+
<UnusedParam>
297+
<code>$options</code>
298+
</UnusedParam>
299+
</file>
286300
<file src="src/Components/LockExpression.php">
287301
<MissingConstructor>
288302
<code>$table</code>
@@ -383,21 +397,11 @@
383397
</PossiblyNullPropertyAssignmentValue>
384398
</file>
385399
<file src="src/Components/ParameterDefinition.php">
386-
<MixedAssignment>
387-
<code><![CDATA[$expr->name]]></code>
388-
</MixedAssignment>
389400
<PossiblyNullPropertyAssignmentValue>
390401
<code>$inOut</code>
391402
<code>$name</code>
392403
<code>$type</code>
393-
<code>DataType::parse($parser, $list)</code>
394404
</PossiblyNullPropertyAssignmentValue>
395-
<RedundantConditionGivenDocblockType>
396-
<code><![CDATA[isset($expr->name)]]></code>
397-
</RedundantConditionGivenDocblockType>
398-
<UnusedParam>
399-
<code>$options</code>
400-
</UnusedParam>
401405
</file>
402406
<file src="src/Components/PartitionDefinition.php">
403407
<MissingConstructor>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Components\Lists;
6+
7+
use PhpMyAdmin\SqlParser\Components\DataType;
8+
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
9+
use PhpMyAdmin\SqlParser\Parser;
10+
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
12+
13+
use function implode;
14+
15+
/**
16+
* The definition of a parameter of a function or procedure.
17+
*/
18+
final class ParameterDefinitions
19+
{
20+
/**
21+
* @param Parser $parser the parser that serves as context
22+
* @param TokensList $list the list of tokens that are being parsed
23+
* @param array<string, mixed> $options parameters for parsing
24+
*
25+
* @return ParameterDefinition[]
26+
*/
27+
public static function parse(Parser $parser, TokensList $list, array $options = []): array
28+
{
29+
$ret = [];
30+
31+
$expr = new ParameterDefinition();
32+
33+
/**
34+
* The state of the parser.
35+
*
36+
* Below are the states of the parser.
37+
*
38+
* 0 -----------------------[ ( ]------------------------> 1
39+
*
40+
* 1 ----------------[ IN / OUT / INOUT ]----------------> 1
41+
* 1 ----------------------[ name ]----------------------> 2
42+
*
43+
* 2 -------------------[ data type ]--------------------> 3
44+
*
45+
* 3 ------------------------[ , ]-----------------------> 1
46+
* 3 ------------------------[ ) ]-----------------------> (END)
47+
*
48+
* @var int
49+
*/
50+
$state = 0;
51+
52+
for (; $list->idx < $list->count; ++$list->idx) {
53+
/**
54+
* Token parsed at this moment.
55+
*/
56+
$token = $list->tokens[$list->idx];
57+
58+
// End of statement.
59+
if ($token->type === TokenType::Delimiter) {
60+
break;
61+
}
62+
63+
// Skipping whitespaces and comments.
64+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
65+
continue;
66+
}
67+
68+
if ($state === 0) {
69+
if (($token->type === TokenType::Operator) && ($token->value === '(')) {
70+
$state = 1;
71+
}
72+
} elseif ($state === 1) {
73+
if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) {
74+
$expr->inOut = $token->value;
75+
++$list->idx;
76+
} elseif ($token->value === ')') {
77+
++$list->idx;
78+
break;
79+
} else {
80+
$expr->name = $token->value;
81+
$state = 2;
82+
}
83+
} elseif ($state === 2) {
84+
$expr->type = DataType::parse($parser, $list);
85+
$state = 3;
86+
} elseif ($state === 3) {
87+
$ret[] = $expr;
88+
$expr = new ParameterDefinition();
89+
if ($token->value === ',') {
90+
$state = 1;
91+
} elseif ($token->value === ')') {
92+
++$list->idx;
93+
break;
94+
}
95+
}
96+
}
97+
98+
// Last iteration was not saved.
99+
if (isset($expr->name) && ($expr->name !== '')) {
100+
$ret[] = $expr;
101+
}
102+
103+
--$list->idx;
104+
105+
return $ret;
106+
}
107+
108+
/** @param ParameterDefinition[] $component the component to be built */
109+
public static function buildAll(array $component): string
110+
{
111+
return '(' . implode(', ', $component) . ')';
112+
}
113+
}

src/Components/ParameterDefinition.php

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
use PhpMyAdmin\SqlParser\Component;
88
use PhpMyAdmin\SqlParser\Context;
9-
use PhpMyAdmin\SqlParser\Parser;
10-
use PhpMyAdmin\SqlParser\TokensList;
11-
use PhpMyAdmin\SqlParser\TokenType;
129

13-
use function implode;
1410
use function trim;
1511

1612
/**
@@ -51,94 +47,6 @@ public function __construct(string|null $name = null, string|null $inOut = null,
5147
$this->type = $type;
5248
}
5349

54-
/**
55-
* @param Parser $parser the parser that serves as context
56-
* @param TokensList $list the list of tokens that are being parsed
57-
* @param array<string, mixed> $options parameters for parsing
58-
*
59-
* @return ParameterDefinition[]
60-
*/
61-
public static function parse(Parser $parser, TokensList $list, array $options = []): array
62-
{
63-
$ret = [];
64-
65-
$expr = new static();
66-
67-
/**
68-
* The state of the parser.
69-
*
70-
* Below are the states of the parser.
71-
*
72-
* 0 -----------------------[ ( ]------------------------> 1
73-
*
74-
* 1 ----------------[ IN / OUT / INOUT ]----------------> 1
75-
* 1 ----------------------[ name ]----------------------> 2
76-
*
77-
* 2 -------------------[ data type ]--------------------> 3
78-
*
79-
* 3 ------------------------[ , ]-----------------------> 1
80-
* 3 ------------------------[ ) ]-----------------------> (END)
81-
*
82-
* @var int
83-
*/
84-
$state = 0;
85-
86-
for (; $list->idx < $list->count; ++$list->idx) {
87-
/**
88-
* Token parsed at this moment.
89-
*/
90-
$token = $list->tokens[$list->idx];
91-
92-
// End of statement.
93-
if ($token->type === TokenType::Delimiter) {
94-
break;
95-
}
96-
97-
// Skipping whitespaces and comments.
98-
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
99-
continue;
100-
}
101-
102-
if ($state === 0) {
103-
if (($token->type === TokenType::Operator) && ($token->value === '(')) {
104-
$state = 1;
105-
}
106-
} elseif ($state === 1) {
107-
if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) {
108-
$expr->inOut = $token->value;
109-
++$list->idx;
110-
} elseif ($token->value === ')') {
111-
++$list->idx;
112-
break;
113-
} else {
114-
$expr->name = $token->value;
115-
$state = 2;
116-
}
117-
} elseif ($state === 2) {
118-
$expr->type = DataType::parse($parser, $list);
119-
$state = 3;
120-
} elseif ($state === 3) {
121-
$ret[] = $expr;
122-
$expr = new static();
123-
if ($token->value === ',') {
124-
$state = 1;
125-
} elseif ($token->value === ')') {
126-
++$list->idx;
127-
break;
128-
}
129-
}
130-
}
131-
132-
// Last iteration was not saved.
133-
if (isset($expr->name) && ($expr->name !== '')) {
134-
$ret[] = $expr;
135-
}
136-
137-
--$list->idx;
138-
139-
return $ret;
140-
}
141-
14250
public function build(): string
14351
{
14452
$tmp = '';
@@ -151,12 +59,6 @@ public function build(): string
15159
);
15260
}
15361

154-
/** @param ParameterDefinition[] $component the component to be built */
155-
public static function buildAll(array $component): string
156-
{
157-
return '(' . implode(', ', $component) . ')';
158-
}
159-
16062
public function __toString(): string
16163
{
16264
return $this->build();

src/Statements/CreateStatement.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpMyAdmin\SqlParser\Components\DataType;
1010
use PhpMyAdmin\SqlParser\Components\Expression;
1111
use PhpMyAdmin\SqlParser\Components\Lists\CreateDefinitions;
12+
use PhpMyAdmin\SqlParser\Components\Lists\ParameterDefinitions;
1213
use PhpMyAdmin\SqlParser\Components\OptionsArray;
1314
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
1415
use PhpMyAdmin\SqlParser\Components\PartitionDefinition;
@@ -480,7 +481,7 @@ public function build(): string
480481
return 'CREATE '
481482
. $this->options->build() . ' '
482483
. $this->name->build() . ' '
483-
. ParameterDefinition::buildAll($this->parameters) . ' '
484+
. ParameterDefinitions::buildAll($this->parameters) . ' '
484485
. $tmp . ' ' . $this->entityOptions->build() . ' '
485486
. TokensList::buildFromArray($this->body);
486487
}
@@ -670,7 +671,7 @@ public function parse(Parser $parser, TokensList $list): void
670671
}
671672
}
672673
} elseif ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) {
673-
$this->parameters = ParameterDefinition::parse($parser, $list);
674+
$this->parameters = ParameterDefinitions::parse($parser, $list);
674675
if ($this->options->has('FUNCTION')) {
675676
$prevToken = $token;
676677
$token = $list->getNextOfType(TokenType::Keyword);

src/Utils/Routine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Utils;
66

77
use PhpMyAdmin\SqlParser\Components\DataType;
8-
use PhpMyAdmin\SqlParser\Components\ParameterDefinition;
8+
use PhpMyAdmin\SqlParser\Components\Lists\ParameterDefinitions;
99
use PhpMyAdmin\SqlParser\Lexer;
1010
use PhpMyAdmin\SqlParser\Parser;
1111
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
@@ -64,7 +64,7 @@ public static function getParameter(string $param): array
6464
$lexer = new Lexer('(' . $param . ')');
6565

6666
// A dummy parser is used for error reporting.
67-
$param = ParameterDefinition::parse(new Parser(), $lexer->list);
67+
$param = ParameterDefinitions::parse(new Parser(), $lexer->list);
6868

6969
if (empty($param[0])) {
7070
return [

0 commit comments

Comments
 (0)