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
Fix wrong type assertion and annotation #3543
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,11 @@ | |
use InvalidArgumentException; | ||
use IteratorAggregate; | ||
use PDO; | ||
use PDOStatement; | ||
use function array_merge; | ||
use function array_values; | ||
use function assert; | ||
use function is_array; | ||
use function reset; | ||
|
||
/** | ||
|
@@ -42,7 +44,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement | |
/** @var int */ | ||
private $lifetime; | ||
|
||
/** @var ResultStatement */ | ||
/** @var ResultStatement|PDOStatement */ | ||
private $statement; | ||
|
||
/** | ||
|
@@ -59,11 +61,12 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement | |
private $defaultFetchMode = FetchMode::MIXED; | ||
|
||
/** | ||
* @param string $cacheKey | ||
* @param string $realKey | ||
* @param int $lifetime | ||
* @param ResultStatement|PDOStatement $stmt | ||
* @param string $cacheKey | ||
* @param string $realKey | ||
* @param int $lifetime | ||
*/ | ||
public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime) | ||
public function __construct($stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even when passing the PDO instance we modify the statement class, so I'd say we shouldn't remove this type hint here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan was complaining about it, that's why I had to modify it. |
||
{ | ||
$this->statement = $stmt; | ||
$this->resultCache = $resultCache; | ||
|
@@ -167,7 +170,10 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX | |
*/ | ||
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) | ||
{ | ||
$this->data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); | ||
$data = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs); | ||
assert(is_array($data)); | ||
|
||
$this->data = $data; | ||
$this->emptied = true; | ||
|
||
return $this->data; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,14 @@ | |
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\Types\Type; | ||
use Exception; | ||
use PDO; | ||
use PDOStatement; | ||
use Throwable; | ||
use function array_key_exists; | ||
use function assert; | ||
use function func_get_args; | ||
use function implode; | ||
use function is_array; | ||
use function is_int; | ||
use function is_string; | ||
use function key; | ||
|
@@ -82,7 +85,7 @@ class Connection implements DriverConnection | |
/** | ||
* The wrapped driver connection. | ||
* | ||
* @var \Doctrine\DBAL\Driver\Connection|null | ||
* @var \Doctrine\DBAL\Driver\Connection|PDO|null | ||
*/ | ||
protected $_conn; | ||
|
||
|
@@ -831,7 +834,10 @@ public function quote($input, $type = null) | |
*/ | ||
public function fetchAll($sql, array $params = [], $types = []) | ||
{ | ||
return $this->executeQuery($sql, $params, $types)->fetchAll(); | ||
$data = $this->executeQuery($sql, $params, $types)->fetchAll(); | ||
assert(is_array($data)); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
|
@@ -867,7 +873,7 @@ public function prepare($statement) | |
* @param int[]|string[] $types The types the previous parameters are in. | ||
* @param QueryCacheProfile|null $qcp The query cache profile, optional. | ||
* | ||
* @return ResultStatement The executed statement. | ||
* @return ResultStatement|PDOStatement The executed statement. | ||
* | ||
* @throws DBALException | ||
*/ | ||
|
@@ -902,6 +908,8 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach | |
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); | ||
} | ||
|
||
assert($stmt instanceof ResultStatement || $stmt instanceof PDOStatement); | ||
|
||
$stmt->setFetchMode($this->defaultFetchMode); | ||
|
||
if ($logger) { | ||
|
@@ -986,7 +994,7 @@ public function project($query, array $params, Closure $function) | |
/** | ||
* Executes an SQL statement, returning a result set as a Statement object. | ||
* | ||
* @return \Doctrine\DBAL\Driver\Statement | ||
* @return DriverStatement|PDOStatement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Interface containing |
||
* | ||
* @throws DBALException | ||
*/ | ||
|
@@ -1007,6 +1015,7 @@ public function query() | |
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]); | ||
} | ||
|
||
assert($statement instanceof PDOStatement || $statement instanceof DriverStatement); | ||
$statement->setFetchMode($this->defaultFetchMode); | ||
|
||
if ($logger) { | ||
|
@@ -1424,12 +1433,12 @@ public function rollbackSavepoint($savepoint) | |
/** | ||
* Gets the wrapped driver connection. | ||
* | ||
* @return DriverConnection | ||
* @return DriverConnection|PDO | ||
*/ | ||
public function getWrappedConnection() | ||
{ | ||
$this->connect(); | ||
assert($this->_conn instanceof DriverConnection); | ||
assert($this->_conn instanceof DriverConnection || $this->_conn instanceof PDO); | ||
|
||
return $this->_conn; | ||
} | ||
|
@@ -1516,9 +1525,9 @@ public function convertToPHPValue($value, $type) | |
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as | ||
* raw PDOStatement instances. | ||
* | ||
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to. | ||
* @param mixed[] $params The map/list of named/positional parameters. | ||
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types). | ||
* @param DriverStatement|PDOStatement $stmt The statement to bind the values to. | ||
* @param mixed[] $params The map/list of named/positional parameters. | ||
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types). | ||
* | ||
* @return void | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,8 +13,10 @@ | |||||
use function array_map; | ||||||
use function array_reverse; | ||||||
use function array_values; | ||||||
use function assert; | ||||||
use function explode; | ||||||
use function file_exists; | ||||||
use function is_array; | ||||||
use function preg_match; | ||||||
use function preg_match_all; | ||||||
use function preg_quote; | ||||||
|
@@ -173,6 +175,7 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null | |||||
$this->_conn->quote($tableName) | ||||||
)); | ||||||
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE); | ||||||
assert(is_array($indexArray)); | ||||||
|
||||||
usort($indexArray, static function ($a, $b) { | ||||||
if ($a['pk'] === $b['pk']) { | ||||||
|
@@ -207,11 +210,12 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null | |||||
$idx['primary'] = false; | ||||||
$idx['non_unique'] = ! $tableIndex['unique']; | ||||||
|
||||||
$stmt = $this->_conn->executeQuery(sprintf( | ||||||
'PRAGMA INDEX_INFO (%s)', | ||||||
$this->_conn->quote($keyName) | ||||||
)); | ||||||
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE); | ||||||
$stmt = $this->_conn->executeQuery(sprintf( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Because of the newline above this, I assume that the spaces aren't needed here. |
||||||
'PRAGMA INDEX_INFO (%s)', | ||||||
$this->_conn->quote($keyName) | ||||||
)); | ||||||
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE); | ||||||
assert(is_array($indexArray)); | ||||||
|
||||||
foreach ($indexArray as $indexColumnRow) { | ||||||
$idx['column_name'] = $indexColumnRow['name']; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,11 @@ public function testInvalidPdoInstance() | |
*/ | ||
public function testValidPdoInstance() | ||
{ | ||
$conn = DriverManager::getConnection([ | ||
'pdo' => new PDO('sqlite::memory:'), | ||
]); | ||
$pdo = new PDO('sqlite::memory:'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we need more tests covering the basic DBAL features for when an instance is used? |
||
$conn = DriverManager::getConnection(['pdo' => $pdo]); | ||
|
||
self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName()); | ||
self::assertSame($pdo, $conn->getWrappedConnection()); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to add an
assert
statement to the constructor to check for this, but apparently PHPStan thinks it's redundant. The code in question would beDo we want to add it and silence the error or leave it as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed and the type hint added back, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree here, it looks like we're missing an assert on the caller and not here.