Skip to content

Commit

Permalink
Fix phpdoc, raise code coverage 100%. (#429)
Browse files Browse the repository at this point in the history
* Fix phpdocs Quoter::class and QuoterInterface::class.
* Raise code coverage Quoter::class 100%.
  • Loading branch information
terabytesoftw committed Dec 21, 2022
1 parent be68b3c commit 32a4e47
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/Schema/Quoter.php
Expand Up @@ -17,6 +17,14 @@
use function strrpos;
use function substr;

/**
* The Quoter is a class that is used to quote table and column names for use in SQL statements. It provides a set of
* methods for quoting different types of names, such as table names, column names, and schema names.
*
* The Quoter class is used by @see \Yiisoft\Db\QueryBuilder\QueryBuilder to quote names that need to be quoted. It is
* also used by @see \Yiisoft\Db\Command\Command to quote names in SQL statements before passing them to database
* servers.
*/
class Quoter implements QuoterInterface
{
public function __construct(
Expand All @@ -31,6 +39,7 @@ public function __construct(
public function getTableNameParts(string $name): array
{
$parts = array_slice(explode('.', $name), -2, 2);

return array_map(fn ($part) => $this->unquoteSimpleTableName($part), $parts);
}

Expand All @@ -51,6 +60,7 @@ public function ensureColumnName(string $name): string
$parts = explode('.', $name);
$name = $parts[count($parts) - 1];
}

return preg_replace('|^\[\[([_\w\-. ]+)\]\]$|', '\1', $name);
}

Expand Down
54 changes: 29 additions & 25 deletions src/Schema/QuoterInterface.php
Expand Up @@ -4,32 +4,36 @@

namespace Yiisoft\Db\Schema;

/**
* The QuoterInterface class is an interface that provides a set of methods that can be used to quote table and column
* names, values, and other SQL expressions independently of the database.
*/
interface QuoterInterface
{
/**
* Splits full table name into parts
* Splits full table name into parts.
*
* @param string $name
* @param string $name The full name of the table.
*
* @return string[]
* @return string[] The table name parts.
*/
public function getTableNameParts(string $name): array;

/**
* Ensures name is wrapped with {{ and }}.
*
* @param string $name
* @param string $name The name to be quoted.
*
* @return string
* @return string The quoted name.
*/
public function ensureNameQuoted(string $name): string;

/**
* Ensures name of column is wrapped with [[ and ]].
*
* @param string $name
* @param string $name The name to be quoted.
*
* @return string
* @return string The quoted name.
*/
public function ensureColumnName(string $name): string;

Expand All @@ -39,11 +43,11 @@ public function ensureColumnName(string $name): string;
* If the column name contains prefix, the prefix will also be properly quoted. If the column name is already quoted
* or contains '(', '[[' or '{{', then this method will do nothing.
*
* @param string $name column name.
* @param string $name The column name to be quoted.
*
* @return string the properly quoted column name.
* @return string The properly quoted column name.
*
* {@see quoteSimpleColumnName()}
* @see quoteSimpleColumnName()
*/
public function quoteColumnName(string $name): string;

Expand All @@ -53,9 +57,9 @@ public function quoteColumnName(string $name): string;
* A simple column name should contain the column name only without any prefix. If the column name is already quoted
* or is the asterisk character '*', this method will do nothing.
*
* @param string $name column name.
* @param string $name The column name to be quoted.
*
* @return string the properly quoted column name.
* @return string The properly quoted column name.
*/
public function quoteSimpleColumnName(string $name): string;

Expand All @@ -65,9 +69,9 @@ public function quoteSimpleColumnName(string $name): string;
* A simple table name should contain the table name only without any schema prefix. If the table name is already
* quoted, this method will do nothing.
*
* @param string $name table name.
* @param string $name The table name to be quoted.
*
* @return string the properly quoted table name.
* @return string The properly quoted table name.
*/
public function quoteSimpleTableName(string $name): string;

Expand All @@ -78,9 +82,9 @@ public function quoteSimpleTableName(string $name): string;
* square brackets are column names. They will be quoted accordingly. Also, the percentage character "%" at the
* beginning or ending of a table name will be replaced with {@see tablePrefix}.
*
* @param string $sql the SQL to be quoted
* @param string $sql The SQL statement to be quoted.
*
* @return string the quoted SQL
* @return string The quoted SQL statement.
*/
public function quoteSql(string $sql): string;

Expand All @@ -90,11 +94,11 @@ public function quoteSql(string $sql): string;
* If the table name contains schema prefix, the prefix will also be properly quoted. If the table name is already
* quoted or contains '(' or '{{', then this method will do nothing.
*
* @param string $name table name.
* @param string $name The table name to be quoted.
*
* @return string the properly quoted table name.
* @return string The properly quoted table name.
*
* {@see quoteSimpleTableName()}
* @see quoteSimpleTableName()
*/
public function quoteTableName(string $name): string;

Expand All @@ -103,9 +107,9 @@ public function quoteTableName(string $name): string;
*
* Note that if the parameter is not a string, it will be returned without change.
*
* @param mixed $value
* @param mixed $value The value to be quoted.
*
* @return mixed The properly quoted string.
* @return mixed The properly quoted value.
*/
public function quoteValue(mixed $value): mixed;

Expand All @@ -115,9 +119,9 @@ public function quoteValue(mixed $value): mixed;
* A simple column name should contain the column name only without any prefix. If the column name is not quoted or
* is the asterisk character '*', this method will do nothing.
*
* @param string $name column name.
* @param string $name The column name to be unquoted.
*
* @return string unquoted column name.
* @return string The unquoted column name.
*/
public function unquoteSimpleColumnName(string $name): string;

Expand All @@ -127,9 +131,9 @@ public function unquoteSimpleColumnName(string $name): string;
* A simple table name should contain the table name only without any schema prefix. If the table name is not
* quoted, this method will do nothing.
*
* @param string $name table name.
* @param string $name The table name to be unquoted.
*
* @return string unquoted table name.
* @return string The unquoted table name.
*/
public function unquoteSimpleTableName(string $name): string;
}
1 change: 1 addition & 0 deletions tests/Provider/AbstractQuoterProvider.php
Expand Up @@ -69,6 +69,7 @@ public function simpleTableNames(): array
{
return [
['test', 'test'],
['(test)', '(test)'],
];
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Schema/QuoterTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests\Schema;

use Yiisoft\Db\Schema\Quoter;
use Yiisoft\Db\Tests\AbstractQuoterTest;
use Yiisoft\Db\Tests\Support\TestTrait;

Expand All @@ -15,4 +16,42 @@
final class QuoterTest extends AbstractQuoterTest
{
use TestTrait;

public function testQuoteSimpleColumnNameWithStartingCharacterEndingCharacterEquals(): void
{
$quoter = new Quoter('`', '`');

$this->assertSame('`column`', $quoter->quoteSimpleColumnName('column'));
}

public function testQuoteSimpleTableNameWithStartingCharacterEndingCharacterEquals(): void
{
$quoter = new Quoter('`', '`');

$this->assertSame('`table`', $quoter->quoteSimpleTableName('table'));
}

public function testQuoteTableNameWithSchema(): void
{
$quoter = new Quoter('`', '`');

$this->assertSame('`schema`.`table`', $quoter->quoteTableName('schema.table'));
}

public function testQuoteValueNotString(): void
{
$quoter = new Quoter('`', '`');

$this->assertFalse($quoter->quoteValue(false));
$this->assertTrue($quoter->quoteValue(true));
$this->assertSame(1, $quoter->quoteValue(1));
$this->assertSame([], $quoter->quoteValue([]));
}

public function testUnquoteSimpleColumnNameWithStartingCharacterEndingCharacterEquals(): void
{
$quoter = new Quoter('`', '`');

$this->assertSame('column', $quoter->unquoteSimpleColumnName('`column`'));
}
}

0 comments on commit 32a4e47

Please sign in to comment.