Completed
Push — develop ( 782b4e...557e80 )
by Adrien
43:38
created

Dompdf::save()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 51
Code Lines 29

Duplication

Lines 9
Ratio 17.65 %

Code Coverage

Tests 22
CRAP Score 10.1394

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
nc 96
nop 1
dl 9
loc 51
ccs 22
cts 29
cp 0.7586
crap 10.1394
rs 6.2727
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
6
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
7
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
8
9
class Dompdf extends Pdf implements IWriter
10
{
11
    /**
12
     * Save Spreadsheet to file.
13
     *
14
     * @param string $pFilename Name of the file to save as
15
     *
16
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
17
     */
18 2
    public function save($pFilename)
19
    {
20 2
        $fileHandle = parent::prepareForSave($pFilename);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (prepareForSave() instead of save()). Are you sure this is correct? If so, you might want to change this to $this->prepareForSave().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
21
22
        //  Default PDF paper size
23 2
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
24
25
        //  Check for paper size and page orientation
26 2 View Code Duplication
        if ($this->getSheetIndex() === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
28
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
29
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
30
        } else {
31 2
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
32 2
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
33 2
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
34
        }
35
36 2
        $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
37
38
        //  Override Page Orientation
39 2
        if ($this->getOrientation() !== null) {
40
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
41
                ? PageSetup::ORIENTATION_PORTRAIT
42
                : $this->getOrientation();
43
        }
44
        //  Override Paper Size
45 2
        if ($this->getPaperSize() !== null) {
46
            $printPaperSize = $this->getPaperSize();
47
        }
48
49 2
        if (isset(self::$paperSizes[$printPaperSize])) {
50 2
            $paperSize = self::$paperSizes[$printPaperSize];
51
        }
52
53
        //  Create PDF
54 2
        $pdf = new \Dompdf\Dompdf();
55 2
        $pdf->setPaper(strtolower($paperSize), $orientation);
56
57 2
        $pdf->loadHtml(
58 2
            $this->generateHTMLHeader(false) .
59 2
            $this->generateSheetData() .
60 2
            $this->generateHTMLFooter()
61
        );
62 2
        $pdf->render();
63
64
        //  Write to file
65 2
        fwrite($fileHandle, $pdf->output());
66
67 2
        parent::restoreStateAfterSave($fileHandle);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (restoreStateAfterSave() instead of save()). Are you sure this is correct? If so, you might want to change this to $this->restoreStateAfterSave().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
68 2
    }
69
}
70