Skip to content

Commit

Permalink
Refactor Quoter::quoteValue() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Nov 20, 2024
1 parent e3fad7b commit ad98738
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function buildCommentString(): string
return '';
}

return ' COMMENT ' . (string) (new Quoter('`', '`'))->quoteValue($this->getComment());
return ' COMMENT ' . (new Quoter('`', '`'))->quoteValue($this->getComment());

Check failure on line 51 in src/Column.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

PossiblyNullArgument

src/Column.php:51:65: PossiblyNullArgument: Argument 1 of Yiisoft\Db\Mysql\Quoter::quoteValue cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 51 in src/Column.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

PossiblyNullArgument

src/Column.php:51:65: PossiblyNullArgument: Argument 1 of Yiisoft\Db\Mysql\Quoter::quoteValue cannot be null, possibly null value provided (see https://psalm.dev/078)
}

public function asString(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function buildComment(ColumnSchemaInterface $column): string
{
$comment = $column->getComment();

return $comment === null ? '' : ' COMMENT ' . (string) $this->queryBuilder->quoter()->quoteValue($comment);
return $comment === null ? '' : ' COMMENT ' . $this->queryBuilder->quoter()->quoteValue($comment);
}

protected function getDbType(ColumnSchemaInterface $column): string
Expand Down
4 changes: 2 additions & 2 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function addCommentOnColumn(string $table, string $column, string $commen
. $this->quoter->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ' COMMENT '
. (string) $this->quoter->quoteValue($comment);
. $this->quoter->quoteValue($comment);

if ($check === 1) {
$alterSql .= ' ' . $checkMatches[0];
Expand All @@ -70,7 +70,7 @@ public function addCommentOnTable(string $table, string $comment): string
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' COMMENT '
. (string) $this->quoter->quoteValue($comment);
. $this->quoter->quoteValue($comment);
}

public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
Expand Down
23 changes: 11 additions & 12 deletions src/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@

use Yiisoft\Db\Schema\Quoter as BaseQuoter;

use function is_string;
use function str_replace;
use function strtr;

/**
* Implements MySQL, MariaDB quoting and unquoting methods.
*/
final class Quoter extends BaseQuoter
{
public function quoteValue(mixed $value): mixed
public function quoteValue(string $value): string
{
if (!is_string($value)) {
return $value;
}

return "'" . str_replace(
['\\', "\x00", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z'],
$value
) . "'";
return "'" . strtr($value, [
'\\' => '\\\\',
"\x00" => '\\0',
"\n" => '\\n',
"\r" => '\\r',
"'" => "\'",
'"' => '\"',
"\x1a" => '\\Z',
]) . "'";
}
}
5 changes: 0 additions & 5 deletions tests/QuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ public function testQuoteValue(): void

$quoter = $db->getQuoter();

$this->assertFalse($quoter->quoteValue(false));
$this->assertTrue($quoter->quoteValue(true));
$this->assertNull($quoter->quoteValue(null));
$this->assertSame("'1.1'", $quoter->quoteValue('1.1'));
$this->assertSame("'1.1e0'", $quoter->quoteValue('1.1e0'));
$this->assertSame("'test'", $quoter->quoteValue('test'));
$this->assertSame("'test\'test'", $quoter->quoteValue("test'test"));
$this->assertSame(1, $quoter->quoteValue(1));
$this->assertSame(1.1, $quoter->quoteValue(1.1));
}
}

0 comments on commit ad98738

Please sign in to comment.