Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In Drupal\Core\Database\Driver\sqlite\Statement::getStatement
we have the following:
protected function getStatement($query, &$args = []) {
if (count($args)) {
// Check if $args is a simple numeric array.
if (range(0, count($args) - 1) === array_keys($args)) {
under PHP 7.2, if for any reason the $args is NULL or not an array, this will lead to notices and/or test failures, like
1) Drupal\KernelTests\Core\Database\DatabaseExceptionWrapperTest::testPreparedStatement
Thrown exception is not a PDOException:
count(): Parameter must be an array or an object that implements Countable
/home/travis/drupal8/vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php:262
/home/travis/drupal8/core/lib/Drupal/Core/Database/Driver/sqlite/Statement.php:30
/home/travis/drupal8/core/lib/Drupal/Core/Database/StatementPrefetch.php:158
/home/travis/drupal8/core/lib/Drupal/Core/Database/Driver/sqlite/Statement.php:90
/home/travis/drupal8/core/lib/Drupal/Core/Database/Connection.php:612
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php:28
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php:39
Note: There is no SQLite testing on DrupalCI with PHP 7.2, yet - it's likely that this will appear once there'll be. The error above is result from a TravisCI test.
Comments
Comment #2
mondrakeI think it's just safer to replace the
count()
with a more appropriate check that $args is a non-empty array.Comment #3
mondrakeComment #4
alexpottWhy would args not be countable? Ie when is this not going to be an array? And is the simpler thing just to type hint to an array?
Comment #6
borisson_I think typehinting to an array makes more sense.
Comment #7
borisson_The array typehint is needed, because further down in the code we're doing array operations, so typehinting to
\Countable
is not sufficient. Implemented that.No interdiff as this is another approach.
Comment #8
daffie CreditAttribution: daffie commentedThe problem with adding a type hint to the parameter is that if goes wrong a type error exception will be thrown.
At the moment only PDOException or DatabaseExceptionWrapper exceptions are allowed.
Any other exception is considered wrong. See the patch from comment #6 with the SQLite testbot result.
The correct patch for me is the one from comment #2. That one is for me RTBC.
Comment #9
alexpottI think we can do something way better here :) patch incoming.
Comment #10
alexpottI thought we might be able to get rid of the SQLite statement fudge but we can't - it seems https://bugs.php.net/bug.php?id=45259 is still a bug. :(
Comment #11
alexpottCommitted and pushed 68cd2e5528 to 8.6.x and 93b2f9e806 to 8.5.x. Thanks!
Just to make it clear #2 was the patch committed.
Added some docs to the base class and interface so other implementations can know - and also any other efforts to typehint.