Avoid using in_array function to compare an array of objects

Created on 2 May 2025, 7 days ago

Problem/Motivation

Avoid the potential for fatal error: "nesting level too deep".

in the TaxonomyReplaceService, the function getTermReferenceField() uses the in_array function to check of a Term object exists in an array of Term objects. This can potentially lead to a fatal error. A similar case is described in https://coderwall.com/p/5efmag/if-you-get-a-nesting-level-too-deep-error...

The link above recommends using strict mode for the in_array function, but strict mode just checks the object types. In this case the code in question is used to check if a Term is in an array of Terms. It should suffice to check if the term IDs are the same. The description of the function actually says this:

"Get the name of the term reference field relevant to this term ID"

My recommendation here is to modify the code in TaxonomyReplaceService beginning on line 146, replacing:

if (in_array($term, $referenced_terms)) {
  return $field_name;
}

with

foreach ($referenced_terms as $referenced_term) {
  if ($referenced_term->id() === $term->id()) {
    return $field_name;
  }
}
πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States aaronpinero

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