Skip to content

Commit

Permalink
PHPStan Level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Feb 3, 2019
1 parent 30b9231 commit 7bafa27
Show file tree
Hide file tree
Showing 43 changed files with 241 additions and 250 deletions.
92 changes: 44 additions & 48 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,11 @@ private function getDatabasePlatformVersion()
*/
private function getServerVersion()
{
$connection = $this->getWrappedConnection();

// Automatic platform version detection.
if ($this->_conn instanceof ServerInfoAwareConnection &&
! $this->_conn->requiresQueryForServerVersion()
) {
return $this->_conn->getServerVersion();
if ($connection instanceof ServerInfoAwareConnection && ! $connection->requiresQueryForServerVersion()) {
return $connection->getServerVersion();
}

// Unable to detect platform version.
Expand Down Expand Up @@ -810,20 +810,15 @@ public function quoteIdentifier($str)
}

/**
* Quotes a given input parameter.
*
* @param mixed $input The parameter to be quoted.
* @param int|null $type The type of the parameter.
*
* @return string The quoted parameter.
* {@inheritDoc}
*/
public function quote($input, $type = null)
{
$this->connect();
$connection = $this->getWrappedConnection();

[$value, $bindingType] = $this->getBindingInfo($input, $type);

return $this->_conn->quote($value, $bindingType);
return $connection->quote($value, $bindingType);
}

/**
Expand Down Expand Up @@ -883,7 +878,7 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
return $this->executeCacheQuery($query, $params, $types, $qcp);
}

$this->connect();
$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();
if ($logger) {
Expand All @@ -894,15 +889,15 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
if ($params) {
[$query, $params, $types] = SQLParserUtils::expandListParameters($query, $params, $types);

$stmt = $this->_conn->prepare($query);
$stmt = $connection->prepare($query);
if ($types) {
$this->_bindTypedValues($stmt, $params, $types);
$stmt->execute();
} else {
$stmt->execute($params);
}
} else {
$stmt = $this->_conn->query($query);
$stmt = $connection->query($query);
}
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
Expand Down Expand Up @@ -931,8 +926,9 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
{
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
if (! $resultCache) {
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();

if ($resultCache === null) {
throw CacheException::noResultDriverConfigured();
}

Expand Down Expand Up @@ -994,7 +990,7 @@ public function project($query, array $params, Closure $function)
*/
public function query()
{
$this->connect();
$connection = $this->getWrappedConnection();

$args = func_get_args();

Expand All @@ -1004,7 +1000,7 @@ public function query()
}

try {
$statement = $this->_conn->query(...$args);
$statement = $connection->query(...$args);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
}
Expand Down Expand Up @@ -1034,7 +1030,7 @@ public function query()
*/
public function executeUpdate($query, array $params = [], array $types = [])
{
$this->connect();
$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();
if ($logger) {
Expand All @@ -1045,7 +1041,8 @@ public function executeUpdate($query, array $params = [], array $types = [])
if ($params) {
[$query, $params, $types] = SQLParserUtils::expandListParameters($query, $params, $types);

$stmt = $this->_conn->prepare($query);
$stmt = $connection->prepare($query);

if ($types) {
$this->_bindTypedValues($stmt, $params, $types);
$stmt->execute();
Expand All @@ -1054,7 +1051,7 @@ public function executeUpdate($query, array $params = [], array $types = [])
}
$result = $stmt->rowCount();
} else {
$result = $this->_conn->exec($query);
$result = $connection->exec($query);
}
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
Expand All @@ -1078,15 +1075,15 @@ public function executeUpdate($query, array $params = [], array $types = [])
*/
public function exec($statement)
{
$this->connect();
$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($statement);
}

try {
$result = $this->_conn->exec($statement);
$result = $connection->exec($statement);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}
Expand Down Expand Up @@ -1115,19 +1112,15 @@ public function getTransactionNestingLevel()
*/
public function errorCode()
{
$this->connect();

return $this->_conn->errorCode();
return $this->getWrappedConnection()->errorCode();
}

/**
* {@inheritDoc}
*/
public function errorInfo()
{
$this->connect();

return $this->_conn->errorInfo();
return $this->getWrappedConnection()->errorInfo();
}

/**
Expand All @@ -1144,9 +1137,7 @@ public function errorInfo()
*/
public function lastInsertId($seqName = null)
{
$this->connect();

return $this->_conn->lastInsertId($seqName);
return $this->getWrappedConnection()->lastInsertId($seqName);
}

/**
Expand Down Expand Up @@ -1228,7 +1219,7 @@ protected function _getNestedTransactionSavePointName()
*/
public function beginTransaction()
{
$this->connect();
$connection = $this->getWrappedConnection();

++$this->transactionNestingLevel;

Expand All @@ -1238,7 +1229,9 @@ public function beginTransaction()
if ($logger) {
$logger->startQuery('"START TRANSACTION"');
}
$this->_conn->beginTransaction();

$connection->beginTransaction();

if ($logger) {
$logger->stopQuery();
}
Expand Down Expand Up @@ -1270,15 +1263,17 @@ public function commit()
throw ConnectionException::commitFailedRollbackOnly();
}

$this->connect();
$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();

if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"COMMIT"');
}
$this->_conn->commit();

$connection->commit();

if ($logger) {
$logger->stopQuery();
}
Expand Down Expand Up @@ -1332,7 +1327,7 @@ public function rollBack()
throw ConnectionException::noActiveTransaction();
}

$this->connect();
$connection = $this->getWrappedConnection();

$logger = $this->_config->getSQLLogger();

Expand All @@ -1341,7 +1336,7 @@ public function rollBack()
$logger->startQuery('"ROLLBACK"');
}
$this->transactionNestingLevel = 0;
$this->_conn->rollBack();
$connection->rollBack();
$this->isRollbackOnly = false;
if ($logger) {
$logger->stopQuery();
Expand Down Expand Up @@ -1380,7 +1375,7 @@ public function createSavepoint($savepoint)
throw ConnectionException::savepointsNotSupported();
}

$this->_conn->exec($this->platform->createSavePoint($savepoint));
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
}

/**
Expand All @@ -1402,7 +1397,7 @@ public function releaseSavepoint($savepoint)
return;
}

$this->_conn->exec($this->platform->releaseSavePoint($savepoint));
$this->getWrappedConnection()->exec($this->platform->releaseSavePoint($savepoint));
}

/**
Expand All @@ -1420,17 +1415,18 @@ public function rollbackSavepoint($savepoint)
throw ConnectionException::savepointsNotSupported();
}

$this->_conn->exec($this->platform->rollbackSavePoint($savepoint));
$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
}

/**
* Gets the wrapped driver connection.
*
* @return \Doctrine\DBAL\Driver\Connection
* @return DriverConnection
*/
public function getWrappedConnection()
{
$this->connect();
assert($this->_conn instanceof DriverConnection);

return $this->_conn;
}
Expand Down Expand Up @@ -1558,8 +1554,8 @@ private function _bindTypedValues($stmt, array $params, array $types)
/**
* Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type.
*
* @param mixed $value The value to bind.
* @param int|string $type The type to bind (PDO or DBAL).
* @param mixed $value The value to bind.
* @param int|string|null $type The type to bind (PDO or DBAL).
*
* @return mixed[] [0] => the (escaped) value, [1] => the binding type.
*/
Expand Down Expand Up @@ -1658,10 +1654,10 @@ public function createQueryBuilder()
*/
public function ping()
{
$this->connect();
$connection = $this->getWrappedConnection();

if ($this->_conn instanceof PingableConnection) {
return $this->_conn->ping();
if ($connection instanceof PingableConnection) {
return $connection->ping();
}

try {
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Events;
use InvalidArgumentException;
use function array_rand;
use function assert;
use function count;
use function func_get_args;

Expand Down Expand Up @@ -344,6 +345,7 @@ public function rollbackSavepoint($savepoint)
public function query()
{
$this->connect('master');
assert($this->_conn instanceof DriverConnection);

$args = func_get_args();

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/DBALException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use function is_resource;
use function is_string;
use function json_encode;
use function preg_replace;
use function sprintf;
use function str_split;

class DBALException extends Exception
{
Expand Down Expand Up @@ -186,7 +186,7 @@ private static function formatParameters(array $params)

if (! is_string($json) || $json === 'null' && is_string($param)) {
// JSON encoding failed, this is not a UTF-8 string.
return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
return sprintf('"%s"', preg_replace('/.{2}/', '\\x$0', bin2hex($param)));
}

return $json;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function connect(array $params, $username = null, $password = null, array
$password = null;
}

return new DB2Connection($params, $username, $password, $driverOptions);
return new DB2Connection($params, (string) $username, (string) $password, $driverOptions);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Driver extends AbstractMySQLDriver
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
try {
return new MysqliConnection($params, $username, $password, $driverOptions);
return new MysqliConnection($params, (string) $username, (string) $password, $driverOptions);
} catch (MysqliException $e) {
throw DBALException::driverException($this, $e);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class MysqliStatement implements IteratorAggregate, Statement
/** @var string[]|false|null */
protected $_columnNames;

/** @var mixed[]|null */
protected $_rowBindedValues;
/** @var mixed[] */
protected $_rowBindedValues = [];

/** @var mixed[] */
protected $_bindedValues;
Expand Down Expand Up @@ -318,6 +318,7 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
}

$values = $this->_fetch();

if ($values === null) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/DBAL/Driver/OCI8/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function connect(array $params, $username = null, $password = null, array
{
try {
return new OCI8Connection(
$username,
$password,
(string) $username,
(string) $password,
$this->_constructDsn($params),
$params['charset'] ?? null,
$params['charset'] ?? '',
$params['sessionMode'] ?? OCI_DEFAULT,
$params['persistent'] ?? false
);
Expand Down
Loading

0 comments on commit 7bafa27

Please sign in to comment.