Skip to content
Open
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
14 changes: 1 addition & 13 deletions src/SqlCommenter/src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,10 @@ public static function formatComments(array $comments): string
return '/*' . implode(
',',
array_map(
static fn (string $value, string $key) => Utils::customUrlEncode($key) . "='" . Utils::customUrlEncode($value) . "'",
static fn (string $value, string $key) => urlencode($key) . "='" . urlencode($value) . "'",
$comments,
array_keys($comments)
),
) . '*/';
}

private static function customUrlEncode(string $input): string
{
$encodedString = urlencode($input);

// Since SQL uses '%' as a keyword, '%' is a by-product of url quoting
// e.g. foo,bar --> foo%2Cbar
// thus in our quoting, we need to escape it too to finally give
// foo,bar --> foo%%2Cbar

return str_replace('%', '%%', $encodedString);
}
}
6 changes: 3 additions & 3 deletions src/SqlCommenter/tests/Unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public function testFormatCommentsWithoutKeys(): void

public function testFormatCommentsWithSpecialCharKeys(): void
{
$this->assertEquals("/*key1='value1%%40',key2='value2'*/", Utils::formatComments(['key1' => 'value1@', 'key2' => 'value2']));
$this->assertEquals("/*key1='value1%40',key2='value2'*/", Utils::formatComments(['key1' => 'value1@', 'key2' => 'value2']));
}

public function testFormatCommentsWithPlaceholder(): void
{
$this->assertEquals("/*key1='value1%%3F',key2='value2'*/", Utils::formatComments(['key1' => 'value1?', 'key2' => 'value2']));
$this->assertEquals("/*key1='value1%3F',key2='value2'*/", Utils::formatComments(['key1' => 'value1?', 'key2' => 'value2']));
}

public function testFormatCommentsWithNamedPlaceholder(): void
{
$this->assertEquals("/*key1='%%3Anamed',key2='value2'*/", Utils::formatComments(['key1' => ':named', 'key2' => 'value2']));
$this->assertEquals("/*key1='%3Anamed',key2='value2'*/", Utils::formatComments(['key1' => ':named', 'key2' => 'value2']));
}
}
Loading