I have written this method to normalize some part of an array into a consistent way.
Input for example can be:
array(
0 => "field_1"
1 => "field_2"
"field_3" => array("type" => "some_type");
);
This should be transformed to:
array(
"field_1" => array("type" => "default");
"field_2" => array("type" => "default");
"field_3" => array("type" => "some_type");
);
I'm trying to lower the cyclomatic complexity (if it all possible).
public function parse_fields(array $fields)
{
$default_value = array('type' => 'default');
$result = array();
foreach($fields as $key => $value)
{
if(is_numeric($key))
{
$result[$value] = $default_value;
}
else
{
if(!empty($value))
{
$result[$key] = $value;
}
else
{
$result[$key] = $default_value;
}
}
}
return $result;
}
Note that in the future, the default values can be expanded, and would need to be merged if some elements were missing from the input array. Can you give tips how I can refactor this code to make it cleaner?