title: split long method in different methods
body: <p>I have this method:</p>
  <pre><code>public function createExcel(Task $task = null, Project $project = null, $projectTitle = null)
      {
          $this-&gt;spreadsheet = new Spreadsheet();
  
          $default_border = array(
              'style' =&gt; Border::BORDER_THIN,
              'color' =&gt; array('rgb' =&gt; '1006A3')
          );
  
          $style_header = array(
              'borders' =&gt; array(
                  'bottom' =&gt; $default_border,
                  'left' =&gt; $default_border,
                  'top' =&gt; $default_border,
                  'right' =&gt; $default_border,
              ),
              'fill' =&gt; array(
                  'type' =&gt; Fill::FILL_SOLID,
                  'color' =&gt; array('rgb' =&gt; 'E1E0F7'),
              ),
              'font' =&gt; array(
                  'bold' =&gt; true,
                  'size' =&gt; 16,
              )
          );
  
          $style_content = array(
              'borders' =&gt; array(
                  'bottom' =&gt; $default_border,
                  'left' =&gt; $default_border,
                  'top' =&gt; $default_border,
                  'right' =&gt; $default_border,
              ),
              'fill' =&gt; array(
                  'type' =&gt; Fill::FILL_SOLID,
                  'color' =&gt; array('rgb' =&gt; 'eeeeee'),
              ),
              'font' =&gt; array(
                  'size' =&gt; 12,
              )
          );
  
          /**
           * Header information of the excel sheet
           */
          $this-&gt;spreadsheet-&gt;setActiveSheetIndex(0)
              -&gt;setCellValue('A1', 'Zoekterm')
              -&gt;setCellValue('B1', 'Website')
              -&gt;setCellValue('C1', 'Pagina')
              -&gt;setCellValue('D1', 'Paginatitel')
              -&gt;setCellValue('E1', 'Paginabeschrijving')
              -&gt;setCellValue('F1', 'KvK-nummer')
              -&gt;setCellValue('G1', 'Vestigingsnummer')
              -&gt;setCellValue('H1', 'Adresgegevens');
          $this-&gt;spreadsheet-&gt;getActiveSheet()-&gt;getStyle('A1:H1')-&gt;applyFromArray($style_header); // give style to header
  
          $dataku = [];
  
  
          if ($projectTitle == \BoolState::TRUE) {
              foreach ($project-&gt;tasks as $task)
                  if ($task-&gt;status_id == TaskStatus::CLOSED)
                      foreach ($task-&gt;activeResults as $result)
                          $dataku[] = [$result-&gt;task-&gt;search_query, $result-&gt;page_url, $result-&gt;page_link, $result-&gt;page_title, $result-&gt;page_description, $result-&gt;coc_number, $result-&gt;branch_code, $result-&gt;address];
          } else {
              foreach ($task-&gt;activeResults as $result)
                  $dataku[] = [$result-&gt;task-&gt;search_query, $result-&gt;page_url, $result-&gt;page_link, $result-&gt;page_title, $result-&gt;page_description, $result-&gt;coc_number, $result-&gt;branch_code, $result-&gt;address];
          }
  
  
          $firststyle = 'A2';
          for ($i = 0; $i &lt; count($dataku); $i++) {
              $urut = $i + 2;
              $search_query = 'A' . $urut;
              $page_url = 'B' . $urut;
              $page_link = 'C' . $urut;
              $page_title = 'D' . $urut;
              $page_description = 'E' . $urut;
              $coc_number = 'F' . $urut;
              $branch_code = 'G' . $urut;
              $address = 'H' . $urut;
              $this-&gt;spreadsheet-&gt;setActiveSheetIndex(0)
                  -&gt;setCellValue($search_query, $dataku[$i][0])
                  -&gt;setCellValue($page_url, $dataku[$i][1])
                  -&gt;setCellValue($page_link, $dataku[$i][2])
                  -&gt;setCellValue($page_title, $dataku[$i][3])
                  -&gt;setCellValue($page_description, $dataku[$i][4])
                  -&gt;setCellValue($coc_number, $dataku[$i][5])
                  -&gt;setCellValue($branch_code, $dataku[$i][6])
                  -&gt;setCellValue($address, $dataku[$i][7]);
  
              $laststyle = $page_description;
          }
  
          $this-&gt;spreadsheet-&gt;getActiveSheet()-&gt;getStyle($firststyle . ':' . $laststyle)-&gt;applyFromArray($style_content); // give style to header
  
          $writer = $this-&gt;outType == 'xlsx' ? new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this-&gt;spreadsheet) : new Xls($this-&gt;spreadsheet);
          var_dump($this-&gt;stream);
          if (!$this-&gt;stream) {
              $writer-&gt;save($this-&gt;filename);
          } else {
              $this-&gt;filename = $this-&gt;filename == null ? $this-&gt;title : $this-&gt;filename;
              $this-&gt;cleanOutput();
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Pragma: public');
              header('Cache-Control: max-age=0');
              header('Content-Disposition: attachment;filename=&quot;NVWA HDT ' . ucfirst($projectTitle ? $task-&gt;project-&gt;description : $task-&gt;search_query) . '.Xls&quot;'); // file name of excel
              header('Content-type: application/vnd.ms-excel');
              $writer-&gt;save('php://output');
              Yii::app()-&gt;end();
          }
      }
  
  
  </code></pre>
  <p>So the upper part is for then layout of the excel sheet. But is it cleaner to have that in a seperate method?</p>
  <p>Thank you</p>
  <p>if I do it like this:</p>
  <pre><code>&lt;?php
  
  namespace Scrape\domain;
  
  $autoload =   DIR_ROOT . 'protected/extensions/phpexcel/Classes/vendor/autoload.php';
  require $autoload;
  
  use Yii;
  use CWidget;
  use Scrape\models\Task;
  use Scrape\models\Project;
  use Scrape\models\TaskStatus;
  use PhpOffice\PhpSpreadsheet\Style\Fill;
  use PhpOffice\PhpSpreadsheet\Writer\Xls;
  use PhpOffice\PhpSpreadsheet\Spreadsheet;
  use PhpOffice\PhpSpreadsheet\Style\Border;
  
  
  define('_SET_MEMORY_LIMIT_', '1024M');
  
  class Downloader extends CWidget
  {
      private $spreadsheet;
      private $task = null;
      private $project = null;
      private $projectTitle = null;
  
      private $style_header = [];
      private $style_content = [];
      private $dataku = [];
      private $stream     = true; //stream to browser
  
      private $outType = 'xlsx';
      private $filename   = null; //export FileName //false;
      private $title = '';
  
      public function downloadByTask(Task $task)
      {
          $this-&gt;createExcel($task, null);
      }
  
      public function downloadByProject(Project $project)
      {
  
          //var_dump($project);
          $this-&gt;createExcel(null, $project, true);
      }
  
      public function createExcel(Task $task = null, Project $project = null, $projectTitle = false)
      {
  
          
          $this-&gt;spreadsheet = new Spreadsheet();
          //var_dump($project);
          $this-&gt;initializeProperties($task, $project, $projectTitle);
  
          $this-&gt;setHeaderContent();
  
          $this-&gt;setMainContent($task, $project, $projectTitle);
  
          $this-&gt;doOutput();
      }
  
      /*-----------------------------------------------------------------
  
      PRIVATE METHODS
  
      -----------------------------------------------------------------*/
      /**
       * Initializes properties
       *
       *
       */
      private function initializeProperties(Task $task = null , Project $project = null, $projectTitle )
      {
          var_dump($this-&gt;project);
          $this-&gt;task = $task;
          $this-&gt;project = $project;
          $this-&gt;projectTitle = $projectTitle;
  
          $default_border = [
              'style' =&gt; Border::BORDER_THIN,
              'color' =&gt; ['rgb' =&gt; '1006A3']
          ];
  
          $this-&gt;style_header = [
              'borders' =&gt; [
                  'bottom' =&gt; $default_border,
                  'left' =&gt; $default_border,
                  'top' =&gt; $default_border,
                  'right' =&gt; $default_border,
              ],
              'fill' =&gt; [
                  'type' =&gt; Fill::FILL_SOLID,
                  'color' =&gt; ['rgb' =&gt; 'E1E0F7'],
              ],
              'font' =&gt; [
                  'bold' =&gt; true,
                  'size' =&gt; 16,
              ]
          ];
  
          $this-&gt;style_content = [
              'borders' =&gt; [
                  'bottom' =&gt; $default_border,
                  'left' =&gt; $default_border,
                  'top' =&gt; $default_border,
                  'right' =&gt; $default_border,
              ],
              'fill' =&gt; [
                  'type' =&gt; Fill::FILL_SOLID,
                  'color' =&gt; ['rgb' =&gt; 'eeeeee'],
              ],
              'font' =&gt; [
                  'size' =&gt; 12,
              ]
          ];
      }
  
      /**
       * Sets the header information of the excel sheet
       *
       *
       */
      private function setHeaderContent()
      {
          $this-&gt;spreadsheet-&gt;setActiveSheetIndex(0)
              -&gt;setCellValue('A1', 'Zoekterm')
              -&gt;setCellValue('B1', 'Website')
              -&gt;setCellValue('C1', 'Pagina')
              -&gt;setCellValue('D1', 'Paginatitel')
              -&gt;setCellValue('E1', 'Paginabeschrijving')
              -&gt;setCellValue('F1', 'KvK-nummer')
              -&gt;setCellValue('G1', 'Vestigingsnummer')
              -&gt;setCellValue('H1', 'Adresgegevens');
          $this-&gt;spreadsheet-&gt;getActiveSheet()-&gt;getStyle('A1:H1')-&gt;applyFromArray($this-&gt;style_header); // give style to header
      }
  
      /**
       * Sets the main excel content
       *
       *
       */
      private function setMainContent()
      {
  
          $this-&gt;prepareDataku();
  
          $firststyle = 'A2';
          $dataku = &amp;$this-&gt;dataku; // define alias
          for ($i = 0; $i &lt; count($dataku); $i++) {
              $urut = $i + 2;
              $search_query = 'A' . $urut;
              $page_url = 'B' . $urut;
              $page_link = 'C' . $urut;
              $page_title = 'D' . $urut;
              $page_description = 'E' . $urut;
              $coc_number = 'F' . $urut;
              $branch_code = 'G' . $urut;
              $address = 'H' . $urut;
              $this-&gt;spreadsheet-&gt;setActiveSheetIndex(0)
                  -&gt;setCellValue($search_query, $dataku[$i][0])
                  -&gt;setCellValue($page_url, $dataku[$i][1])
                  -&gt;setCellValue($page_link, $dataku[$i][2])
                  -&gt;setCellValue($page_title, $dataku[$i][3])
                  -&gt;setCellValue($page_description, $dataku[$i][4])
                  -&gt;setCellValue($coc_number, $dataku[$i][5])
                  -&gt;setCellValue($branch_code, $dataku[$i][6])
                  -&gt;setCellValue($address, $dataku[$i][7]);
  
              $laststyle = $page_description;
          }
  
          $this-&gt;spreadsheet-&gt;getActiveSheet()-&gt;getStyle($firststyle . ':' . $laststyle)-&gt;applyFromArray($this-&gt;style_content);
      }
  
      /**
       * Prepares dataku array needed by setMainContent()
       *
       *
       */
      private function prepareDataku()
      {
  
          // Pay attention to &quot;!=&quot;. This block is the else block in your code
          if ($this-&gt;projectTitle != \BoolState::TRUE) {
              foreach ($this-&gt;task-&gt;activeResults as $result) {
                  $this-&gt;dataku[] = [$result-&gt;task-&gt;search_query, $result-&gt;page_url, $result-&gt;page_link, $result-&gt;page_title, $result-&gt;page_description, $result-&gt;coc_number, $result-&gt;branch_code, $result-&gt;address];
              }
              return;
          }
  
          foreach ($this-&gt;project-&gt;tasks as $task) {
              if ($task-&gt;status_id == TaskStatus::CLOSED) {
                  foreach ($task-&gt;activeResults as $result) {
                      $this-&gt;dataku[] = [$result-&gt;task-&gt;search_query, $result-&gt;page_url, $result-&gt;page_link, $result-&gt;page_title, $result-&gt;page_description, $result-&gt;coc_number, $result-&gt;branch_code, $result-&gt;address];
                  }
              }
          }
      }
  
      /**
       * Saves the generated excel file on the local disc or writes it to the output stream.
       *
       *
       *
       */
      private function doOutput()
      {
  
  
  
          $writer = $this-&gt;outType == 'xlsx' ? new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this-&gt;spreadsheet) : new Xls($this-&gt;spreadsheet);
          // var_dump($this-&gt;stream);
  
          # Save the excel file on the disc
          if (!$this-&gt;stream) {
              $writer-&gt;save($this-&gt;filename);
              return;
          }
  
          # Print the excel to the output stream
          $this-&gt;filename = $this-&gt;filename == null ? $this-&gt;title : $this-&gt;filename;
  
          $filename = &quot;NVWA HDT &quot; . ucfirst($this-&gt;projectTitle ? $this-&gt;task-&gt;project-&gt;description : $this-&gt;task-&gt;search_query) . '.Xls';
          $this-&gt;cleanOutput();
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Pragma: public');
          header('Cache-Control: max-age=0');
          header('Content-Disposition: attachment;filename=&quot;' . $filename . '&quot;'); // file name of excel
          header('Content-type: application/vnd.ms-excel');
          $writer-&gt;save('php://output');
          Yii::app()-&gt;end();
      }
  }
  
  
  </code></pre>
  <p>then the</p>
  <pre><code>var_dump($this-&gt;project);
  
  </code></pre>
  <p>in the method</p>
  <pre><code>initializeProperties
  
  </code></pre>
  <p>it is not called.</p>
  <pre><code>Attempt to read property &quot;project&quot; on null
  </code></pre>
  <p>oke, in the method:</p>
  <pre><code>private function initializeProperties( Task $task = null , Project $project = null, $projectTitle = true )
      {
          var_dump($this-&gt;project);
  }
  </code></pre>
  <p>is project null</p>
  <p>but in the method:</p>
  <pre><code>private function doOutput()
      {
  
          var_dump($this-&gt;project);
  }
  
  </code></pre>
  <p>I see the info for project.</p>
  <p>I have a defaultController and in the defulatController there are these two methods:</p>
  <pre><code>    public function downloadByTask(Task $task)
      {
          $this-&gt;createExcel($task, null);
      }
  
      public function downloadByProject(Project $project)
      {
          //var_dump($project);
          $this-&gt;createExcel(null, $project, true);
      }
  </code></pre>