title: How to refactor a long function?
body: <p>I have a function that prepares a receipt output. But since it has all kinds of conditions it ends up being very long and difficult to understand..</p>
  <p>How would one go about refactoring this? Any ideas?</p>
  <p>If I split this into 100 small functions would that really be better?</p>
  <pre><code>public static function prepare_receipt($printer)
  {
      if (self::hasItems($printer['id']))
      {
          $output = '';
  
          if ($_POST['pre_receipt'])
          {
              $output .= &quot;======== Pre receipt =======\n\n\n&quot;;
          }
  
          /**
           * Time and table
           */
          if ($_POST['isTakeaway'] || $_POST[&quot;isDeliveryGuys&quot;] || $_POST[&quot;isBolt&quot;]) {
              $output .= &quot;Table: &quot; . $_POST['table'] . &quot;\n&quot;;
              $output .= &quot;Floor: &quot; . $_POST['floor'] . &quot;\n&quot;;
              $output .= &quot;Time: &quot; . $_POST['takeawayTime'] . &quot;\n&quot;;
  
              if ($_POST['order_comment']) {
                  $output .= &quot;Comment: &quot; . removeSpecialChars($_POST['order_comment']) . &quot;\n&quot;;
              }
          } else {
              $output .= &quot;Table: &quot; . $_POST['table'] . &quot;\n\n&quot;;
              $output .= &quot;Floor: &quot; . $_POST['floor'] . &quot;\n\n&quot;;
  
              if ($_POST['order_comment']) {
                  $output .= &quot;Comment: &quot; . removeSpecialChars($_POST['order_comment']) . &quot;\n&quot;;
              }
          }
  
          $output .= &quot;------------------------\n&quot;;
  
  
          /**
           * Food items
           */
          foreach ($_POST['orderedItems'] as $orderedItem)
          {
              $has_unprinted_quantity = false;
  
              if (isset($orderedItem['last_printed_quantity'])) {
                  $unprinted_quantity_count = intval($orderedItem['is_printed_quantity']) - intval($orderedItem['last_printed_quantity']);
  
                  if ($unprinted_quantity_count &gt; 0) {
                      $has_unprinted_quantity = true;
                  }
              }
  
  
              if ( ($orderedItem['should_print'] &amp;&amp;
                   !$orderedItem['is_printed'] &amp;&amp;
                    $orderedItem['is_visible']) ||
                    $_POST['pre_receipt'] ||
                    $has_unprinted_quantity)
              {
                  if (is_array($orderedItem['printers'])) {
                      $in_printer = in_array($printer['id'], $orderedItem['printers']);
                  } else {
                      $in_printer = in_array($printer['id'], json_decode($orderedItem['printers'], true));
                  }
  
                  if (  $in_printer || $_POST['pre_receipt'] )
                  {
                      if ($orderedItem['is_sidedish'] &amp;&amp; !$_POST['pre_receipt']) {
                          continue;
                      }
  
                      if ($has_unprinted_quantity) {
                          $output .= $unprinted_quantity_count . 'x ';
                      } else {
                          $output .= $orderedItem['quantity'] . 'x ';
                      }
  
                      // We ned to split it for multiple lines...
                      $itemDescriptionParts = self::split($orderedItem['description']);
  
                      foreach ($itemDescriptionParts as $itemDescription) {
                          $itemDescriptionClean = removeSpecialChars($itemDescription);
                          $output .= $itemDescriptionClean;
                      }
  
                      // Add price for pre receipt
                      if ($_POST['pre_receipt']) {
                          $output .= &quot; - &quot; . number_format($orderedItem['price_with_discount'], 2, '.', ',');
                      }
                      
                      if (!$_POST['pre_receipt']) {
                          if ($orderedItem['comments'] != '') {
                              $output .= &quot;   &gt; &quot; . removeSpecialChars(substr($orderedItem['comments'], 0, 27)) . &quot;\n&quot;;
                          }
                      }
  
                      /** Side dishes */
                      if (isset($orderedItem['side_dishes']) &amp;&amp; !$_POST['pre_receipt'])
                      {
                          foreach ($orderedItem['side_dishes'] as $side_dish) {
                              $output .= &quot;\n   + &quot; . removeSpecialChars(substr($side_dish['description'], 0, 27)) . &quot;\n&quot;;
                          }
                      }
  
                      $output .= &quot;\n&quot;;
                  }
              }
          }
  
          /**
           * Sums
           */
  
  
          /**
           * Footer
           */
          $output .= &quot;------------------------\n&quot;;
  
          if ($_POST['pre_receipt'])
          {
              $output .= &quot;\nSubtotal: &quot; . number_format($_POST['order']['subtotal'], 2, '.', ',') . &quot;\n&quot;;
              $output .= &quot;Discount: &quot; . number_format($_POST['order']['discount'], 2, '.', ',') . &quot;\n&quot;;
              $output .= &quot;Total: &quot; . number_format($_POST['order']['total'], 2, '.', ',') . &quot;\n\n&quot;;
          }
  
          $output .= &quot;Time: &quot; . getTime() . &quot;\n&quot;;
  
          return $output;
      }
      else
      {
          return 'EMPTY';
      }
  }
  </code></pre>
  <p>Any pointers in the right direction would be much appreciated.</p>