I think it would be clearer to put this logic upfront. 
```php
if (!$entity) {
  ...
}

if ($type) {
   ...
}
```