Home > Back-end >  How to divide one big function into small functions in php for best practices?
How to divide one big function into small functions in php for best practices?

Time:07-03

I have created a function exportFile that export file into .csv and .txt for different platforms Amazon, Magento and catch from the database. Since, the function is getting big because of long headers as well as some repeated loops how can I divide this function into separate functions for best practices?

function exportFile ($fileName, $platform, $option) {
    $test_db = new MysqliDb (TEST_DB_HOSTNAME, TEST_DB_USERNAME, TEST_DB_PASSWORD, TEST_DB_DATABASE);
    
    $file = fopen(DOWNLOAD_FILE_PATH.$fileName, 'wb') OR die(json_encode(array("status" => "error", "errMessage" => "Error while downloading file!")));
    if ($file) {
        $query = "SELECT * FROM database";
        $data = $test_db->rawQuery($query);

        switch ($platform) {
               case 'magento':
               $header = array(
                    #csv titles goes here
               );
               fputcsv($file, $header);
               foreach ($data as $rows) {
                  $csvData = array(
                      #csv records from database goes here
                  );
                  fputcsv($file, $csvData);
               }
               break;
               case 'catch':
               $header = array(
                    #csv titles goes here but not same as magento 
               );
               fputcsv($file, $header);
               foreach ($data as $rows) {
                  $csvData = array(
                      #csv records from database goes here but not same as magento
                  );
                  fputcsv($file, $csvData);
               }
               break;
            }
        }
 }

CodePudding user response:

There are many ways to break things up. Here's a starter

function fileOpen {$filename}
{  return fopen(DOWNLOAD_FILE_PATH.$fileName, 'wb') OR
          die(json_encode(array("status" => "error", "errMessage" => "Error while downloading file!")));
}

function getData ()
{
  $test_db = new MysqliDb (TEST_DB_HOSTNAME, TEST_DB_USERNAME, TEST_DB_PASSWORD, TEST_DB_DATABASE);
  $query = "SELECT * FROM database";
  return $test_db->rawQuery($query);
}

function doMagento ($file, &$data)
{
   $header = array(
        #csv titles goes here
   );
   fputcsv($file, $header);
   foreach ($data as $rows) {
      $csvData = array(
          #csv records from database goes here
      );
      fputcsv($file, $csvData);
   }
}

function doCatch ($file, &$data)
{
   $header = array(
        #csv titles goes here but not same as magento
   );
   fputcsv($file, $header);
   foreach ($data as $rows) {
      $csvData = array(
          #csv records from database goes here but not same as magento
      );
      fputcsv($file, $csvData);
   }
}

function exportFile ($fileName, $platform, $option) {

    $file = fileOpen ($filename);
    if ($file) {
        $data = getData ();

        switch ($platform) {
               case 'magento': doMagento ($file, $data);
                               break;
               case 'catch':   doCatch ($file, $data);
                               break;
            }
        }
 }

One more iteration is required to make a function for csvData. That's for you to do !

  • Related