Read a JSON config file with a fallback in PHP

$config = [];

if (is_readable($path)) {
    $config = json_decode(file_get_contents($path), true) ?? [];

    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new RuntimeException('Invalid JSON: ' . json_last_error_msg());
    }
}

$config = array_replace_recursive($defaults, $config);