Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforced parameter and return value types in Driver classes #3560

Merged
merged 1 commit into from
May 25, 2019
Merged
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 3.0

## BC BREAK: Changes in the `Doctrine\DBAL\Driver` API

1. The `$username` and `$password` arguments of `::connect()` are no longer nullable. Use an empty string to indicate empty username or password.
2. The return value of `::getDatabase()` has been documented as nullable since some of the drivers allow establishing a connection without selecting a database.

## BC BREAK: `Doctrine\DBAL\Driver::getName()` removed

The `Doctrine\DBAL\Driver::getName()` has been removed.
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ public function connect() : void
}

$driverOptions = $this->params['driverOptions'] ?? [];
$user = $this->params['user'] ?? null;
$password = $this->params['password'] ?? null;
$user = $this->params['user'] ?? '';
$password = $this->params['password'] ?? '';

$this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions);
$this->isConnected = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ protected function connectTo($connectionName)

$connectionParams = $this->chooseConnectionConfiguration($connectionName, $params);

$user = $connectionParams['user'] ?? null;
$password = $connectionParams['password'] ?? null;
$user = $connectionParams['user'] ?? '';
$password = $connectionParams['password'] ?? '';

return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
}
Expand Down
28 changes: 16 additions & 12 deletions lib/Doctrine/DBAL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\DBAL;

use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;

Expand All @@ -16,35 +17,38 @@ interface Driver
/**
* Attempts to create a connection with the database.
*
* @param mixed[] $params All connection parameters passed by the user.
* @param string|null $username The username to use when connecting.
* @param string|null $password The password to use when connecting.
* @param mixed[] $driverOptions The driver options to use when connecting.
* @param mixed[] $params All connection parameters passed by the user.
* @param string $username The username to use when connecting.
* @param string $password The password to use when connecting.
* @param mixed[] $driverOptions The driver options to use when connecting.
*
* @return \Doctrine\DBAL\Driver\Connection The database connection.
* @return DriverConnection The database connection.
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = []);
public function connect(
array $params,
string $username = '',
string $password = '',
array $driverOptions = []
) : DriverConnection;

/**
* Gets the DatabasePlatform instance that provides all the metadata about
* the platform this driver connects to.
*
* @return AbstractPlatform The database platform.
*/
public function getDatabasePlatform();
public function getDatabasePlatform() : AbstractPlatform;

/**
* Gets the SchemaManager that can be used to inspect and change the underlying
* database schema of the platform this driver connects to.
*
* @return AbstractSchemaManager
*/
public function getSchemaManager(Connection $conn);
public function getSchemaManager(Connection $conn) : AbstractSchemaManager;

/**
* Gets the name of the database connected to for this driver.
*
* @return string The name of the database.
* @return string The name of the database or NULL if no database is currently selected.
*/
public function getDatabase(Connection $conn);
public function getDatabase(Connection $conn) : ?string;
}
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/Driver/AbstractDB2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\DB2SchemaManager;

/**
Expand All @@ -17,7 +19,7 @@ abstract class AbstractDB2Driver implements Driver
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn)
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();

Expand All @@ -27,15 +29,15 @@ public function getDatabase(Connection $conn)
/**
* {@inheritdoc}
*/
public function getDatabasePlatform()
public function getDatabasePlatform() : AbstractPlatform
{
return new DB2Platform();
}

/**
* {@inheritdoc}
*/
public function getSchemaManager(Connection $conn)
public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{
return new DB2SchemaManager($conn);
}
Expand Down
16 changes: 10 additions & 6 deletions lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match;
Expand All @@ -30,7 +34,7 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver,
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
* @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
*/
public function convertException($message, DriverException $exception)
public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{
switch ($exception->getCode()) {
case 1213:
Expand Down Expand Up @@ -106,15 +110,15 @@ public function convertException($message, DriverException $exception)
return new Exception\NotNullConstraintViolationException($message, $exception);
}

return new Exception\DriverException($message, $exception);
return new DriverException($message, $exception);
}

/**
* {@inheritdoc}
*
* @throws DBALException
*/
public function createDatabasePlatformForVersion($version)
public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{
$mariadb = stripos($version, 'mariadb') !== false;
if ($mariadb && version_compare($this->getMariaDbMysqlVersionNumber($version), '10.2.7', '>=')) {
Expand Down Expand Up @@ -192,7 +196,7 @@ private function getMariaDbMysqlVersionNumber(string $versionString) : string
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn)
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();

Expand All @@ -204,7 +208,7 @@ public function getDatabase(Connection $conn)
*
* @return MySqlPlatform
*/
public function getDatabasePlatform()
public function getDatabasePlatform() : AbstractPlatform
{
return new MySqlPlatform();
}
Expand All @@ -214,7 +218,7 @@ public function getDatabasePlatform()
*
* @return MySqlSchemaManager
*/
public function getSchemaManager(Connection $conn)
public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{
return new MySqlSchemaManager($conn);
}
Expand Down
18 changes: 10 additions & 8 deletions lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractOracleDriver\EasyConnectString;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\OracleSchemaManager;

/**
Expand All @@ -19,7 +23,7 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
/**
* {@inheritdoc}
*/
public function convertException($message, DriverException $exception)
public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{
switch ($exception->getCode()) {
case 1:
Expand Down Expand Up @@ -56,13 +60,13 @@ public function convertException($message, DriverException $exception)
return new Exception\ForeignKeyConstraintViolationException($message, $exception);
}

return new Exception\DriverException($message, $exception);
return new DriverException($message, $exception);
}

/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn)
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();

Expand All @@ -72,15 +76,15 @@ public function getDatabase(Connection $conn)
/**
* {@inheritdoc}
*/
public function getDatabasePlatform()
public function getDatabasePlatform() : AbstractPlatform
{
return new OraclePlatform();
}

/**
* {@inheritdoc}
*/
public function getSchemaManager(Connection $conn)
public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{
return new OracleSchemaManager($conn);
}
Expand All @@ -89,10 +93,8 @@ public function getSchemaManager(Connection $conn)
* Returns an appropriate Easy Connect String for the given parameters.
*
* @param mixed[] $params The connection parameters to return the Easy Connect String for.
*
* @return string
*/
protected function getEasyConnectString(array $params)
protected function getEasyConnectString(array $params) : string
{
return (string) EasyConnectString::fromConnectionParameters($params);
}
Expand Down
16 changes: 10 additions & 6 deletions lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
use Doctrine\DBAL\VersionAwarePlatformDriver;
use function preg_match;
Expand All @@ -27,7 +31,7 @@ abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDri
*
* @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
*/
public function convertException($message, DriverException $exception)
public function convertException(string $message, DriverExceptionInterface $exception) : DriverException
{
switch ($exception->getSQLState()) {
case '40001':
Expand Down Expand Up @@ -73,13 +77,13 @@ public function convertException($message, DriverException $exception)
return new Exception\ConnectionException($message, $exception);
}

return new Exception\DriverException($message, $exception);
return new DriverException($message, $exception);
}

/**
* {@inheritdoc}
*/
public function createDatabasePlatformForVersion($version)
public function createDatabasePlatformForVersion(string $version) : AbstractPlatform
{
if (! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
throw InvalidPlatformVersion::new(
Expand All @@ -106,7 +110,7 @@ public function createDatabasePlatformForVersion($version)
/**
* {@inheritdoc}
*/
public function getDatabase(Connection $conn)
public function getDatabase(Connection $conn) : ?string
{
$params = $conn->getParams();

Expand All @@ -116,15 +120,15 @@ public function getDatabase(Connection $conn)
/**
* {@inheritdoc}
*/
public function getDatabasePlatform()
public function getDatabasePlatform() : AbstractPlatform
{
return new PostgreSqlPlatform();
}

/**
* {@inheritdoc}
*/
public function getSchemaManager(Connection $conn)
public function getSchemaManager(Connection $conn) : AbstractSchemaManager
{
return new PostgreSqlSchemaManager($conn);
}
Expand Down
Loading