You are here

public function MarkdownBenchmarkAverages::iterate in Markdown 3.0.x

Iterates a callback that produces benchmarks.

Parameters

callable $callback: A callback.

array $args: The arguments to provide to the $callback.

Return value

static

File

src/MarkdownBenchmarkAverages.php, line 134

Class

MarkdownBenchmarkAverages
Class MarkdownBenchmarkAverages.

Namespace

Drupal\markdown

Code

public function iterate(callable $callback, array $args = []) {
  $this->benchmarks = [];

  // Iterate the callback the specified amount of times.
  for ($i = 0; $i < $this->iterationCount; $i++) {
    $benchmarks = (array) call_user_func_array($callback, $args);

    // Verify all benchmarks are the proper object.
    foreach ($benchmarks as $benchmark) {
      if (!$benchmark instanceof MarkdownBenchmark) {
        throw new \InvalidArgumentException(sprintf('The provided callback must return an instance of \\Drupal\\markdown\\MarkdownBenchmark, got "%s" instead.', (string) $benchmark));
      }

      // Remove the result if this is the last benchmark. This is to reduce
      // the amount of storage needed on the backend.
      if ($this->iterationCount > 1 && $i < $this->iterationCount - 1) {
        $benchmark
          ->clearResult();
      }
    }
    $this->benchmarks = array_merge($this->benchmarks, $benchmarks);
  }
  return $this;
}