Completed
Push — develop ( 069c66...25ff91 )
by Adrien
23:18
created

Tcpdf::save()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 8.512

Importance

Changes 0
Metric Value
cc 8
eloc 40
nc 48
nop 1
dl 0
loc 67
ccs 32
cts 40
cp 0.8
crap 8.512
rs 6.6523
c 0
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 Tcpdf 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 1
    public function save($pFilename)
19
    {
20 1
        $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 1
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
24
25
        //  Check for paper size and page orientation
26 1
        if ($this->getSheetIndex() === null) {
27
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
28
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
29
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
30
            $printMargins = $this->spreadsheet->getSheet(0)->getPageMargins();
31
        } else {
32 1
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
33 1
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
34 1
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
35 1
            $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
36
        }
37
38
        //  Override Page Orientation
39 1
        if ($this->getOrientation() !== null) {
40
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
41
                ? 'L'
42
                : 'P';
43
        }
44
        //  Override Paper Size
45 1
        if ($this->getPaperSize() !== null) {
46
            $printPaperSize = $this->getPaperSize();
47
        }
48
49 1
        if (isset(self::$paperSizes[$printPaperSize])) {
50 1
            $paperSize = self::$paperSizes[$printPaperSize];
51
        }
52
53
        //  Create PDF
54 1
        $pdf = new \TCPDF($orientation, 'pt', $paperSize);
55 1
        $pdf->setFontSubsetting(false);
56
        //    Set margins, converting inches to points (using 72 dpi)
57 1
        $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
58 1
        $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
59
60 1
        $pdf->setPrintHeader(false);
61 1
        $pdf->setPrintFooter(false);
62
63 1
        $pdf->AddPage();
64
65
        //  Set the appropriate font
66 1
        $pdf->SetFont($this->getFont());
67 1
        $pdf->writeHTML(
68 1
            $this->generateHTMLHeader(false) .
69 1
            $this->generateSheetData() .
70 1
            $this->generateHTMLFooter()
71
        );
72
73
        //  Document info
74 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
75 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
76 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
77 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
78 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
79
80
        //  Write to file
81 1
        fwrite($fileHandle, $pdf->output($pFilename, 'S'));
82
83 1
        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...
84 1
    }
85
}
86