I need a little help in the PHP, because I'm trying to use the array_filter()
to prevent to many lines and make it simply and clear, but I'm struggling in this one, even if I'm reading this documentation: Array_Filter so I need a correct way to implement, because sometimes the getCategoryTree()
comes empty or with null values, which I'm trying to not show it, I thought to create other function to show only specific things without category, but I feel that it is not efficient:
What I'm trying is to populate from the url www.foobar.com/checkout/payments, and this is the original code:
$page['eData'] = [
'codeMoneyFormatter' => $this->moneyFormatter->getcodeMoneyFormatter(),
'checkout' => [
'actionField' => ['step' => 3, 'option' => 'Review Order'],
// ---------------- THIS ONE MUST REFACTOR -------
'products' => $this->getCartFromOrder($order),
],
];
// --------------------- REFACTOR ------------------
$itemData['category'] = $category ? $this->getCategoryTree($category->getId()) : '';
// -------------------------------------------------
This function is where it shows the category, but I don't like it because at all, because in some pages it comes empty/null which it's not correct, so I want to use the array_filter()
// --------------------- ARRAY_FILTER ------------------
$itemData['category'] = array_filter($category ? $this->getCategoryTree($category->getId()) : '');
// -------------------------------------------------
The function that I created to show only specific thing, it's correct because it does not show the category, but I feel that it's unnecessary because its repeating the same from the original one:
$page['eData'] = [
'codeMoneyFormatter' => $this->moneyFormatter->getcodeMoneyFormatter(),
'checkout' => [
'actionField' => ['step' => 3, 'option' => 'Review Order'],
'products' => $this->getCheckoutFromOrder($order),
],
];
It's repeating almost the same functions and it's not the correct standard of DRY (Don't repeat yourself) that's why I thought to implement the array_filter()
but how???