TypeError: return value int meanwhile it should be array

Created on 22 June 2023, over 1 year ago

Problem/Motivation

TypeError: Drupal\performance_profiler\PerformanceDatabaseActions::fillTable(): Return value must be of type array, int returned in Drupal\performance_profiler\PerformanceDatabaseActions->fillTable() (line 304 of modules/contrib/performance_profiler/src/PerformanceDatabaseActions.php).

Steps to reproduce

Check the code implementation of that function:

public function fillTable(string $table, bool $is_ajax, string $insert_type = 'one_operation'): array {
    try {
      $table_name = self::TABLE_PREFIX . $table;

      if ($this->database->query("SELECT id FROM {$table_name} LIMIT 1")->fetchField()) {
        $this->messenger->addWarning("Skipped table {$table_name}, because it already has values.");
        return 0;
      }

      $timer = 'mysql_fill_generation_benchmark';
      $this->startTimer($timer);
      $query_values = $this->generateValues($table, $insert_type == 'one_operation');
      $columns = implode(', ', array_keys(self::tables()[$table]['columns']));

      if ($insert_type == 'one_operation') {
        $this->database->query("INSERT INTO `{$table_name}` ({$columns}) VALUES {$query_values}");
      }
      elseif (!empty($query_values) && is_array($query_values)) {
        if ($insert_type == 'transaction') {
          $transaction = $this->database->startTransaction();
        }
        try {
          foreach ($query_values as $values) {
            $this->database->query("INSERT INTO `{$table_name}` ({$columns}) VALUES {$values}");
          }
        }
        catch (\Exception $exception) {
          if ($insert_type == 'transaction') {
            $transaction->rollBack();
          }
        }
        if ($insert_type == 'transaction') {
          unset($transaction);
        }
      }
      return $this->stopAndLogTimer($timer, $is_ajax, "Generate and insert {$table_name}");
    }
    catch (\Exception $e) {
      $this->messenger->addWarning("Failed executing query, {$e->getMessage()}");
    }

    return [
      'message' => '',
      'time' => 0,
    ];
  }

We could see that the function could return a zero which is an int value.

Proposed resolution

Add int type hint.
public function fillTable(string $table, bool $is_ajax, string $insert_type = 'one_operation'): array | int

Remaining tasks

  1. Fork the project.
  2. Make the code change.
  3. Pull Request.
🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇹🇳Tunisia Ahmed Aziz ABBASSI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024