Trying to access array offset on value of type int

Created on 13 July 2023, 12 months ago
Updated 14 July 2023, 12 months ago

Problem/Motivation

Getting this error when rendering the template.

Warning: Trying to access array offset on value of type int in Drupal\toolshed\Twig\ToolshedTwigExtension->areChildrenEmpty() (line 147 of modules/contrib/toolshed/src/Twig/ToolshedTwigExtension.php)

Line 147 - if (($key === '' || $key[0] !== '#') && is_array($child))

Proposed resolution

check if the value is a string or not a string in the condition

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States wildcats369

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

Comments & Activities

  • Issue created by @wildcats369
  • @superman369 opened merge request.
  • Status changed to Needs work 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States lemming

    Hello @superman369 thanks for the bug report and the patch.

    It would actually be better to avoid casting values, and instead update the conditional to check if the value is a string or not a string. Casting to a string has some hidden overhead here, and we can definitely avoid it.

    I'd be okay with:

    if ((is_int($key) || $key === '' || $key[0] !== '#') && is_array($child))
    

    Or

    if ((!is_string($key) || $key === '' || $key[0] !== '#') && is_array($child))
    

    The latter might be better because it verifies that they key is associative (a string) before treating it as a string. The first option should also work because PHP only allows integers and strings as array keys.

  • πŸ‡ΊπŸ‡ΈUnited States wildcats369
  • Status changed to Fixed 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States lemming

    Thanks for the quick response and the updated PR.

    I've merged this in, and have a release coming soon that will include this.

  • πŸ‡ΊπŸ‡ΈUnited States wildcats369

    Great. Thanks.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024