Skip to content

Commit 5dbecfe

Browse files
committed
Split up PartitionDefinition
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 43d7c54 commit 5dbecfe

7 files changed

Lines changed: 250 additions & 182 deletions

File tree

phpstan-baseline.neon

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,11 @@ parameters:
9595
count: 1
9696
path: src/Components/ParameterDefinition.php
9797

98-
-
99-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
100-
count: 1
101-
path: src/Components/PartitionDefinition.php
102-
103-
-
104-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$name \\(string\\) does not accept mixed\\.$#"
105-
count: 1
106-
path: src/Components/PartitionDefinition.php
107-
10898
-
10999
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) in empty\\(\\) is not falsy\\.$#"
110100
count: 1
111101
path: src/Components/PartitionDefinition.php
112102

113-
-
114-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$subpartitions \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\>\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#"
115-
count: 1
116-
path: src/Components/PartitionDefinition.php
117-
118-
-
119-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$type \\(string\\) does not accept mixed\\.$#"
120-
count: 1
121-
path: src/Components/PartitionDefinition.php
122-
123103
-
124104
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Reference\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
125105
count: 1
@@ -425,6 +405,26 @@ parameters:
425405
count: 1
426406
path: src/Parsers/ParameterDefinitions.php
427407

408+
-
409+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
410+
count: 1
411+
path: src/Parsers/PartitionDefinitions.php
412+
413+
-
414+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$name \\(string\\) does not accept mixed\\.$#"
415+
count: 1
416+
path: src/Parsers/PartitionDefinitions.php
417+
418+
-
419+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$subpartitions \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\>\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#"
420+
count: 1
421+
path: src/Parsers/PartitionDefinitions.php
422+
423+
-
424+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\PartitionDefinition\\:\\:\\$type \\(string\\) does not accept mixed\\.$#"
425+
count: 1
426+
path: src/Parsers/PartitionDefinitions.php
427+
428428
-
429429
message: "#^Cannot access property \\$values on array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#"
430430
count: 1

psalm-baseline.xml

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,39 +161,12 @@
161161
<code>$subpartitions</code>
162162
<code>$type</code>
163163
</MissingConstructor>
164-
<MixedAssignment>
165-
<code><![CDATA[$ret->name]]></code>
166-
<code><![CDATA[$ret->type]]></code>
167-
</MixedAssignment>
168-
<MixedOperand>
169-
<code><![CDATA[$ret->name]]></code>
170-
</MixedOperand>
171-
<PossiblyNullPropertyAssignmentValue>
172-
<code><![CDATA[Expression::parse(
173-
$parser,
174-
$list,
175-
[
176-
'parenthesesDelimited' => true,
177-
'breakOnAlias' => true,
178-
],
179-
)]]></code>
180-
</PossiblyNullPropertyAssignmentValue>
181-
<PropertyTypeCoercion>
182-
<code><![CDATA[ArrayObjs::parse(
183-
$parser,
184-
$list,
185-
['type' => self::class],
186-
)]]></code>
187-
</PropertyTypeCoercion>
188-
<RedundantCondition>
189-
<code>$state === 6</code>
190-
</RedundantCondition>
164+
<PossiblyUnusedProperty>
165+
<code>$partitionOptions</code>
166+
</PossiblyUnusedProperty>
191167
<RedundantConditionGivenDocblockType>
192168
<code><![CDATA[! empty($this->options)]]></code>
193169
</RedundantConditionGivenDocblockType>
194-
<UnusedVariable>
195-
<code>$idx</code>
196-
</UnusedVariable>
197170
</file>
198171
<file src="src/Components/Reference.php">
199172
<PossiblyNullPropertyAssignmentValue>
@@ -768,6 +741,38 @@
768741
<code><![CDATA[isset($expr->name)]]></code>
769742
</RedundantConditionGivenDocblockType>
770743
</file>
744+
<file src="src/Parsers/PartitionDefinitions.php">
745+
<MixedAssignment>
746+
<code><![CDATA[$ret->name]]></code>
747+
<code><![CDATA[$ret->type]]></code>
748+
</MixedAssignment>
749+
<MixedOperand>
750+
<code><![CDATA[$ret->name]]></code>
751+
</MixedOperand>
752+
<PossiblyNullPropertyAssignmentValue>
753+
<code><![CDATA[Expression::parse(
754+
$parser,
755+
$list,
756+
[
757+
'parenthesesDelimited' => true,
758+
'breakOnAlias' => true,
759+
],
760+
)]]></code>
761+
</PossiblyNullPropertyAssignmentValue>
762+
<PropertyTypeCoercion>
763+
<code><![CDATA[ArrayObjs::parse(
764+
$parser,
765+
$list,
766+
['type' => self::class],
767+
)]]></code>
768+
</PropertyTypeCoercion>
769+
<RedundantCondition>
770+
<code>$state === 6</code>
771+
</RedundantCondition>
772+
<UnusedVariable>
773+
<code>$idx</code>
774+
</UnusedVariable>
775+
</file>
771776
<file src="src/Parsers/References.php">
772777
<PossiblyInvalidPropertyFetch>
773778
<code><![CDATA[ArrayObjs::parse($parser, $list)->values]]></code>

src/Components/AlterOperation.php

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

77
use PhpMyAdmin\SqlParser\Component;
8+
use PhpMyAdmin\SqlParser\Parsers\PartitionDefinitions;
89
use PhpMyAdmin\SqlParser\Token;
910
use PhpMyAdmin\SqlParser\TokensList;
1011

@@ -73,7 +74,7 @@ public function build(): string
7374
$ret .= $afterFieldsOptions . TokensList::buildFromArray($this->unknown);
7475

7576
if (isset($this->partitions)) {
76-
$ret .= PartitionDefinition::buildAll($this->partitions);
77+
$ret .= PartitionDefinitions::buildAll($this->partitions);
7778
}
7879

7980
return trim($ret);

src/Components/PartitionDefinition.php

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
namespace PhpMyAdmin\SqlParser\Components;
66

77
use PhpMyAdmin\SqlParser\Component;
8-
use PhpMyAdmin\SqlParser\Parseable;
9-
use PhpMyAdmin\SqlParser\Parser;
10-
use PhpMyAdmin\SqlParser\Parsers\ArrayObjs;
11-
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
12-
use PhpMyAdmin\SqlParser\TokensList;
13-
use PhpMyAdmin\SqlParser\TokenType;
8+
use PhpMyAdmin\SqlParser\Parsers\PartitionDefinitions;
149

15-
use function implode;
1610
use function trim;
1711

1812
/**
1913
* Parses the create definition of a partition.
2014
*
2115
* Used for parsing `CREATE TABLE` statement.
2216
*/
23-
final class PartitionDefinition implements Component, Parseable
17+
final class PartitionDefinition implements Component
2418
{
2519
/**
2620
* All field options.
@@ -109,125 +103,13 @@ final class PartitionDefinition implements Component, Parseable
109103
*/
110104
public $options;
111105

112-
/**
113-
* @param Parser $parser the parser that serves as context
114-
* @param TokensList $list the list of tokens that are being parsed
115-
* @param array<string, mixed> $options parameters for parsing
116-
*/
117-
public static function parse(Parser $parser, TokensList $list, array $options = []): PartitionDefinition
118-
{
119-
$ret = new static();
120-
121-
/**
122-
* The state of the parser.
123-
*
124-
* Below are the states of the parser.
125-
*
126-
* 0 -------------[ PARTITION | SUBPARTITION ]------------> 1
127-
*
128-
* 1 -----------------------[ name ]----------------------> 2
129-
*
130-
* 2 ----------------------[ VALUES ]---------------------> 3
131-
*
132-
* 3 ---------------------[ LESS THAN ]-------------------> 4
133-
* 3 ------------------------[ IN ]-----------------------> 4
134-
*
135-
* 4 -----------------------[ expr ]----------------------> 5
136-
*
137-
* 5 ----------------------[ options ]--------------------> 6
138-
*
139-
* 6 ------------------[ subpartitions ]------------------> (END)
140-
*/
141-
$state = 0;
142-
143-
for (; $list->idx < $list->count; ++$list->idx) {
144-
/**
145-
* Token parsed at this moment.
146-
*/
147-
$token = $list->tokens[$list->idx];
148-
149-
// End of statement.
150-
if ($token->type === TokenType::Delimiter) {
151-
break;
152-
}
153-
154-
// Skipping whitespaces and comments.
155-
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
156-
continue;
157-
}
158-
159-
if ($state === 0) {
160-
$ret->isSubpartition = ($token->type === TokenType::Keyword) && ($token->keyword === 'SUBPARTITION');
161-
$state = 1;
162-
} elseif ($state === 1) {
163-
$ret->name = $token->value;
164-
165-
// Looking ahead for a 'VALUES' keyword.
166-
// Loop until the end of the partition name (delimited by a whitespace)
167-
while ($nextToken = $list->tokens[++$list->idx]) {
168-
if ($nextToken->type !== TokenType::None) {
169-
break;
170-
}
171-
172-
$ret->name .= $nextToken->value;
173-
}
174-
175-
$idx = $list->idx--;
176-
// Get the first token after the white space.
177-
$nextToken = $list->tokens[++$idx];
178-
179-
$state = ($nextToken->type === TokenType::Keyword)
180-
&& ($nextToken->value === 'VALUES')
181-
? 2 : 5;
182-
} elseif ($state === 2) {
183-
$state = 3;
184-
} elseif ($state === 3) {
185-
$ret->type = $token->value;
186-
$state = 4;
187-
} elseif ($state === 4) {
188-
if ($token->value === 'MAXVALUE') {
189-
$ret->expr = $token->value;
190-
} else {
191-
$ret->expr = Expression::parse(
192-
$parser,
193-
$list,
194-
[
195-
'parenthesesDelimited' => true,
196-
'breakOnAlias' => true,
197-
],
198-
);
199-
}
200-
201-
$state = 5;
202-
} elseif ($state === 5) {
203-
$ret->options = OptionsArrays::parse($parser, $list, static::$partitionOptions);
204-
$state = 6;
205-
} elseif ($state === 6) {
206-
if (($token->type === TokenType::Operator) && ($token->value === '(')) {
207-
$ret->subpartitions = ArrayObjs::parse(
208-
$parser,
209-
$list,
210-
['type' => self::class],
211-
);
212-
++$list->idx;
213-
}
214-
215-
break;
216-
}
217-
}
218-
219-
--$list->idx;
220-
221-
return $ret;
222-
}
223-
224106
public function build(): string
225107
{
226108
if ($this->isSubpartition) {
227109
return trim('SUBPARTITION ' . $this->name . ' ' . $this->options);
228110
}
229111

230-
$subpartitions = empty($this->subpartitions) ? '' : ' ' . self::buildAll($this->subpartitions);
112+
$subpartitions = empty($this->subpartitions) ? '' : ' ' . PartitionDefinitions::buildAll($this->subpartitions);
231113

232114
return trim(
233115
'PARTITION ' . $this->name
@@ -237,12 +119,6 @@ public function build(): string
237119
);
238120
}
239121

240-
/** @param PartitionDefinition[] $component the component to be built */
241-
public static function buildAll(array $component): string
242-
{
243-
return "(\n" . implode(",\n", $component) . "\n)";
244-
}
245-
246122
public function __toString(): string
247123
{
248124
return $this->build();

0 commit comments

Comments
 (0)