Instead of making huge functions that makes a lot of things given a set of options, prefer splitting them into small atoms.
<?php
/**
* A huge function that does processing and formatting.
*/
function getValues($arg, array $options) {
$values = [];
$options + [
'implode' => false,
'separator' => ', '
];
// processing...
if ($options['implode']) {
$values = implode($options['separator'], $values);
}
return $values;
}
/**
* Getting values as an array
*/
echo getValues('arg');
// => [1, 2]
/**
* Getting values as a string
*/
echo getValues('arg', [
'implode' => true,
'separator' => ' / '
]);
// => "1 / 2"
<?php
/**
* A simple function for processing.
*/
function getValues($arg) {
$values = [];
// processing
return $values;
}
/**
* A simple function for formatting.
*/
function inlineValues($arg, $separator = ', ') {
return implode($separator, values($arg)):
}
/**
* Getting values as an array.
*/
getValues('arg');
// => [1, 2]
/**
* Getting values as a string.
*/
inlineValues(
getValues('arg'),
' / '
);
// => "1 / 2"