NestedArray filter doesn't clear empty multidimensional arrays

Created on 17 August 2023, over 1 year ago

Problem/Motivation

When I have a multidimensional array with empty values, only the last children have the filter callable applied because the filter callable is applied before the children are evaluated.

$array = [
  'first' => ['', '', ''],
  'second => [
    ['', ''],
    '',
  ],
  'third' => [],
];

NestedArray::filter($array)

This will result in the following array:

$array = [
  'first' => [],
  'second => [
    [],
  ]
];

I would expect empty parents to also have the filter callable applied.

Steps to reproduce

Run the code example above.

Proposed resolution

Run the callable after iterating over the children elements.

Remaining tasks

Identify whether this is intentional behaviour (there don't appear to be any uses other than in test for this function).

API changes

Breaking changes to the way multidimensional arrays are filtered.

πŸ› Bug report
Status

Active

Version

9.5

Component
BaseΒ  β†’

Last updated about 16 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom kalpaitch

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

Comments & Activities

  • Issue created by @kalpaitch
  • πŸ‡¬πŸ‡§United Kingdom kalpaitch
  • πŸ‡¬πŸ‡§United Kingdom kalpaitch
  • last update over 1 year ago
    30,341 pass
  • @kalpaitch opened merge request.
  • πŸ‡¬πŸ‡§United Kingdom kalpaitch
  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

    This depends on whether the recursive traversal is top-to-bottom or bottom-to-top, and the current documentation doesn't specify that. So there is definitely a bug here, but it's either a code AND documentation bug (behaviour is wrong, should be fixed and documented) or just a documentation bug (behaviour is correct, should be documented).

      'second' => [
        ['', ''],
        '',
      ],
    

    The value of 'second' is not empty, so it's not filtered -- that's consistent with a top-to-bottom recursion.

  • πŸ‡©πŸ‡ͺGermany donquixote

    I encountered this myself while working on another core issue.

    To me, clearing out all empty arrays after their children have been cleaned up would be the preferable behavior.
    Unlike the current behavior, this would be idempotent, which is generally nice to have.

    However, this would be a change of existing behavior that we could consider a BC break.

    An alternative would be to introduce a separate method which has the desired behavior.
    Like NestedArray::filterRecursive() or NestedArray::filterCompletely().

    The solution proposed in the issue summary is correct either way: We filter the parent array _after_ filtering the children.

Production build 0.71.5 2024