Jump to content

Recommended Posts

Hi

 

I have the following code:

 

    $image_1 = $value->getElementsByTagName("Image1");
    $image1  = $image_1->item(0)->nodeValue;

    $image_2 = $value->getElementsByTagName("Image2");
    $image2  = $image_2->item(0)->nodeValue;

    $image_3 = $value->getElementsByTagName("Image3");
    $image3  = $image_3->item(0)->nodeValue;

    $filename_1 = basename($image1);
    $ch = curl_init ($image1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec ($ch);
    curl_close ($ch);
    $fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_1,'w');
    fwrite($fp, $rawdata);
    fclose($fp);

    $filename_2 = basename($image2);
    $ch = curl_init ($image2);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec ($ch);
    curl_close ($ch);
    $fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_2,'w');
    fwrite($fp, $rawdata);
    fclose($fp);

    $filename_3 = basename($image3);
    $ch = curl_init ($image3);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec ($ch);
    curl_close ($ch);
    $fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_3,'w');
    fwrite($fp, $rawdata);
    fclose($fp);

 

Would someone help me to refactor this, as I can seem needing $image_4, $image_5 at a later date.

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/228100-refactoring-help/
Share on other sites

Whenever you see repeated code like that you should replace that code with a function:

<?php
function do_work($value, $num) {

    $image = $value->getElementsByTagName("Image$num");
    $imagex  = $image->item(0)->nodeValue;

    $filename = basename($imagex);
    $ch = curl_init ($imagex);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec ($ch);
    curl_close ($ch);
    $fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename,'w');
    fwrite($fp, $rawdata);
    fclose($fp);
}

for ($i=1;$i<4;++$i) {
    do_work($value,$i);
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/228100-refactoring-help/#findComment-1176255
Share on other sites

Always think in reusable components, here's an example derived from your code:

 

function do($name) {
    $image3 = do1($value, $name);
    $filename_3 = basename($image3);
    $rawdata = do2($image3);
    do3(Mage::getBaseDir('media') . DS . 'import/'.$filename_3);
}

function do1($value, $name) {
    return $value->getElementsByTagName($name)->item(0)->nodeValue;
}

function do2($image3) {
    $ch = curl_init($image3);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close($ch);
    return $rawdata;
}

function do3($filename_3) {
    $fp = fopen($filename_3,'w');
    fwrite($fp, $rawdata);
    fclose($fp);
}

 

The above code won't run but the important thing about this code is that it shows the principles applied. If you have a big chunk of code, look for distinct tasks executed by your code and wrap each into a function.

Link to comment
https://forums.phpfreaks.com/topic/228100-refactoring-help/#findComment-1176338
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.