Verified Commit 645ffba8 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3522561 Fully type StatementInterface methods' parameters

By: mondrake
By: smustgrave
By: longwave
By: dcam
By: godotislate
By: amateescu
parent 5b4ad871
Loading
Loading
Loading
Loading
Loading
+14 −15
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public function getConnectionTarget(): string {
  /**
   * {@inheritdoc}
   */
  abstract public function execute($args = [], $options = []);
  abstract public function execute(?array $args = [], array $options = []);

  /**
   * Dispatches an event informing that the statement execution begins.
@@ -207,9 +207,7 @@ public function getQueryString() {
  /**
   * {@inheritdoc}
   */
  public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
    assert($mode instanceof FetchAs);

  public function setFetchMode(FetchAs $mode, string|int|null $a1 = NULL, array $a2 = []) {
    $this->fetchMode = $mode;
    switch ($mode) {
      case FetchAs::ClassObject:
@@ -240,8 +238,13 @@ public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
  /**
   * {@inheritdoc}
   */
  public function fetch($mode = NULL, $cursorOrientation = NULL, $cursorOffset = NULL) {
    assert($mode === NULL || $mode instanceof FetchAs);
  public function fetch(?FetchAs $mode = NULL, $cursorOrientation = NULL, $cursorOffset = NULL) {
    if ($cursorOrientation !== NULL) {
      @trigger_error("Passing the \$cursorOrientation value to " . __METHOD__ . "() is deprecated in drupal:11.4.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3551924", E_USER_DEPRECATED);
    }
    if ($cursorOffset !== NULL) {
      @trigger_error("Passing the \$cursorOffset value to " . __METHOD__ . "() is deprecated in drupal:11.4.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3551924", E_USER_DEPRECATED);
    }

    $fetchOptions = match(func_num_args()) {
      0 => $this->fetchOptions,
@@ -296,7 +299,7 @@ public function fetchAssoc() {
  /**
   * {@inheritdoc}
   */
  public function fetchField($index = 0) {
  public function fetchField(int $index = 0) {
    $column = $this->result->fetch(FetchAs::Column, ['column' => $index]);

    if ($column === FALSE) {
@@ -311,9 +314,7 @@ public function fetchField($index = 0) {
  /**
   * {@inheritdoc}
   */
  public function fetchAll($mode = NULL, $columnIndex = NULL, $constructorArguments = NULL) {
    assert($mode === NULL || $mode instanceof FetchAs);

  public function fetchAll(?FetchAs $mode = NULL, ?int $columnIndex = NULL, ?array $constructorArguments = NULL) {
    $fetchMode = $mode ?? $this->fetchMode;
    if (isset($columnIndex)) {
      $this->fetchOptions['column'] = $columnIndex;
@@ -332,16 +333,14 @@ public function fetchAll($mode = NULL, $columnIndex = NULL, $constructorArgument
  /**
   * {@inheritdoc}
   */
  public function fetchCol($index = 0) {
  public function fetchCol(int $index = 0) {
    return $this->fetchAll(FetchAs::Column, $index);
  }

  /**
   * {@inheritdoc}
   */
  public function fetchAllAssoc($key, $fetch = NULL) {
    assert($fetch === NULL || $fetch instanceof FetchAs);

  public function fetchAllAssoc(string $key, ?FetchAs $fetch = NULL) {
    $result = $this->result->fetchAllAssoc($key, $fetch ?? $this->fetchMode, $this->fetchOptions);
    $this->markResultsetFetchingComplete();
    return $result;
@@ -350,7 +349,7 @@ public function fetchAllAssoc($key, $fetch = NULL) {
  /**
   * {@inheritdoc}
   */
  public function fetchAllKeyed($keyIndex = 0, $valueIndex = 1) {
  public function fetchAllKeyed(int $keyIndex = 0, int $valueIndex = 1) {
    $result = $this->result->fetchAllKeyed($keyIndex, $valueIndex);
    $this->markResultsetFetchingComplete();
    return $result;
+53 −37
Original line number Diff line number Diff line
@@ -2,18 +2,20 @@

namespace Drupal\Core\Database;

use Drupal\Core\Database\Statement\FetchAs;

/**
 * Represents a prepared statement.
 *
 * Child implementations should either extend StatementWrapperIterator:
 * Child implementations should either extend StatementBase:
 * @code
 * class Drupal\my_module\Driver\Database\my_driver\Statement extends Drupal\Core\Database\StatementWrapperIterator {}
 * class \Drupal\my_module\Driver\Database\my_driver\Statement extends \Drupal\Core\Database\Statement\StatementBase {}
 * @endcode
 * or define their own class. If defining their own class, they will also have
 * to implement either the \Iterator or \IteratorAggregate interface before
 * Drupal\Core\Database\StatementInterface:
 * @code
 * class Drupal\my_module\Driver\Database\my_driver\Statement implements Iterator, Drupal\Core\Database\StatementInterface {}
 * class \Drupal\my_module\Driver\Database\my_driver\Statement implements \Iterator, \Drupal\Core\Database\StatementInterface {}
 * @endcode
 *
 * @ingroup database
@@ -24,15 +26,15 @@ interface StatementInterface extends \Traversable {
   * Executes a prepared statement.
   *
   * @param array|null $args
   *   An array of values with as many elements as there are bound parameters in
   *   the SQL statement being executed. This can be NULL.
   *   (Optional) An array of values with as many elements as there are bound
   *   parameters in the SQL statement being executed. This can be NULL.
   * @param array $options
   *   An array of options for this query.
   *   (Optional) An array of options for this query.
   *
   * @return bool
   *   TRUE on success, or FALSE on failure.
   */
  public function execute($args = [], $options = []);
  public function execute(?array $args = [], array $options = []);

  /**
   * Gets the query string of this statement.
@@ -68,17 +70,17 @@ public function rowCount();
   * @param \Drupal\Core\Database\Statement\FetchAs $mode
   *   One of the cases of the FetchAs enum.
   * @param string|int|null $a1
   *   An option depending of the fetch mode specified by $mode:
   *   (Optional) An option depending of the fetch mode specified by $mode:
   *   - for FetchAs::Column, the index of the column to fetch;
   *   - for FetchAs::ClassObject, the name of the class to create.
   * @param list<mixed> $a2
   *   If $mode is FetchAs::ClassObject, the optional arguments to pass to the
   *   object constructor.
   *   (Optional) If $mode is FetchAs::ClassObject, the optional arguments to
   *   pass to the constructor.
   *
   * @return bool
   *   TRUE if successful, FALSE if not.
   */
  public function setFetchMode($mode, $a1 = NULL, $a2 = []);
  public function setFetchMode(FetchAs $mode, string|int|null $a1 = NULL, array $a2 = []);

  /**
   * Fetches the next row from a result set.
@@ -87,28 +89,36 @@ public function setFetchMode($mode, $a1 = NULL, $a2 = []);
   *   (Optional) one of the cases of the FetchAs enum. If not specified,
   *   defaults to what is specified by setFetchMode().
   * @param int|null $cursor_orientation
   *   Not implemented in all database drivers, don't use.
   *   (deprecated) Not implemented in all database drivers, don't use. The
   *   $cursor_orientation parameter is deprecated in drupal:11.4.0 and will be
   *   removed in drupal:12.0.0. There is no replacement.
   * @param int|null $cursor_offset
   *   Not implemented in all database drivers, don't use.
   *   (deprecated) Not implemented in all database drivers, don't use. The
   *   $cursor_offset parameter is deprecated in drupal:11.4.0 and will be
   *   removed in drupal:12.0.0. There is no replacement.
   *
   * @return array|object|false
   *   A result, formatted according to $mode, or FALSE on failure.
   *
   * @see https://www.drupal.org/node/3551924
   */
  public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);
  public function fetch(?FetchAs $mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);

  /**
   * Returns a single field from the next record of a result set.
   * Returns a single column value from the next record of a result set.
   *
   * @param int $index
   *   The numeric index of the field to return. Defaults to the first field.
   *   (Optional) The numeric index of the column to return. Defaults to the
   *   first one.
   *
   * @return mixed
   *   A single field from the next record, or FALSE if there is no next record.
   *   A single column value from the next record, or FALSE if there is no next
   *   record.
   *
   * @throws \ValueError
   *   If there is a record and the column index is not defined.
   */
  public function fetchField($index = 0);
  public function fetchField(int $index = 0);

  /**
   * Fetches the next row and returns it as an object.
@@ -117,11 +127,11 @@ public function fetchField($index = 0);
   * StatementInterface::setFetchMode() or stdClass if not specified.
   *
   * @param string|null $class_name
   *   Name of the created class.
   *   (Optional) Name of the created class.
   * @param array $constructor_arguments
   *   Elements of this array are passed to the constructor.
   *   (Optional) Elements of this array are passed to the constructor.
   *
   * @return mixed
   * @return object|false|null
   *   The object of specified class or \stdClass if not specified. Returns
   *   FALSE or NULL if there is no next row.
   */
@@ -146,15 +156,16 @@ public function fetchAssoc();
   *   (Optional) one of the cases of the FetchAs enum. If not specified,
   *   defaults to what is specified by setFetchMode().
   * @param int|null $column_index
   *   If $mode is FetchAs::Column, the index of the column to fetch.
   * @param array $constructor_arguments
   *   If $mode is FetchAs::ClassObject, the arguments to pass to the
   *   constructor.
   *   (Optional) If $mode is FetchAs::Column, the index of the column to
   *   fetch.
   * @param array|null $constructor_arguments
   *   (Optional) If $mode is FetchAs::ClassObject, the arguments to pass to
   *   the constructor.
   *
   * @return array
   *   An array of results.
   */
  public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL);
  public function fetchAll(?FetchAs $mode = NULL, ?int $column_index = NULL, ?array $constructor_arguments = NULL);

  /**
   * Returns an entire single column of a result set as an indexed array.
@@ -162,7 +173,8 @@ public function fetchAll($mode = NULL, $column_index = NULL, $constructor_argume
   * Note that this method will run the result set to the end.
   *
   * @param int $index
   *   The index of the column number to fetch.
   *   (Optional) The index of the column number to fetch. By default, the
   *   first one.
   *
   * @return array
   *   An indexed array, or an empty array if there is no result set.
@@ -170,37 +182,41 @@ public function fetchAll($mode = NULL, $column_index = NULL, $constructor_argume
   * @throws \ValueError
   *   If there is at least one record but the column index is not defined.
   */
  public function fetchCol($index = 0);
  public function fetchCol(int $index = 0);

  /**
   * Returns the entire result set as a single associative array.
   *
   * This method is only useful for two-column result sets. It will return an
   * associative array where the key is one column from the result set and the
   * value is another field. In most cases, the default of the first two columns
   * is appropriate.
   * value is another column. In most cases, the default of the first two
   * columns is appropriate.
   *
   * Note that this method will run the result set to the end.
   *
   * @param int $key_index
   *   The numeric index of the field to use as the array key.
   *   (Optional) The numeric index of the column to use as the array key. By
   *   default, the first column.
   * @param int $value_index
   *   The numeric index of the field to use as the array value.
   *   (Optional) The numeric index of the column to use as the array value. By
   *   default, the second column.
   *
   * @return array
   *   An associative array, or an empty array if there is no result set.
   */
  public function fetchAllKeyed($key_index = 0, $value_index = 1);
  public function fetchAllKeyed(int $key_index = 0, int $value_index = 1);

  /**
   * Returns the result set as an associative array keyed by the given field.
   * Returns the result set as an associative array keyed by the given column.
   *
   * If the given key appears multiple times, later records will overwrite
   * earlier ones.
   *
   * Note that this method will run the result set to the end.
   *
   * @param string $key
   *   The name of the field on which to index the array.
   * @param \Drupal\Core\Database\Statement\FetchAs|string|null $fetch
   *   The name of the column on which to index the array.
   * @param \Drupal\Core\Database\Statement\FetchAs|null $fetch
   *   (Optional) the fetch mode to use. One of the cases of the FetchAs enum.
   *   If set to FetchAs::Associative or FetchAs::List the returned value will
   *   be an array of arrays. For any other value it will be an array of
@@ -210,6 +226,6 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1);
   * @return array
   *   An associative array, or an empty array if there is no result set.
   */
  public function fetchAllAssoc($key, $fetch = NULL);
  public function fetchAllAssoc(string $key, ?FetchAs $fetch = NULL);

}