You could just do:

```php
        if (null !== $encoding) {
            return new \DOMDocument($version, $encoding);
        }
        
        return new \DOMDocument();
```