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
Description
PHP 8.5 deprecates using
nullas an array offset.QueryBuilder::reorderNodes()andbuildRebuildDictionary()use$parentId(which can benullfor root nodes) as an array key in multiple places, producing deprecationwarnings.
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
$dictionary[null] = reset($dictionary);isset($dictionary[$parentId])foreach ($dictionary[$parentId] as $model)unset($dictionary[$parentId])$dictionary[$model->getParentId()][]$dictionary[$parentId][]Suggested fix
Normalize
nullto empty string''when used as an array key, while keeping the originalnullvalue forrawNode()calls (wherenullparent is semantically correct for root nodes).Line 914: