Skip to content

Split up Context::escape() #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ parameters:
count: 1
path: src/Components/ArrayObj.php

-
message: "#^Binary operation \"\\.\" between ' AS ' and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: src/Components/CaseExpression.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\CaseExpression\\:\\:\\$alias \\(string\\|null\\) does not accept mixed\\.$#"
count: 1
Expand All @@ -90,11 +85,6 @@ parameters:
count: 1
path: src/Components/Condition.php

-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and ' ' results in an error\\.$#"
count: 1
path: src/Components/CreateDefinition.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\CreateDefinition\\:\\:\\$name \\(string\\|null\\) does not accept mixed\\.$#"
count: 2
Expand Down Expand Up @@ -125,16 +115,6 @@ parameters:
count: 1
path: src/Components/DataType.php

-
message: "#^Binary operation \"\\.\" between ' AS ' and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: src/Components/Expression.php

-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<string\\>\\|string given\\.$#"
count: 1
path: src/Components/Expression.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\:\\:\\$alias \\(string\\|null\\) does not accept mixed\\.$#"
count: 3
Expand Down Expand Up @@ -250,16 +230,6 @@ parameters:
count: 1
path: src/Components/JoinKeyword.php

-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and ' ' results in an error\\.$#"
count: 1
path: src/Components/Key.php

-
message: "#^Binary operation \"\\.\\=\" between '' and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: src/Components/Key.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$columns \\(array\\<array\\{name\\?\\: string, length\\?\\: int, order\\?\\: string\\}\\>\\) does not accept non\\-empty\\-array\\<array\\<string, mixed\\>\\>\\.$#"
count: 1
Expand Down Expand Up @@ -430,11 +400,6 @@ parameters:
count: 1
path: src/Components/Reference.php

-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<string\\>\\|string given\\.$#"
count: 1
path: src/Components/Reference.php

-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Reference\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -480,11 +445,6 @@ parameters:
count: 1
path: src/Components/UnionKeyword.php

-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) should return array\\<string\\>\\|string but returns array\\<array\\<string\\>\\|string\\>\\.$#"
count: 1
path: src/Context.php

-
message: "#^Static property PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:\\$keywords \\(non\\-empty\\-array\\<non\\-empty\\-string, int\\>\\) does not accept default value of type array\\{\\}\\.$#"
count: 1
Expand Down Expand Up @@ -690,21 +650,6 @@ parameters:
count: 1
path: src/Statements/DeleteStatement.php

-
message: "#^Binary operation \"\\.\" between ' ' and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: src/Statements/ExplainStatement.php

-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and '\\.' results in an error\\.$#"
count: 1
path: src/Statements/ExplainStatement.php

-
message: "#^Binary operation \"\\.\\=\" between string and array\\<string\\>\\|string results in an error\\.$#"
count: 1
path: src/Statements/ExplainStatement.php

-
message: "#^Cannot access property \\$keyword on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#"
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 @@ -126,9 +126,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($component->alias)]]></code>
</PossiblyInvalidOperand>
</file>
<file src="src/Components/Condition.php">
<InvalidArgument>
Expand Down Expand Up @@ -164,9 +161,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($component->name)]]></code>
</PossiblyInvalidOperand>
<PossiblyNullOperand>
<code><![CDATA[$component->options]]></code>
</PossiblyNullOperand>
Expand Down Expand Up @@ -210,12 +204,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidArgument>
<code>Context::escape($fields)</code>
</PossiblyInvalidArgument>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($component->alias)]]></code>
</PossiblyInvalidOperand>
<PossiblyNullOperand>
<code><![CDATA[$ret->expr]]></code>
<code><![CDATA[$ret->expr]]></code>
Expand Down Expand Up @@ -366,10 +354,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($column['name'])]]></code>
<code><![CDATA[Context::escape($component->name)]]></code>
</PossiblyInvalidOperand>
<PossiblyNullOperand>
<code><![CDATA[$component->options]]></code>
<code><![CDATA[$component->options]]></code>
Expand Down Expand Up @@ -528,9 +512,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($component->name)]]></code>
</PossiblyInvalidOperand>
<PossiblyNullPropertyAssignmentValue>
<code>$inOut</code>
<code>$name</code>
Expand Down Expand Up @@ -586,9 +567,6 @@
<MoreSpecificImplementedParamType>
<code>$component</code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidArgument>
<code><![CDATA[Context::escape($component->columns)]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidPropertyFetch>
<code><![CDATA[ArrayObj::parse($parser, $list)->values]]></code>
</PossiblyInvalidPropertyFetch>
Expand Down Expand Up @@ -667,12 +645,6 @@
<InvalidPropertyFetch>
<code>$context::$keywords</code>
</InvalidPropertyFetch>
<InvalidReturnStatement>
<code>$str</code>
</InvalidReturnStatement>
<InvalidReturnType>
<code>string|string[]</code>
</InvalidReturnType>
<MixedAssignment>
<code>self::$keywords</code>
</MixedAssignment>
Expand Down Expand Up @@ -1118,12 +1090,6 @@
<code><![CDATA[$this->explainedDatabase]]></code>
<code><![CDATA[$this->explainedTable]]></code>
</MixedAssignment>
<PossiblyInvalidOperand>
<code><![CDATA[Context::escape($this->explainedColumn)]]></code>
<code><![CDATA[Context::escape($this->explainedDatabase)]]></code>
<code><![CDATA[Context::escape($this->explainedTable)]]></code>
<code><![CDATA[Context::escape($this->explainedTable)]]></code>
</PossiblyInvalidOperand>
<PossiblyNullPropertyFetch>
<code><![CDATA[$nextToken->keyword]]></code>
<code><![CDATA[$nextToken->value]]></code>
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public static function build($component, array $options = []): string
$fields[] = $component->column;
}

$ret = implode('.', Context::escape($fields));
$ret = implode('.', Context::escapeAll($fields));
}

if (! empty($component->alias)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function build($component, array $options = []): string
{
return trim(
$component->table
. ' (' . implode(', ', Context::escape($component->columns)) . ') '
. ' (' . implode(', ', Context::escapeAll($component->columns)) . ') '
. $component->options
);
}
Expand Down
33 changes: 20 additions & 13 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use function explode;
use function in_array;
use function intval;
use function is_array;
use function is_int;
use function is_numeric;
use function str_replace;
Expand Down Expand Up @@ -669,21 +668,13 @@ private static function getModeFromString(string $mode): int
/**
* Escapes the symbol by adding surrounding backticks.
*
* @param string[]|string $str the string to be escaped
* @param string $quote quote to be used when escaping
* @param string $str the string to be escaped
* @param string $quote quote to be used when escaping
*
* @return string|string[]
* @return string
*/
public static function escape($str, string $quote = '`')
public static function escape(string $str, string $quote = '`')
{
if (is_array($str)) {
foreach ($str as $key => $value) {
$str[$key] = static::escape($value);
}

return $str;
}

if ((static::$mode & self::SQL_MODE_NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) {
return $str;
}
Expand All @@ -695,6 +686,22 @@ public static function escape($str, string $quote = '`')
return $quote . str_replace($quote, $quote . $quote, $str) . $quote;
}

/**
* Escapes the symbol by adding surrounding backticks.
*
* @param string[] $strings the string to be escaped
*
* @return string[]
*/
public static function escapeAll(array $strings): array
{
foreach ($strings as $key => $value) {
$strings[$key] = static::escape($value);
}

return $strings;
}

/**
* Returns char used to quote identifiers based on currently set SQL Mode (ie. standard or ANSI_QUOTES)
*
Expand Down
8 changes: 7 additions & 1 deletion tests/Lexer/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ public function testEscape(): void
Context::setMode();
$this->assertEquals('`test`', Context::escape('test'));

$this->assertEquals(['`a`', '`b`'], Context::escape(['a', 'b']));
$this->assertEquals(['`a`', '`b`'], Context::escapeAll(['a', 'b']));
}

public function testEscapeAll(): void
{
Context::setMode();
$this->assertEquals(['`a`', '`b`'], Context::escapeAll(['a', 'b']));
}
}