diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 44a1ab280..5f55cc21e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -60,11 +60,6 @@ parameters: count: 1 path: src/Components/ArrayObj.php - - - message: "#^Binary operation \"\\.\" between ' AS ' and array\\\\|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 @@ -90,11 +85,6 @@ parameters: count: 1 path: src/Components/Condition.php - - - message: "#^Binary operation \"\\.\" between array\\\\|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 @@ -125,16 +115,6 @@ parameters: count: 1 path: src/Components/DataType.php - - - message: "#^Binary operation \"\\.\" between ' AS ' and array\\\\|string results in an error\\.$#" - count: 1 - path: src/Components/Expression.php - - - - message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\\\|string given\\.$#" - count: 1 - path: src/Components/Expression.php - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\:\\:\\$alias \\(string\\|null\\) does not accept mixed\\.$#" count: 3 @@ -250,16 +230,6 @@ parameters: count: 1 path: src/Components/JoinKeyword.php - - - message: "#^Binary operation \"\\.\" between array\\\\|string and ' ' results in an error\\.$#" - count: 1 - path: src/Components/Key.php - - - - message: "#^Binary operation \"\\.\\=\" between '' and array\\\\|string results in an error\\.$#" - count: 1 - path: src/Components/Key.php - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$columns \\(array\\\\) does not accept non\\-empty\\-array\\\\>\\.$#" count: 1 @@ -430,11 +400,6 @@ parameters: count: 1 path: src/Components/Reference.php - - - message: "#^Parameter \\#2 \\$array of function implode expects array\\|null, array\\\\|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 @@ -480,11 +445,6 @@ parameters: count: 1 path: src/Components/UnionKeyword.php - - - message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) should return array\\\\|string but returns array\\\\|string\\>\\.$#" - count: 1 - path: src/Context.php - - message: "#^Static property PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:\\$keywords \\(non\\-empty\\-array\\\\) does not accept default value of type array\\{\\}\\.$#" count: 1 @@ -690,21 +650,6 @@ parameters: count: 1 path: src/Statements/DeleteStatement.php - - - message: "#^Binary operation \"\\.\" between ' ' and array\\\\|string results in an error\\.$#" - count: 1 - path: src/Statements/ExplainStatement.php - - - - message: "#^Binary operation \"\\.\" between array\\\\|string and '\\.' results in an error\\.$#" - count: 1 - path: src/Statements/ExplainStatement.php - - - - message: "#^Binary operation \"\\.\\=\" between string and array\\\\|string results in an error\\.$#" - count: 1 - path: src/Statements/ExplainStatement.php - - message: "#^Cannot access property \\$keyword on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8eafc263c..48da65600 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -126,9 +126,6 @@ $component - - alias)]]> - @@ -164,9 +161,6 @@ $component - - name)]]> - options]]> @@ -210,12 +204,6 @@ $component - - Context::escape($fields) - - - alias)]]> - expr]]> expr]]> @@ -366,10 +354,6 @@ $component - - - name)]]> - options]]> options]]> @@ -528,9 +512,6 @@ $component - - name)]]> - $inOut $name @@ -586,9 +567,6 @@ $component - - columns)]]> - values]]> @@ -667,12 +645,6 @@ $context::$keywords - - $str - - - string|string[] - self::$keywords @@ -1118,12 +1090,6 @@ explainedDatabase]]> explainedTable]]> - - explainedColumn)]]> - explainedDatabase)]]> - explainedTable)]]> - explainedTable)]]> - keyword]]> value]]> diff --git a/src/Components/Expression.php b/src/Components/Expression.php index 8858a1e71..1c9387022 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -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)) { diff --git a/src/Components/Reference.php b/src/Components/Reference.php index 7b62a35f1..d64d1b541 100644 --- a/src/Components/Reference.php +++ b/src/Components/Reference.php @@ -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 ); } diff --git a/src/Context.php b/src/Context.php index 0b5c05d54..42c393244 100644 --- a/src/Context.php +++ b/src/Context.php @@ -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; @@ -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; } @@ -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) * diff --git a/tests/Lexer/ContextTest.php b/tests/Lexer/ContextTest.php index 5f8c69f09..76e79a364 100644 --- a/tests/Lexer/ContextTest.php +++ b/tests/Lexer/ContextTest.php @@ -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'])); } }