From 3e142e41b739ae1305a39a462af3d1dd6181d8d0 Mon Sep 17 00:00:00 2001 From: "Jonathan H. Wage" Date: Tue, 4 Jun 2019 11:25:24 -0500 Subject: [PATCH] Make classes final where applicable. --- UPGRADE.md | 33 +++++ lib/Doctrine/DBAL/Cache/ArrayStatement.php | 2 +- .../DBAL/Cache/ResultCacheStatement.php | 2 +- .../DBAL/Driver/IBMDB2/DB2Connection.php | 4 +- lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php | 2 +- .../DBAL/Driver/IBMDB2/DB2Statement.php | 32 ++--- .../DBAL/Driver/Mysqli/MysqliStatement.php | 116 +++++++++--------- lib/Doctrine/DBAL/Driver/OCI8/Driver.php | 6 +- lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php | 4 +- lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php | 2 +- lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php | 4 +- lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php | 12 +- lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php | 6 +- .../DBAL/Driver/PDOSqlsrv/Statement.php | 2 +- .../DBAL/Driver/SQLAnywhere/Driver.php | 2 +- .../SQLAnywhere/SQLAnywhereConnection.php | 2 +- .../SQLAnywhere/SQLAnywhereStatement.php | 2 +- lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php | 2 +- .../DBAL/Driver/SQLSrv/SQLSrvConnection.php | 6 +- .../DBAL/Driver/SQLSrv/SQLSrvStatement.php | 96 +++++++-------- .../DBAL/Driver/StatementIterator.php | 2 +- .../DBAL/Id/TableGeneratorSchemaVisitor.php | 2 +- lib/Doctrine/DBAL/Portability/Statement.php | 58 ++++----- .../SingleDatabaseSynchronizer.php | 2 +- .../Schema/Visitor/RemoveNamespacedAssets.php | 2 +- .../DBAL/Sharding/PoolingShardManager.php | 2 +- .../SQLAzureFederationsSynchronizer.php | 3 +- .../SQLAzure/Schema/MultiTenantVisitor.php | 2 +- .../ShardChoser/MultiTenantShardChoser.php | 2 +- .../DBAL/Driver/StatementIteratorTest.php | 57 --------- 30 files changed, 222 insertions(+), 247 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 39c710b36a5..812dfd0b5f2 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -4,6 +4,39 @@ Table columns are no longer indexed by column name. Use the `name` attribute of the column instead. +## BC BREAK: Classes made final + +- Class constant `SQLSrvStatement::LAST_INSERT_ID_SQL` was changed from public to private. +- Class `Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser` was made final. +- Class `Doctrine\DBAL\Sharding\SQLAzure\Schema\MultiTenantVisitor` was made final. +- Class `Doctrine\DBAL\Sharding\SQLAzure\SQLAzureFederationsSynchronizer` was made final. +- Class `Doctrine\DBAL\Sharding\PoolingShardManager` was made final. +- Class `Doctrine\DBAL\Id\TableGeneratorSchemaVisitor` was made final. +- Class `Doctrine\DBAL\Driver\OCI8\Driver` was made final. +- Class `Doctrine\DBAL\Driver\Mysqli\Driver` was made final. +- Class `Doctrine\DBAL\Driver\Mysqli\MysqliStatement` was made final. +- Class `Doctrine\DBAL\Driver\PDOSqlsrv\Driver` was made final. +- Class `Doctrine\DBAL\Driver\PDOSqlsrv\Statement` was made final. +- Class `Doctrine\DBAL\Driver\PDOMySql\Driver` was made final. +- Class `Doctrine\DBAL\Driver\IBMDB2\DB2Connection` was made final. +- Class `Doctrine\DBAL\Driver\IBMDB2\DB2Statement` was made final. +- Class `Doctrine\DBAL\Driver\IBMDB2\DB2Driver` was made final. +- Class `Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement` was made final. +- Class `Doctrine\DBAL\Driver\SQLSrv\Driver` was made final. +- Class `Doctrine\DBAL\Driver\SQLSrv\SQLSrvConnection` was made final. +- Class `Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereConnection` was made final. +- Class `Doctrine\DBAL\Driver\SQLAnywhere\Driver` was made final. +- Class `Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement` was made final. +- Class `Doctrine\DBAL\Driver\PDOPgSql\Driver` was made final. +- Class `Doctrine\DBAL\Driver\PDOOracle\Driver` was made final. +- Class `Doctrine\DBAL\Driver\PDOSqlite\Driver` was made final. +- Class `Doctrine\DBAL\Driver\StatementIterator` was made final. +- Class `Doctrine\DBAL\Cache\ResultCacheStatement` was made final. +- Class `Doctrine\DBAL\Cache\ArrayStatement` was made final. +- Class `Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer` was made final. +- Class `Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets` was made final. +- Class `Doctrine\DBAL\Portability\Statement` was made final. + ## BC BREAK: Changes in the `Doctrine\DBAL\Schema` API - Column precision no longer defaults to 10. The default value is NULL. diff --git a/lib/Doctrine/DBAL/Cache/ArrayStatement.php b/lib/Doctrine/DBAL/Cache/ArrayStatement.php index 257f3a1ebf8..e7414382814 100644 --- a/lib/Doctrine/DBAL/Cache/ArrayStatement.php +++ b/lib/Doctrine/DBAL/Cache/ArrayStatement.php @@ -17,7 +17,7 @@ use function reset; use function sprintf; -class ArrayStatement implements IteratorAggregate, ResultStatement +final class ArrayStatement implements IteratorAggregate, ResultStatement { /** @var mixed[] */ private $data; diff --git a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php index 4eb342022ce..6afac1b7d9e 100644 --- a/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php +++ b/lib/Doctrine/DBAL/Cache/ResultCacheStatement.php @@ -30,7 +30,7 @@ * Also you have to realize that the cache will load the whole result into memory at once to ensure 2. * This means that the memory usage for cached results might increase by using this feature. */ -class ResultCacheStatement implements IteratorAggregate, ResultStatement +final class ResultCacheStatement implements IteratorAggregate, ResultStatement { /** @var Cache */ private $resultCache; diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php index 3439e1b96e5..48d726a6852 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php @@ -23,10 +23,10 @@ use function db2_rollback; use function db2_server_info; -class DB2Connection implements Connection, ServerInfoAwareConnection +final class DB2Connection implements Connection, ServerInfoAwareConnection { /** @var resource */ - private $conn = null; + private $conn; /** * @param array $params diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php index 604a46ad1e9..bcadcc405da 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php @@ -14,7 +14,7 @@ /** * IBM DB2 Driver. */ -class DB2Driver extends AbstractDB2Driver +final class DB2Driver extends AbstractDB2Driver { /** * {@inheritdoc} diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 154b9763ab6..9d7d5029233 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -48,7 +48,7 @@ use function strtolower; use function tmpfile; -class DB2Statement implements IteratorAggregate, Statement +final class DB2Statement implements IteratorAggregate, Statement { /** @var resource */ private $stmt; @@ -128,21 +128,6 @@ public function bindParam($param, &$variable, int $type = ParameterType::STRING, } } - /** - * @param int $position Parameter position - * @param mixed $variable - * - * @throws DB2Exception - */ - private function bind(int $position, &$variable, int $parameterType, int $dataType) : void - { - $this->bindParam[$position] =& $variable; - - if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) { - throw DB2Exception::fromStatementError($this->stmt); - } - } - /** * {@inheritdoc} */ @@ -336,6 +321,21 @@ public function rowCount() : int return @db2_num_rows($this->stmt) ? : 0; } + /** + * @param int $position Parameter position + * @param mixed $variable + * + * @throws DB2Exception + */ + private function bind(int $position, &$variable, int $parameterType, int $dataType) : void + { + $this->bindParam[$position] =& $variable; + + if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) { + throw DB2Exception::fromStatementError($this->stmt); + } + } + /** * Casts a stdClass object to the given class name mapping its' properties. * diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index 544c6102a6d..cd3d1acfdcc 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -32,10 +32,10 @@ use function is_resource; use function str_repeat; -class MysqliStatement implements IteratorAggregate, Statement +final class MysqliStatement implements IteratorAggregate, Statement { /** @var string[] */ - protected static $_paramTypeMap = [ + private static $paramTypeMap = [ ParameterType::STRING => 's', ParameterType::BINARY => 's', ParameterType::BOOLEAN => 'i', @@ -45,32 +45,32 @@ class MysqliStatement implements IteratorAggregate, Statement ]; /** @var mysqli */ - protected $_conn; + private $conn; /** @var mysqli_stmt */ - protected $_stmt; + private $stmt; /** @var string[]|false|null */ - protected $_columnNames; + private $columnNames; /** @var mixed[] */ - protected $_rowBindedValues = []; + private $rowBindedValues = []; /** @var mixed[] */ - protected $_bindedValues = []; + private $bindedValues = []; /** @var string */ - protected $types; + private $types; /** * Contains ref values for bindValue(). * * @var mixed[] */ - protected $_values = []; + private $values = []; /** @var int */ - protected $_defaultFetchMode = FetchMode::MIXED; + private $defaultFetchMode = FetchMode::MIXED; /** * Indicates whether the statement is in the state when fetching results is possible @@ -84,23 +84,23 @@ class MysqliStatement implements IteratorAggregate, Statement */ public function __construct(mysqli $conn, string $sql) { - $this->_conn = $conn; + $this->conn = $conn; $stmt = $conn->prepare($sql); if ($stmt === false) { - throw ConnectionError::new($this->_conn); + throw ConnectionError::new($this->conn); } - $this->_stmt = $stmt; + $this->stmt = $stmt; - $paramCount = $this->_stmt->param_count; + $paramCount = $this->stmt->param_count; if (0 >= $paramCount) { return; } - $this->types = str_repeat('s', $paramCount); - $this->_bindedValues = array_fill(1, $paramCount, null); + $this->types = str_repeat('s', $paramCount); + $this->bindedValues = array_fill(1, $paramCount, null); } /** @@ -110,12 +110,12 @@ public function bindParam($param, &$variable, int $type = ParameterType::STRING, { assert(is_int($param)); - if (! isset(self::$_paramTypeMap[$type])) { + if (! isset(self::$paramTypeMap[$type])) { throw UnknownType::new($type); } - $this->_bindedValues[$param] =& $variable; - $this->types[$param - 1] = self::$_paramTypeMap[$type]; + $this->bindedValues[$param] =& $variable; + $this->types[$param - 1] = self::$paramTypeMap[$type]; } /** @@ -125,13 +125,13 @@ public function bindValue($param, $value, int $type = ParameterType::STRING) : v { assert(is_int($param)); - if (! isset(self::$_paramTypeMap[$type])) { + if (! isset(self::$paramTypeMap[$type])) { throw UnknownType::new($type); } - $this->_values[$param] = $value; - $this->_bindedValues[$param] =& $this->_values[$param]; - $this->types[$param - 1] = self::$_paramTypeMap[$type]; + $this->values[$param] = $value; + $this->bindedValues[$param] =& $this->values[$param]; + $this->types[$param - 1] = self::$paramTypeMap[$type]; } /** @@ -141,18 +141,18 @@ public function execute(?array $params = null) : void { if ($params !== null && count($params) > 0) { if (! $this->bindUntypedValues($params)) { - throw StatementError::new($this->_stmt); + throw StatementError::new($this->stmt); } } else { $this->bindTypedParameters(); } - if (! $this->_stmt->execute()) { - throw StatementError::new($this->_stmt); + if (! $this->stmt->execute()) { + throw StatementError::new($this->stmt); } - if ($this->_columnNames === null) { - $meta = $this->_stmt->result_metadata(); + if ($this->columnNames === null) { + $meta = $this->stmt->result_metadata(); if ($meta !== false) { $fields = $meta->fetch_fields(); assert(is_array($fields)); @@ -164,17 +164,17 @@ public function execute(?array $params = null) : void $meta->free(); - $this->_columnNames = $columnNames; + $this->columnNames = $columnNames; } else { - $this->_columnNames = false; + $this->columnNames = false; } } - if ($this->_columnNames !== false) { + if ($this->columnNames !== false) { // Store result of every execution which has it. Otherwise it will be impossible // to execute a new statement in case if the previous one has non-fetched rows // @link http://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html - $this->_stmt->store_result(); + $this->stmt->store_result(); // Bind row values _after_ storing the result. Otherwise, if mysqli is compiled with libmysql, // it will have to allocate as much memory as it may be needed for the given column type @@ -187,15 +187,15 @@ public function execute(?array $params = null) : void // It's also important that row values are bound after _each_ call to store_result(). Otherwise, // if mysqli is compiled with libmysql, subsequently fetched string values will get truncated // to the length of the ones fetched during the previous execution. - $this->_rowBindedValues = array_fill(0, count($this->_columnNames), null); + $this->rowBindedValues = array_fill(0, count($this->columnNames), null); $refs = []; - foreach ($this->_rowBindedValues as $key => &$value) { + foreach ($this->rowBindedValues as $key => &$value) { $refs[$key] =& $value; } - if (! $this->_stmt->bind_result(...$refs)) { - throw StatementError::new($this->_stmt); + if (! $this->stmt->bind_result(...$refs)) { + throw StatementError::new($this->stmt); } } @@ -212,12 +212,12 @@ private function bindTypedParameters() : void $streams = $values = []; $types = $this->types; - foreach ($this->_bindedValues as $parameter => $value) { + foreach ($this->bindedValues as $parameter => $value) { if (! isset($types[$parameter - 1])) { - $types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; + $types[$parameter - 1] = static::$paramTypeMap[ParameterType::STRING]; } - if ($types[$parameter - 1] === static::$_paramTypeMap[ParameterType::LARGE_OBJECT]) { + if ($types[$parameter - 1] === static::$paramTypeMap[ParameterType::LARGE_OBJECT]) { if (is_resource($value)) { if (get_resource_type($value) !== 'stream') { throw new InvalidArgumentException('Resources passed with the LARGE_OBJECT parameter type must be stream resources.'); @@ -227,14 +227,14 @@ private function bindTypedParameters() : void continue; } - $types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; + $types[$parameter - 1] = static::$paramTypeMap[ParameterType::STRING]; } $values[$parameter] = $value; } - if (count($values) > 0 && ! $this->_stmt->bind_param($types, ...$values)) { - throw StatementError::new($this->_stmt); + if (count($values) > 0 && ! $this->stmt->bind_param($types, ...$values)) { + throw StatementError::new($this->stmt); } $this->sendLongData($streams); @@ -257,8 +257,8 @@ private function sendLongData(array $streams) : void throw FailedReadingStreamOffset::new($paramNr); } - if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { - throw StatementError::new($this->_stmt); + if (! $this->stmt->send_long_data($paramNr - 1, $chunk)) { + throw StatementError::new($this->stmt); } } } @@ -278,7 +278,7 @@ private function bindUntypedValues(array $values) : bool $params[] =& $v; } - return $this->_stmt->bind_param($types, ...$params); + return $this->stmt->bind_param($types, ...$params); } /** @@ -286,11 +286,11 @@ private function bindUntypedValues(array $values) : bool */ private function _fetch() { - $ret = $this->_stmt->fetch(); + $ret = $this->stmt->fetch(); if ($ret === true) { $values = []; - foreach ($this->_rowBindedValues as $v) { + foreach ($this->rowBindedValues as $v) { $values[] = $v; } @@ -311,7 +311,7 @@ public function fetch(?int $fetchMode = null, ...$args) return false; } - $fetchMode = $fetchMode ?: $this->_defaultFetchMode; + $fetchMode = $fetchMode ?: $this->defaultFetchMode; if ($fetchMode === FetchMode::COLUMN) { return $this->fetchColumn(); @@ -324,15 +324,15 @@ public function fetch(?int $fetchMode = null, ...$args) } if ($values === false) { - throw StatementError::new($this->_stmt); + throw StatementError::new($this->stmt); } if ($fetchMode === FetchMode::NUMERIC) { return $values; } - assert(is_array($this->_columnNames)); - $assoc = array_combine($this->_columnNames, $values); + assert(is_array($this->columnNames)); + $assoc = array_combine($this->columnNames, $values); assert(is_array($assoc)); switch ($fetchMode) { @@ -355,7 +355,7 @@ public function fetch(?int $fetchMode = null, ...$args) */ public function fetchAll(?int $fetchMode = null, ...$args) : array { - $fetchMode = $fetchMode ?: $this->_defaultFetchMode; + $fetchMode = $fetchMode ?: $this->defaultFetchMode; $rows = []; @@ -395,7 +395,7 @@ public function fetchColumn(int $columnIndex = 0) */ public function closeCursor() : void { - $this->_stmt->free_result(); + $this->stmt->free_result(); $this->result = false; } @@ -404,11 +404,11 @@ public function closeCursor() : void */ public function rowCount() : int { - if ($this->_columnNames === false) { - return $this->_stmt->affected_rows; + if ($this->columnNames === false) { + return $this->stmt->affected_rows; } - return $this->_stmt->num_rows; + return $this->stmt->num_rows; } /** @@ -416,7 +416,7 @@ public function rowCount() : int */ public function columnCount() : int { - return $this->_stmt->field_count; + return $this->stmt->field_count; } /** @@ -424,7 +424,7 @@ public function columnCount() : int */ public function setFetchMode(int $fetchMode, ...$args) : void { - $this->_defaultFetchMode = $fetchMode; + $this->defaultFetchMode = $fetchMode; } /** diff --git a/lib/Doctrine/DBAL/Driver/OCI8/Driver.php b/lib/Doctrine/DBAL/Driver/OCI8/Driver.php index bf392885e70..546057e10de 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/Driver.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/Driver.php @@ -12,7 +12,7 @@ /** * A Doctrine DBAL driver for the Oracle OCI8 PHP extensions. */ -class Driver extends AbstractOracleDriver +final class Driver extends AbstractOracleDriver { /** * {@inheritdoc} @@ -27,7 +27,7 @@ public function connect( return new OCI8Connection( $username, $password, - $this->_constructDsn($params), + $this->constructDsn($params), $params['charset'] ?? '', $params['sessionMode'] ?? OCI_DEFAULT, $params['persistent'] ?? false @@ -44,7 +44,7 @@ public function connect( * * @return string The DSN. */ - protected function _constructDsn(array $params) : string + private function constructDsn(array $params) : string { return $this->getEasyConnectString($params); } diff --git a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php index 73ef818b143..260e90ba7dd 100644 --- a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php @@ -14,7 +14,7 @@ /** * PDO MySql driver. */ -class Driver extends AbstractMySQLDriver +final class Driver extends AbstractMySQLDriver { /** * {@inheritdoc} @@ -50,7 +50,7 @@ public function connect( * * @return string The DSN. */ - protected function constructPdoDsn(array $params) : string + private function constructPdoDsn(array $params) : string { $dsn = 'mysql:'; if (isset($params['host']) && $params['host'] !== '') { diff --git a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php index 6dfcdb6b428..d20df171bb3 100644 --- a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php @@ -19,7 +19,7 @@ * which leads us to the recommendation to use the "oci8" driver to connect * to Oracle instead. */ -class Driver extends AbstractOracleDriver +final class Driver extends AbstractOracleDriver { /** * {@inheritdoc} diff --git a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php index 800be0adb19..0068d290a34 100644 --- a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php @@ -32,7 +32,7 @@ public function connect( try { $connection = new PDOConnection( - $this->_constructPdoDsn($params), + $this->constructPdoDsn($params), $username, $password, $driverOptions @@ -67,7 +67,7 @@ public function connect( * * @return string The DSN. */ - private function _constructPdoDsn(array $params) : string + private function constructPdoDsn(array $params) : string { $dsn = 'pgsql:'; diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php index 89598f0826c..e8de9a3948b 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php @@ -18,7 +18,7 @@ class Driver extends AbstractSQLiteDriver { /** @var mixed[] */ - protected $_userDefinedFunctions = [ + private $userDefinedFunctions = [ 'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1], 'mod' => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2], 'locate' => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1], @@ -34,8 +34,8 @@ public function connect( array $driverOptions = [] ) : Connection { if (isset($driverOptions['userDefinedFunctions'])) { - $this->_userDefinedFunctions = array_merge( - $this->_userDefinedFunctions, + $this->userDefinedFunctions = array_merge( + $this->userDefinedFunctions, $driverOptions['userDefinedFunctions'] ); unset($driverOptions['userDefinedFunctions']); @@ -43,7 +43,7 @@ public function connect( try { $connection = new PDOConnection( - $this->_constructPdoDsn($params), + $this->constructPdoDsn($params), $username, $password, $driverOptions @@ -54,7 +54,7 @@ public function connect( $pdo = $connection->getWrappedConnection(); - foreach ($this->_userDefinedFunctions as $fn => $data) { + foreach ($this->userDefinedFunctions as $fn => $data) { $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); } @@ -68,7 +68,7 @@ public function connect( * * @return string The DSN. */ - protected function _constructPdoDsn(array $params) : string + private function constructPdoDsn(array $params) : string { $dsn = 'sqlite:'; if (isset($params['path'])) { diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php index 4096d5127b6..8eede1bcef3 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php @@ -13,7 +13,7 @@ /** * The PDO-based Sqlsrv driver. */ -class Driver extends AbstractSQLServerDriver +final class Driver extends AbstractSQLServerDriver { /** * {@inheritdoc} @@ -39,7 +39,7 @@ public function connect( } return new Connection( - $this->_constructPdoDsn($params, $dsnOptions), + $this->constructPdoDsn($params, $dsnOptions), $username, $password, $pdoOptions @@ -54,7 +54,7 @@ public function connect( * * @return string The DSN. */ - private function _constructPdoDsn(array $params, array $connectionOptions) : string + private function constructPdoDsn(array $params, array $connectionOptions) : string { $dsn = 'sqlsrv:server='; diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php index ba5d3dc87d8..f4c9a8540ea 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Statement.php @@ -11,7 +11,7 @@ /** * PDO SQL Server Statement */ -class Statement extends PDOStatement +final class Statement extends PDOStatement { /** * {@inheritdoc} diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/Driver.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/Driver.php index 9264e50a39d..4facfcd90f9 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/Driver.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/Driver.php @@ -16,7 +16,7 @@ /** * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension. */ -class Driver extends AbstractSQLAnywhereDriver +final class Driver extends AbstractSQLAnywhereDriver { /** * {@inheritdoc} diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php index e815f760bda..f4f93156d61 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php @@ -24,7 +24,7 @@ /** * SAP Sybase SQL Anywhere implementation of the Connection interface. */ -class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection +final class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection { /** @var resource The SQL Anywhere connection resource. */ private $connection; diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php index 02c4217c9b4..a9066f4eb0c 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereStatement.php @@ -40,7 +40,7 @@ /** * SAP SQL Anywhere implementation of the Statement interface. */ -class SQLAnywhereStatement implements IteratorAggregate, Statement +final class SQLAnywhereStatement implements IteratorAggregate, Statement { /** @var resource The connection resource. */ private $conn; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php b/lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php index c786b996c58..97b7d3cc53c 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php @@ -10,7 +10,7 @@ /** * Driver for ext/sqlsrv. */ -class Driver extends AbstractSQLServerDriver +final class Driver extends AbstractSQLServerDriver { /** * {@inheritdoc} diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php index a6b09b02818..00682c6bbb9 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php @@ -21,13 +21,13 @@ /** * SQL Server implementation for the Connection interface. */ -class SQLSrvConnection implements Connection, ServerInfoAwareConnection +final class SQLSrvConnection implements Connection, ServerInfoAwareConnection { /** @var resource */ - protected $conn; + private $conn; /** @var LastInsertId */ - protected $lastInsertId; + private $lastInsertId; /** * @param array $connectionOptions diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php index 3c33a094352..9d70c716e9c 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvStatement.php @@ -37,7 +37,7 @@ /** * SQL Server Statement. */ -class SQLSrvStatement implements IteratorAggregate, Statement +final class SQLSrvStatement implements IteratorAggregate, Statement { /** * The SQLSRV Resource. @@ -122,10 +122,8 @@ class SQLSrvStatement implements IteratorAggregate, Statement /** * Append to any INSERT query to retrieve the last insert id. - * - * @deprecated This constant has been deprecated and will be made private in 3.0 */ - public const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;'; + private const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;'; /** * @param resource $conn @@ -234,51 +232,6 @@ public function execute(?array $params = null) : void $this->result = true; } - /** - * Prepares SQL Server statement resource - * - * @return resource - * - * @throws SQLSrvException - */ - private function prepare() - { - $params = []; - - foreach ($this->variables as $column => &$variable) { - switch ($this->types[$column]) { - case ParameterType::LARGE_OBJECT: - $params[$column - 1] = [ - &$variable, - SQLSRV_PARAM_IN, - SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), - SQLSRV_SQLTYPE_VARBINARY('max'), - ]; - break; - - case ParameterType::BINARY: - $params[$column - 1] = [ - &$variable, - SQLSRV_PARAM_IN, - SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY), - ]; - break; - - default: - $params[$column - 1] =& $variable; - break; - } - } - - $stmt = sqlsrv_prepare($this->conn, $this->sql, $params); - - if (! $stmt) { - throw SQLSrvException::fromSqlSrvErrors(); - } - - return $stmt; - } - /** * {@inheritdoc} */ @@ -401,4 +354,49 @@ public function rowCount() : int return sqlsrv_rows_affected($this->stmt) ?: 0; } + + /** + * Prepares SQL Server statement resource + * + * @return resource + * + * @throws SQLSrvException + */ + private function prepare() + { + $params = []; + + foreach ($this->variables as $column => &$variable) { + switch ($this->types[$column]) { + case ParameterType::LARGE_OBJECT: + $params[$column - 1] = [ + &$variable, + SQLSRV_PARAM_IN, + SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), + SQLSRV_SQLTYPE_VARBINARY('max'), + ]; + break; + + case ParameterType::BINARY: + $params[$column - 1] = [ + &$variable, + SQLSRV_PARAM_IN, + SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY), + ]; + break; + + default: + $params[$column - 1] =& $variable; + break; + } + } + + $stmt = sqlsrv_prepare($this->conn, $this->sql, $params); + + if (! $stmt) { + throw SQLSrvException::fromSqlSrvErrors(); + } + + return $stmt; + } } diff --git a/lib/Doctrine/DBAL/Driver/StatementIterator.php b/lib/Doctrine/DBAL/Driver/StatementIterator.php index 98d3f08b3a9..53811f6bab4 100644 --- a/lib/Doctrine/DBAL/Driver/StatementIterator.php +++ b/lib/Doctrine/DBAL/Driver/StatementIterator.php @@ -6,7 +6,7 @@ use IteratorAggregate; -class StatementIterator implements IteratorAggregate +final class StatementIterator implements IteratorAggregate { /** @var Statement */ private $statement; diff --git a/lib/Doctrine/DBAL/Id/TableGeneratorSchemaVisitor.php b/lib/Doctrine/DBAL/Id/TableGeneratorSchemaVisitor.php index 54d26f9445b..2268ab929f1 100644 --- a/lib/Doctrine/DBAL/Id/TableGeneratorSchemaVisitor.php +++ b/lib/Doctrine/DBAL/Id/TableGeneratorSchemaVisitor.php @@ -12,7 +12,7 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Visitor\Visitor; -class TableGeneratorSchemaVisitor implements Visitor +final class TableGeneratorSchemaVisitor implements Visitor { /** @var string */ private $generatorTableName; diff --git a/lib/Doctrine/DBAL/Portability/Statement.php b/lib/Doctrine/DBAL/Portability/Statement.php index 9df3169ed84..0be7d48453c 100644 --- a/lib/Doctrine/DBAL/Portability/Statement.php +++ b/lib/Doctrine/DBAL/Portability/Statement.php @@ -18,7 +18,7 @@ /** * Portability wrapper for a Statement. */ -class Statement implements IteratorAggregate, DriverStatement +final class Statement implements IteratorAggregate, DriverStatement { /** @var int */ private $portability; @@ -164,34 +164,6 @@ public function fetchAll(?int $fetchMode = null, ...$args) : array return $rows; } - /** - * @param mixed $row - * - * @return mixed - */ - protected function fixRow($row, bool $iterateRow, bool $fixCase) - { - if (! $row) { - return $row; - } - - if ($fixCase) { - $row = array_change_key_case($row, $this->case); - } - - if ($iterateRow) { - foreach ($row as $k => $v) { - if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { - $row[$k] = null; - } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { - $row[$k] = rtrim($v); - } - } - } - - return $row; - } - /** * {@inheritdoc} */ @@ -219,4 +191,32 @@ public function rowCount() : int return $this->stmt->rowCount(); } + + /** + * @param mixed $row + * + * @return mixed + */ + private function fixRow($row, bool $iterateRow, bool $fixCase) + { + if (! $row) { + return $row; + } + + if ($fixCase) { + $row = array_change_key_case($row, $this->case); + } + + if ($iterateRow) { + foreach ($row as $k => $v) { + if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { + $row[$k] = null; + } elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { + $row[$k] = rtrim($v); + } + } + } + + return $row; + } } diff --git a/lib/Doctrine/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizer.php b/lib/Doctrine/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizer.php index 605add99cc6..f694992d654 100644 --- a/lib/Doctrine/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizer.php +++ b/lib/Doctrine/DBAL/Schema/Synchronizer/SingleDatabaseSynchronizer.php @@ -14,7 +14,7 @@ /** * Schema Synchronizer for Default DBAL Connection. */ -class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer +final class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer { /** @var AbstractPlatform */ private $platform; diff --git a/lib/Doctrine/DBAL/Schema/Visitor/RemoveNamespacedAssets.php b/lib/Doctrine/DBAL/Schema/Visitor/RemoveNamespacedAssets.php index 78991298872..8945a62038c 100644 --- a/lib/Doctrine/DBAL/Schema/Visitor/RemoveNamespacedAssets.php +++ b/lib/Doctrine/DBAL/Schema/Visitor/RemoveNamespacedAssets.php @@ -20,7 +20,7 @@ * This visitor filters all these non-default namespaced tables and sequences * and removes them from the SChema instance. */ -class RemoveNamespacedAssets extends AbstractVisitor +final class RemoveNamespacedAssets extends AbstractVisitor { /** @var Schema */ private $schema; diff --git a/lib/Doctrine/DBAL/Sharding/PoolingShardManager.php b/lib/Doctrine/DBAL/Sharding/PoolingShardManager.php index 5a21944d303..c02c90bb721 100644 --- a/lib/Doctrine/DBAL/Sharding/PoolingShardManager.php +++ b/lib/Doctrine/DBAL/Sharding/PoolingShardManager.php @@ -10,7 +10,7 @@ /** * Shard Manager for the Connection Pooling Shard Strategy */ -class PoolingShardManager implements ShardManager +final class PoolingShardManager implements ShardManager { /** @var PoolingShardConnection */ private $conn; diff --git a/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureFederationsSynchronizer.php b/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureFederationsSynchronizer.php index 343b941a6ec..e22626f2fbb 100644 --- a/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureFederationsSynchronizer.php +++ b/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureFederationsSynchronizer.php @@ -23,8 +23,9 @@ * global database and then applying the operations step by step using the * {@see \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer}. */ -class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer +final class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer { + // can these by private? public const FEDERATION_TABLE_FEDERATED = 'azure.federated'; public const FEDERATION_DISTRIBUTION_NAME = 'azure.federatedOnDistributionName'; diff --git a/lib/Doctrine/DBAL/Sharding/SQLAzure/Schema/MultiTenantVisitor.php b/lib/Doctrine/DBAL/Sharding/SQLAzure/Schema/MultiTenantVisitor.php index a14f26c2418..cb27c412321 100644 --- a/lib/Doctrine/DBAL/Sharding/SQLAzure/Schema/MultiTenantVisitor.php +++ b/lib/Doctrine/DBAL/Sharding/SQLAzure/Schema/MultiTenantVisitor.php @@ -34,7 +34,7 @@ * SQLAzure throws errors when you try to create IDENTIY columns on federated * tables. */ -class MultiTenantVisitor implements Visitor +final class MultiTenantVisitor implements Visitor { /** @var string[] */ private $excludedTables = []; diff --git a/lib/Doctrine/DBAL/Sharding/ShardChoser/MultiTenantShardChoser.php b/lib/Doctrine/DBAL/Sharding/ShardChoser/MultiTenantShardChoser.php index c6c2f3843f4..620de4f9154 100644 --- a/lib/Doctrine/DBAL/Sharding/ShardChoser/MultiTenantShardChoser.php +++ b/lib/Doctrine/DBAL/Sharding/ShardChoser/MultiTenantShardChoser.php @@ -10,7 +10,7 @@ * The MultiTenant Shard choser assumes that the distribution value directly * maps to the shard id. */ -class MultiTenantShardChoser implements ShardChoser +final class MultiTenantShardChoser implements ShardChoser { /** * {@inheritdoc} diff --git a/tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php b/tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php index 63cc84c41d3..0fd99201910 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/StatementIteratorTest.php @@ -4,36 +4,14 @@ namespace Doctrine\Tests\DBAL\Driver; -use Doctrine\DBAL\Driver\IBMDB2\DB2Statement; -use Doctrine\DBAL\Driver\Mysqli\MysqliStatement; -use Doctrine\DBAL\Driver\OCI8\OCI8Statement; -use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement; -use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement; use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\StatementIterator; -use Doctrine\DBAL\Portability\Statement as PortabilityStatement; use Doctrine\Tests\DbalTestCase; -use IteratorAggregate; use PHPUnit\Framework\MockObject\MockObject; use Traversable; -use function extension_loaded; class StatementIteratorTest extends DbalTestCase { - /** - * @dataProvider statementProvider() - */ - public function testGettingIteratorDoesNotCallFetch(string $class) : void - { - /** @var IteratorAggregate|MockObject $stmt */ - $stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']); - $stmt->expects($this->never())->method('fetch'); - $stmt->expects($this->never())->method('fetchAll'); - $stmt->expects($this->never())->method('fetchColumn'); - - $stmt->getIterator(); - } - public function testIteratorIterationCallsFetchOncePerStep() : void { $stmt = $this->createMock(Statement::class); @@ -46,18 +24,6 @@ public function testIteratorIterationCallsFetchOncePerStep() : void $this->assertIterationCallsFetchOncePerStep($stmtIterator, $calls); } - /** - * @dataProvider statementProvider() - */ - public function testStatementIterationCallsFetchOncePerStep(string $class) : void - { - $stmt = $this->createPartialMock($class, ['fetch']); - - $calls = 0; - $this->configureStatement($stmt, $calls); - $this->assertIterationCallsFetchOncePerStep($stmt, $calls); - } - private function configureStatement(MockObject $stmt, int &$calls) : void { $values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false, 'impossible']; @@ -79,27 +45,4 @@ private function assertIterationCallsFetchOncePerStep(Traversable $iterator, int $this->assertEquals($i + 1, $calls); } } - - /** - * @return string[][] - */ - public static function statementProvider() : iterable - { - if (extension_loaded('ibm_db2')) { - yield [DB2Statement::class]; - } - - yield [MysqliStatement::class]; - - if (extension_loaded('oci8')) { - yield [OCI8Statement::class]; - } - - yield [PortabilityStatement::class]; - yield [SQLAnywhereStatement::class]; - - if (extension_loaded('sqlsrv')) { - yield [SQLSrvStatement::class]; - } - } }