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

Mpdf::save()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 64
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 8.216

Importance

Changes 0
Metric Value
cc 8
eloc 40
nc 48
nop 1
dl 0
loc 64
ccs 34
cts 40
cp 0.85
crap 8.216
rs 6.8232
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\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
7
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
8
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
9
10
class Mpdf extends Pdf implements IWriter
11
{
12
    /**
13
     * Save Spreadsheet to file.
14
     *
15
     * @param string $pFilename Name of the file to save as
16
     *
17
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
18
     * @throws PhpSpreadsheetException
19
     */
20 1
    public function save($pFilename)
21
    {
22 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...
23
24
        //  Default PDF paper size
25 1
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
26
27
        //  Check for paper size and page orientation
28 1
        if (null === $this->getSheetIndex()) {
29
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
30
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
31
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
32
            $printMargins = $this->spreadsheet->getSheet(0)->getPageMargins();
0 ignored issues
show
Unused Code introduced by
$printMargins is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
        } else {
34 1
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
35 1
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
36 1
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
37 1
            $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
0 ignored issues
show
Unused Code introduced by
$printMargins is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
        }
39 1
        $this->setOrientation($orientation);
40
41
        //  Override Page Orientation
42 1
        if (null !== $this->getOrientation()) {
43 1
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
44
                ? PageSetup::ORIENTATION_PORTRAIT
45 1
                : $this->getOrientation();
46
        }
47 1
        $orientation = strtoupper($orientation);
48
49
        //  Override Paper Size
50 1
        if (null !== $this->getPaperSize()) {
51
            $printPaperSize = $this->getPaperSize();
52
        }
53
54 1
        if (isset(self::$paperSizes[$printPaperSize])) {
55 1
            $paperSize = self::$paperSizes[$printPaperSize];
56
        }
57
58
        //  Create PDF
59 1
        $config = ['tempDir' => $this->tempDir];
60 1
        $pdf = new \Mpdf\Mpdf($config);
61 1
        $ortmp = $orientation;
62 1
        $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
63 1
        $pdf->DefOrientation = $orientation;
64 1
        $pdf->AddPage($orientation);
65
66
        //  Document info
67 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
68 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
69 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
70 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
71 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
72
73 1
        $pdf->WriteHTML(
74 1
            $this->generateHTMLHeader(false) .
75 1
            $this->generateSheetData() .
76 1
            $this->generateHTMLFooter()
77
        );
78
79
        //  Write to file
80 1
        fwrite($fileHandle, $pdf->Output('', 'S'));
81
82 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...
83 1
    }
84
}
85