@alexpott ,
There is typo mistake in $role_permissions[$rid] = $entities[$rid]?->getPermissions() : [];
. It should be $role_permissions[$rid] = $entities[$rid]->getPermissions() ?: [];
Added new points: Updating the composer file to latest 9.x ,Updating the composer file to latest Drupal 10: