Use argument unpacking in FieldItemList::delegateMethod()

Created on 31 March 2024, 5 months ago
Updated 1 April 2024, 5 months ago

Problem/Motivation

  protected function delegateMethod($method) {
    $result = [];
    $args = array_slice(func_get_args(), 1);
    foreach ($this->list as $delta => $item) {
      // call_user_func_array() is way slower than a direct call so we avoid
      // using it if have no parameters.
      $result[$delta] = $args ? call_user_func_array([$item, $method], $args) : $item->{$method}();
    }
    return $result;
  }

Something like this should be both more efficient and more readable:

  protected function delegateMethod($method, ...$args) {
    $result = [];
    foreach ($this->list as $delta => $item) {
      $result[$delta] = $item->{$method}(...$args);
    }
    return $result;
  }

or maybe even

  protected function delegateMethod($method, ...$args) {
    return array_map(fn($item) => $item->{$method}(...$args), $this->list);
  }

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Needs work

Version

10.3

Component
Field 

Last updated 1 day ago

Created by

🇬🇧United Kingdom longwave UK

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024