Skip to content

Commit

Permalink
Split up build() into a static and instance method
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela committed Aug 30, 2023
1 parent 9516074 commit dfe2577
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 90 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Cannot assign new offset to array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string\\.$#"
count: 3
path: src/Components/AlterOperation.php

-
message: "#^Parameter \\#1 \\$key of function array_key_exists expects int\\|string, float\\|int\\|string given\\.$#"
count: 2
Expand All @@ -20,11 +15,6 @@ parameters:
count: 1
path: src/Components/AlterOperation.php

-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
count: 3
path: src/Components/AlterOperation.php

-
message: "#^Cannot access an offset on array\\<int\\<0, max\\>, mixed\\>\\|static\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\)\\.$#"
count: 1
Expand Down Expand Up @@ -510,11 +500,6 @@ parameters:
count: 2
path: src/Statements/CreateStatement.php

-
message: "#^Cannot assign new offset to array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string\\.$#"
count: 4
path: src/Statements/CreateStatement.php

-
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 8
Expand Down Expand Up @@ -560,11 +545,6 @@ parameters:
count: 1
path: src/Statements/CreateStatement.php

-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
count: 4
path: src/Statements/CreateStatement.php

-
message: "#^Argument of an invalid type array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\|null supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down
34 changes: 0 additions & 34 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1153,11 +1153,6 @@
<code><![CDATA[$this->withers]]></code>
</PossiblyNullArrayOffset>
</file>
<file src="src/TokensList.php">
<RedundantConditionGivenDocblockType>
<code>is_array($list)</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Tools/ContextGenerator.php">
<PossiblyNullArgument>
<code><![CDATA[$options['keywords']]]></code>
Expand Down Expand Up @@ -1419,41 +1414,12 @@
<InvalidArgument>
<code>$find[$k]</code>
</InvalidArgument>
<MixedArgument>
<code><![CDATA[$list->tokens[$j]]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code>$newList</code>
<code>$newList</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$list->tokens[$i]]]></code>
<code><![CDATA[$list->tokens[$j]]]></code>
<code><![CDATA[$list->tokens[$j]]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$newList[]</code>
<code>$newList[]</code>
</MixedAssignment>
<MixedPropertyFetch>
<code><![CDATA[$list->tokens[$i]->type]]></code>
<code><![CDATA[$list->tokens[$j]->type]]></code>
</MixedPropertyFetch>
<PossiblyInvalidArgument>
<code>$list</code>
<code><![CDATA[$pattern['value_str']]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidOperand>
<code><![CDATA[$pattern['flags']]]></code>
</PossiblyInvalidOperand>
<PossiblyInvalidPropertyFetch>
<code><![CDATA[$list->count]]></code>
<code><![CDATA[$list->tokens]]></code>
</PossiblyInvalidPropertyFetch>
<UndefinedPropertyFetch>
<code><![CDATA[$list->count]]></code>
<code><![CDATA[$list->tokens]]></code>
</UndefinedPropertyFetch>
</file>
<file src="tests/Builder/AlterStatementTest.php">
<PossiblyUnusedMethod>
Expand Down
10 changes: 4 additions & 6 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,8 @@ final class AlterOperation implements Component

/**
* Unparsed tokens.
*
* @var Token[]|string
*/
public $unknown = [];
public TokensList $unknown;

/**
* @param OptionsArray $options options of alter operation
Expand All @@ -288,12 +286,12 @@ public function __construct(
$options = null,
$field = null,
$partitions = null,
$unknown = []
array $unknown = []
) {
$this->partitions = $partitions;
$this->options = $options;
$this->field = $field;
$this->unknown = $unknown;
$this->unknown = new TokensList($unknown);
}

/**
Expand Down Expand Up @@ -543,7 +541,7 @@ public static function build($component): string
$ret .= $component->field . ' ';
}

$ret .= $afterFieldsOptions . TokensList::build($component->unknown);
$ret .= $afterFieldsOptions . $component->unknown->build();

if (isset($component->partitions)) {
$ret .= PartitionDefinition::buildAll($component->partitions);
Expand Down
12 changes: 6 additions & 6 deletions src/Statements/CreateStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ class CreateStatement extends Statement
* For views, it is the select statement that creates the view.
* Used by `CREATE FUNCTION`, `CREATE PROCEDURE` and `CREATE VIEW`.
*
* @var Token[]|string
* @var Token[]
*/
public $body = [];
public array $body = [];

public function build(): string
{
Expand Down Expand Up @@ -488,7 +488,7 @@ public function build(): string
. OptionsArray::build($this->options) . ' '
. Expression::build($this->name) . ' '
. $fields . ' AS ' . $builtStatement
. (! empty($this->body) ? TokensList::build($this->body) : '') . ' '
. TokensList::buildFromArray($this->body) . ' '
. OptionsArray::build($this->entityOptions);
}

Expand All @@ -498,7 +498,7 @@ public function build(): string
. Expression::build($this->name) . ' '
. OptionsArray::build($this->entityOptions) . ' '
. 'ON ' . Expression::build($this->table) . ' '
. 'FOR EACH ROW ' . TokensList::build($this->body);
. 'FOR EACH ROW ' . TokensList::buildFromArray($this->body);
}

if ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) {
Expand All @@ -512,13 +512,13 @@ public function build(): string
. Expression::build($this->name) . ' '
. ParameterDefinition::buildAll($this->parameters) . ' '
. $tmp . ' ' . OptionsArray::build($this->entityOptions) . ' '
. TokensList::build($this->body);
. TokensList::buildFromArray($this->body);
}

return 'CREATE '
. OptionsArray::build($this->options) . ' '
. Expression::build($this->name) . ' '
. TokensList::build($this->body);
. TokensList::buildFromArray($this->body);
}

/**
Expand Down
27 changes: 12 additions & 15 deletions src/TokensList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function count;
use function in_array;
use function is_array;
use function is_string;

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

/**
* Builds an array of tokens by merging their raw value.
*
* @param string|Token[]|TokensList $list the tokens to be built
*/
public static function build($list): string
public function build(): string
{
if (is_string($list)) {
return $list;
}

if ($list instanceof self) {
$list = $list->tokens;
}
return static::buildFromArray($this->tokens);
}

/**
* Builds an array of tokens by merging their raw value.
*
* @param Token[] $list the tokens to be built
*/
public static function buildFromArray(array $list): string
{
$ret = '';
if (is_array($list)) {
foreach ($list as $tok) {
$ret .= $tok->token;
}
foreach ($list as $token) {
$ret .= $token->token;
}

return $ret;
Expand Down
8 changes: 3 additions & 5 deletions src/Utils/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ public static function replaceTokens(
$isList = $list instanceof TokensList;

// Parsing the tokens.
if (! $isList) {
if (! $list instanceof TokensList) {
$list = Lexer::getTokens($list);
}

/**
* The list to be returned.
*
* @var Token[]
*/
$newList = [];
$newList = new TokensList();

/**
* The length of the find pattern is calculated only once.
Expand Down Expand Up @@ -148,6 +146,6 @@ public static function replaceTokens(
}
}

return $isList ? new TokensList($newList) : TokensList::build($newList);
return $isList ? $newList : $newList->build();
}
}
21 changes: 18 additions & 3 deletions tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PHPUnit\Framework\Attributes\DataProvider;

Expand Down Expand Up @@ -524,7 +525,7 @@ public function testBuilderCreateProcedure(): void

$this->assertSame(
'SELECT _var',
TokensList::build($stmt->body)
TokensList::buildFromArray($stmt->body)
);
}

Expand Down Expand Up @@ -652,7 +653,7 @@ public function testBuilderCreateFunction(): void
. ' RETURN TRUE;' . "\n"
. ' END IF;' . "\n"
. 'END',
TokensList::build($stmt->body)
TokensList::buildFromArray($stmt->body)
);
}

Expand All @@ -664,7 +665,21 @@ public function testBuilderTrigger(): void
$stmt->name = new Expression('ins_sum');
$stmt->entityOptions = new OptionsArray(['BEFORE', 'INSERT']);
$stmt->table = new Expression('account');
$stmt->body = 'SET @sum = @sum + NEW.amount';
$stmt->body = [
new Token('SET', Token::TYPE_KEYWORD),
new Token(' ', Token::TYPE_WHITESPACE),
new Token('@sum', Token::TYPE_NONE),
new Token(' ', Token::TYPE_WHITESPACE),
new Token('=', Token::TYPE_OPERATOR),
new Token(' ', Token::TYPE_WHITESPACE),
new Token('@sum', Token::TYPE_NONE),
new Token(' ', Token::TYPE_WHITESPACE),
new Token('+', Token::TYPE_OPERATOR),
new Token(' ', Token::TYPE_WHITESPACE),
new Token('NEW', Token::TYPE_KEYWORD),
new Token('.', Token::TYPE_OPERATOR),
new Token('amount', Token::TYPE_NONE),
];

$this->assertEquals(
'CREATE TRIGGER ins_sum BEFORE INSERT ON account ' .
Expand Down
3 changes: 2 additions & 1 deletion tests/Lexer/TokensListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function setUp(): void
public function testBuild(): void
{
$list = new TokensList($this->tokens);
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', TokensList::build($list));
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', $list->build());
$this->assertEquals('SELECT * FROM `test` WHERE name=fa', TokensList::buildFromArray($this->tokens));
}

public function testAdd(): void
Expand Down

0 comments on commit dfe2577

Please sign in to comment.