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

Dompdf   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 14.75 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 61
ccs 22
cts 29
cp 0.7586
rs 10
c 1
b 0
f 0
wmc 9
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
C save() 9 51 9

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\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