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

Mpdf   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.84%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 73
ccs 33
cts 38
cp 0.8684
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
C save() 9 62 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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