Skip to content

PHP 8.5: Using null as an array offset is deprecated in QueryBuilder.php #631

Description

@axvidev

Description

PHP 8.5 deprecates using null as an array offset. QueryBuilder::reorderNodes() and buildRebuildDictionary() use $parentId (which can be null for root nodes) as an array key in multiple places, producing deprecation
warnings.

Warning

Using null as an array offset is deprecated in vendor/kalnoy/nestedset/src/QueryBuilder.php on line 946
Using null as an array offset is deprecated in vendor/kalnoy/nestedset/src/QueryBuilder.php on line 951

Affected lines

  • Line 914: $dictionary[null] = reset($dictionary);
  • Line 946: isset($dictionary[$parentId])
  • Line 951: foreach ($dictionary[$parentId] as $model)
  • Line 963: unset($dictionary[$parentId])
  • Line 1007: $dictionary[$model->getParentId()][]
  • Line 1071: $dictionary[$parentId][]

Suggested fix

Normalize null to empty string '' when used as an array key, while keeping the original null value for rawNode() calls (where null parent is semantically correct for root nodes).

Line 914:

// Before
$dictionary[null] = reset($dictionary);

// After
$dictionary[''] = reset($dictionary);

Method reorderNodes() (lines 943-966):
protected static function reorderNodes(
    array &$dictionary, array &$updated, $parentId = null, $cut = 1
) {
    $key = $parentId ?? '';

    if (! isset($dictionary[$key])) {
        return $cut;
    }

    foreach ($dictionary[$key] as $model) {
        $lft = $cut;

        $cut = self::reorderNodes($dictionary, $updated, $model->getKey(), $cut + 1);

        if ($model->rawNode($lft, $cut, $parentId)->isDirty()) {
            $updated[] = $model;
        }

        ++$cut;
    }

    unset($dictionary[$key]);

    return $cut;
}

Line 1007:
// Before
$dictionary[$model->getParentId()][] = $model;

// After
$dictionary[$model->getParentId() ?? ''][] = $model;

Line 1071:
// Before
$dictionary[$parentId][] = $model;

// After
$dictionary[$parentId ?? ''][] = $model;

Environment

- PHP 8.5
- Laravel 12
- kalnoy/nestedset v6.0.6

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions