Skip to content

Commit 875ce2b

Browse files
Merge pull request #480 from kamil-tekiela/Context-escape
Split up Context::escape()
2 parents 7980970 + f9da4ee commit 875ce2b

File tree

6 files changed

+29
-105
lines changed

6 files changed

+29
-105
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ parameters:
6060
count: 1
6161
path: src/Components/ArrayObj.php
6262

63-
-
64-
message: "#^Binary operation \"\\.\" between ' AS ' and array\\<string\\>\\|string results in an error\\.$#"
65-
count: 1
66-
path: src/Components/CaseExpression.php
67-
6863
-
6964
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\CaseExpression\\:\\:\\$alias \\(string\\|null\\) does not accept mixed\\.$#"
7065
count: 1
@@ -90,11 +85,6 @@ parameters:
9085
count: 1
9186
path: src/Components/Condition.php
9287

93-
-
94-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and ' ' results in an error\\.$#"
95-
count: 1
96-
path: src/Components/CreateDefinition.php
97-
9888
-
9989
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\CreateDefinition\\:\\:\\$name \\(string\\|null\\) does not accept mixed\\.$#"
10090
count: 2
@@ -125,16 +115,6 @@ parameters:
125115
count: 1
126116
path: src/Components/DataType.php
127117

128-
-
129-
message: "#^Binary operation \"\\.\" between ' AS ' and array\\<string\\>\\|string results in an error\\.$#"
130-
count: 1
131-
path: src/Components/Expression.php
132-
133-
-
134-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<string\\>\\|string given\\.$#"
135-
count: 1
136-
path: src/Components/Expression.php
137-
138118
-
139119
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\:\\:\\$alias \\(string\\|null\\) does not accept mixed\\.$#"
140120
count: 3
@@ -250,16 +230,6 @@ parameters:
250230
count: 1
251231
path: src/Components/JoinKeyword.php
252232

253-
-
254-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and ' ' results in an error\\.$#"
255-
count: 1
256-
path: src/Components/Key.php
257-
258-
-
259-
message: "#^Binary operation \"\\.\\=\" between '' and array\\<string\\>\\|string results in an error\\.$#"
260-
count: 1
261-
path: src/Components/Key.php
262-
263233
-
264234
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$columns \\(array\\<array\\{name\\?\\: string, length\\?\\: int, order\\?\\: string\\}\\>\\) does not accept non\\-empty\\-array\\<array\\<string, mixed\\>\\>\\.$#"
265235
count: 1
@@ -430,11 +400,6 @@ parameters:
430400
count: 1
431401
path: src/Components/Reference.php
432402

433-
-
434-
message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\<string\\>\\|string given\\.$#"
435-
count: 1
436-
path: src/Components/Reference.php
437-
438403
-
439404
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Reference\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
440405
count: 1
@@ -480,11 +445,6 @@ parameters:
480445
count: 1
481446
path: src/Components/UnionKeyword.php
482447

483-
-
484-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) should return array\\<string\\>\\|string but returns array\\<array\\<string\\>\\|string\\>\\.$#"
485-
count: 1
486-
path: src/Context.php
487-
488448
-
489449
message: "#^Static property PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:\\$keywords \\(non\\-empty\\-array\\<non\\-empty\\-string, int\\>\\) does not accept default value of type array\\{\\}\\.$#"
490450
count: 1
@@ -690,21 +650,6 @@ parameters:
690650
count: 1
691651
path: src/Statements/DeleteStatement.php
692652

693-
-
694-
message: "#^Binary operation \"\\.\" between ' ' and array\\<string\\>\\|string results in an error\\.$#"
695-
count: 1
696-
path: src/Statements/ExplainStatement.php
697-
698-
-
699-
message: "#^Binary operation \"\\.\" between array\\<string\\>\\|string and '\\.' results in an error\\.$#"
700-
count: 1
701-
path: src/Statements/ExplainStatement.php
702-
703-
-
704-
message: "#^Binary operation \"\\.\\=\" between string and array\\<string\\>\\|string results in an error\\.$#"
705-
count: 1
706-
path: src/Statements/ExplainStatement.php
707-
708653
-
709654
message: "#^Cannot access property \\$keyword on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#"
710655
count: 1

psalm-baseline.xml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@
126126
<MoreSpecificImplementedParamType>
127127
<code>$component</code>
128128
</MoreSpecificImplementedParamType>
129-
<PossiblyInvalidOperand>
130-
<code><![CDATA[Context::escape($component->alias)]]></code>
131-
</PossiblyInvalidOperand>
132129
</file>
133130
<file src="src/Components/Condition.php">
134131
<InvalidArgument>
@@ -164,9 +161,6 @@
164161
<MoreSpecificImplementedParamType>
165162
<code>$component</code>
166163
</MoreSpecificImplementedParamType>
167-
<PossiblyInvalidOperand>
168-
<code><![CDATA[Context::escape($component->name)]]></code>
169-
</PossiblyInvalidOperand>
170164
<PossiblyNullOperand>
171165
<code><![CDATA[$component->options]]></code>
172166
</PossiblyNullOperand>
@@ -210,12 +204,6 @@
210204
<MoreSpecificImplementedParamType>
211205
<code>$component</code>
212206
</MoreSpecificImplementedParamType>
213-
<PossiblyInvalidArgument>
214-
<code>Context::escape($fields)</code>
215-
</PossiblyInvalidArgument>
216-
<PossiblyInvalidOperand>
217-
<code><![CDATA[Context::escape($component->alias)]]></code>
218-
</PossiblyInvalidOperand>
219207
<PossiblyNullOperand>
220208
<code><![CDATA[$ret->expr]]></code>
221209
<code><![CDATA[$ret->expr]]></code>
@@ -366,10 +354,6 @@
366354
<MoreSpecificImplementedParamType>
367355
<code>$component</code>
368356
</MoreSpecificImplementedParamType>
369-
<PossiblyInvalidOperand>
370-
<code><![CDATA[Context::escape($column['name'])]]></code>
371-
<code><![CDATA[Context::escape($component->name)]]></code>
372-
</PossiblyInvalidOperand>
373357
<PossiblyNullOperand>
374358
<code><![CDATA[$component->options]]></code>
375359
<code><![CDATA[$component->options]]></code>
@@ -528,9 +512,6 @@
528512
<MoreSpecificImplementedParamType>
529513
<code>$component</code>
530514
</MoreSpecificImplementedParamType>
531-
<PossiblyInvalidOperand>
532-
<code><![CDATA[Context::escape($component->name)]]></code>
533-
</PossiblyInvalidOperand>
534515
<PossiblyNullPropertyAssignmentValue>
535516
<code>$inOut</code>
536517
<code>$name</code>
@@ -586,9 +567,6 @@
586567
<MoreSpecificImplementedParamType>
587568
<code>$component</code>
588569
</MoreSpecificImplementedParamType>
589-
<PossiblyInvalidArgument>
590-
<code><![CDATA[Context::escape($component->columns)]]></code>
591-
</PossiblyInvalidArgument>
592570
<PossiblyInvalidPropertyFetch>
593571
<code><![CDATA[ArrayObj::parse($parser, $list)->values]]></code>
594572
</PossiblyInvalidPropertyFetch>
@@ -667,12 +645,6 @@
667645
<InvalidPropertyFetch>
668646
<code>$context::$keywords</code>
669647
</InvalidPropertyFetch>
670-
<InvalidReturnStatement>
671-
<code>$str</code>
672-
</InvalidReturnStatement>
673-
<InvalidReturnType>
674-
<code>string|string[]</code>
675-
</InvalidReturnType>
676648
<MixedAssignment>
677649
<code>self::$keywords</code>
678650
</MixedAssignment>
@@ -1118,12 +1090,6 @@
11181090
<code><![CDATA[$this->explainedDatabase]]></code>
11191091
<code><![CDATA[$this->explainedTable]]></code>
11201092
</MixedAssignment>
1121-
<PossiblyInvalidOperand>
1122-
<code><![CDATA[Context::escape($this->explainedColumn)]]></code>
1123-
<code><![CDATA[Context::escape($this->explainedDatabase)]]></code>
1124-
<code><![CDATA[Context::escape($this->explainedTable)]]></code>
1125-
<code><![CDATA[Context::escape($this->explainedTable)]]></code>
1126-
</PossiblyInvalidOperand>
11271093
<PossiblyNullPropertyFetch>
11281094
<code><![CDATA[$nextToken->keyword]]></code>
11291095
<code><![CDATA[$nextToken->value]]></code>

src/Components/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public static function build($component, array $options = []): string
472472
$fields[] = $component->column;
473473
}
474474

475-
$ret = implode('.', Context::escape($fields));
475+
$ret = implode('.', Context::escapeAll($fields));
476476
}
477477

478478
if (! empty($component->alias)) {

src/Components/Reference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function build($component, array $options = []): string
147147
{
148148
return trim(
149149
$component->table
150-
. ' (' . implode(', ', Context::escape($component->columns)) . ') '
150+
. ' (' . implode(', ', Context::escapeAll($component->columns)) . ') '
151151
. $component->options
152152
);
153153
}

src/Context.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use function explode;
99
use function in_array;
1010
use function intval;
11-
use function is_array;
1211
use function is_int;
1312
use function is_numeric;
1413
use function str_replace;
@@ -669,21 +668,13 @@ private static function getModeFromString(string $mode): int
669668
/**
670669
* Escapes the symbol by adding surrounding backticks.
671670
*
672-
* @param string[]|string $str the string to be escaped
673-
* @param string $quote quote to be used when escaping
671+
* @param string $str the string to be escaped
672+
* @param string $quote quote to be used when escaping
674673
*
675-
* @return string|string[]
674+
* @return string
676675
*/
677-
public static function escape($str, string $quote = '`')
676+
public static function escape(string $str, string $quote = '`')
678677
{
679-
if (is_array($str)) {
680-
foreach ($str as $key => $value) {
681-
$str[$key] = static::escape($value);
682-
}
683-
684-
return $str;
685-
}
686-
687678
if ((static::$mode & self::SQL_MODE_NO_ENCLOSING_QUOTES) && (! static::isKeyword($str, true))) {
688679
return $str;
689680
}
@@ -695,6 +686,22 @@ public static function escape($str, string $quote = '`')
695686
return $quote . str_replace($quote, $quote . $quote, $str) . $quote;
696687
}
697688

689+
/**
690+
* Escapes the symbol by adding surrounding backticks.
691+
*
692+
* @param string[] $strings the string to be escaped
693+
*
694+
* @return string[]
695+
*/
696+
public static function escapeAll(array $strings): array
697+
{
698+
foreach ($strings as $key => $value) {
699+
$strings[$key] = static::escape($value);
700+
}
701+
702+
return $strings;
703+
}
704+
698705
/**
699706
* Returns char used to quote identifiers based on currently set SQL Mode (ie. standard or ANSI_QUOTES)
700707
*

tests/Lexer/ContextTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ public function testEscape(): void
222222
Context::setMode();
223223
$this->assertEquals('`test`', Context::escape('test'));
224224

225-
$this->assertEquals(['`a`', '`b`'], Context::escape(['a', 'b']));
225+
$this->assertEquals(['`a`', '`b`'], Context::escapeAll(['a', 'b']));
226+
}
227+
228+
public function testEscapeAll(): void
229+
{
230+
Context::setMode();
231+
$this->assertEquals(['`a`', '`b`'], Context::escapeAll(['a', 'b']));
226232
}
227233
}

0 commit comments

Comments
 (0)