Array_Filter so I need a correct way to implement, because sometimes the getCategoryTree() comes empty or with null values
Easy, don't change types and feed array filter something it shouldn't eat.
array_filter($category ? $this->getCategoryTree($category->getId()) : '');
Should be (something like)
$data = $category ? $this->getCategoryTree($category->getId()) : ; if(!is_array($data)) $data = ; array_filter($data);
You can probably do it simpler then this. But, type changes in PHP can be troublesome because the language is loosely typed and won't complain to much.
This can be easly tested
<br /> <b>Warning</b>: array_filter() expects parameter 1 to be array, string given in <b>[...][...]</b> on line <b>3</b><br /> NULL
This on the other hand
Simply returns an empty array.
The last thing I will say is about 80% of the code you put in the question was unnecessary to answer the question and just serves to confuse other users.