Have I gone too far in factoring out repeated code in this example? In a controller (PHP, CodeIgniter) I'm adding scripts &amp; css to variables to be used in the view output. My initial coding looked like this:

private function _add_script($path, $where = self::ADD_LAST) {
    $html = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    switch($where) {
        case self::ADD_LAST:
            array_push($this-&gt;_data['script'], $html);
            break;
        case self::ADD_FIRST:
            array_unshift($this-&gt;_data['script'], $html);
            break;
    }
}

private function _add_css($path, self::ADD_LAST) {
    $html = "    &lt;link rel='stylesheet' href='$path'&gt;\n";
    switch($where) {
        case self::ADD_LAST:
            array_push($this-&gt;_data['css'], $html);
            break;
        case self::ADD_FIRST:
            array_unshift($this-&gt;_data['css'], $html);
            break;
    }
}


Well, I noticed that these two are basically just repeating themselves, so I immediately rewrote them into this format:

private function _add_script($path, $where = self::ADD_LAST) {
    $html = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    $this-&gt;_add_external('script', $html, $where);
}


private function _add_css($path, $where = self::ADD_LAST) {
    $html = "&lt;link rel='stylesheet' href='$path'&gt;\n";
    $this-&gt;_add_external('css', $html, $where);
}



private function _add_external($type, $html, $where) {
    switch($where) {
        case self::ADD_LAST:
            array_push($this-&gt;_data[$type], $html);
            break;
        case self::ADD_FIRST:
            array_unshift($this-&gt;_data[$type], $html);
            break;
    }
}


Does the second version seem like the way to go? Or perhaps a 3rd alternative. The requirements call for being able to add elements to the list either before or after all other elements already in the list.
I think what you did was a good thing. I also think you could combine the first 2 functions as well. 

Something like this: 

private function _add_item($path, $type, $where = self::ADD_LAST) {
    $script = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    $css = "&lt;link rel='stylesheet' href='$path'&gt;\n";

    if ($type == 'script') {
        $html = $script; 
    }
    elseif ($type == 'css') {
        $html = $css;
    }

    $this-&gt;_add_external($type, $html, $where);
}


Or this: 

private function _add_item($path, $type, $where = self::ADD_LAST) {
    $script = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    $css = "&lt;link rel='stylesheet' href='$path'&gt;\n";

    switch($type) {
        case 'script':
            $html = $script; 
            break;
        case 'type':
            $html = $css;
            break;
    }

    $this-&gt;_add_external($type, $html, $where);
}


Or this (My favorite): 

private function _add_item($path, $type, $where = self::ADD_LAST) {
    $array['script'] = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    $array['css'] = "&lt;link rel='stylesheet' href='$path'&gt;\n";

    $html = $array[$type]; 

    $this-&gt;_add_external($type, $html, $where);
}


EDIT:  

And I think this would work too, an all in one solution:  

private function _add_item_external($path, $type, $where = self::ADD_LAST) {
    $array['script'] = "&lt;script src='$path'&gt;&lt;/script&gt;\n";
    $array['css'] = "&lt;link rel='stylesheet' href='$path'&gt;\n";

    switch($where) {
        case self::ADD_LAST:
            array_push($this-&gt;_data[$type], $array[$type]);
            break;
        case self::ADD_FIRST:
            array_unshift($this-&gt;_data[$type], $array[$type]);
            break;
    }
}