Completed
Push — develop ( 66f372...1cdc85 )
by Adrien
20:59
created

DomPDF::save()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 53
Code Lines 31

Duplication

Lines 16
Ratio 30.19 %

Code Coverage

Tests 23
CRAP Score 10.3926

Importance

Changes 0
Metric Value
cc 9
eloc 31
c 0
b 0
f 0
nc 96
nop 1
dl 16
loc 53
ccs 23
cts 31
cp 0.7419
crap 10.3926
rs 6.8963

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
/**
6
 *  Copyright (c) 2006 - 2015 PhpSpreadsheet.
7
 *
8
 *  This library is free software; you can redistribute it and/or
9
 *  modify it under the terms of the GNU Lesser General Public
10
 *  License as published by the Free Software Foundation; either
11
 *  version 2.1 of the License, or (at your option) any later version.
12
 *
13
 *  This library is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 *  Lesser General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU Lesser General Public
19
 *  License along with this library; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 *  @category    PhpSpreadsheet
23
 *
24
 *  @copyright   Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 *  @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class DomPDF extends Core implements \PhpOffice\PhpSpreadsheet\Writer\IWriter
28
{
29
    /**
30
     *  Save Spreadsheet to file.
31
     *
32
     *  @param   string     $pFilename   Name of the file to save as
33
     *
34
     *  @throws  \PhpOffice\PhpSpreadsheet\Writer\Exception
35
     */
36 2
    public function save($pFilename = null)
37
    {
38 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...
39
40
        //  Default PDF paper size
41 2
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
42
43
        //  Check for paper size and page orientation
44 2 View Code Duplication
        if (is_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...
45
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
46
                == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
47
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
48
            $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...
49
        } else {
50 2
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
51 2
                == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
52 2
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
53 2
            $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...
54
        }
55
56 2
        $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
57
58
        //  Override Page Orientation
59 2 View Code Duplication
        if (!is_null($this->getOrientation())) {
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...
60
            $orientation = ($this->getOrientation() == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_DEFAULT)
61
                ? \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT
62
                : $this->getOrientation();
63
        }
64
        //  Override Paper Size
65 2
        if (!is_null($this->getPaperSize())) {
66
            $printPaperSize = $this->getPaperSize();
67
        }
68
69 2
        if (isset(self::$paperSizes[$printPaperSize])) {
70 2
            $paperSize = self::$paperSizes[$printPaperSize];
71
        }
72
73
        //  Create PDF
74 2
        $pdf = new \Dompdf\Dompdf();
75 2
        $pdf->set_paper(strtolower($paperSize), $orientation);
0 ignored issues
show
Deprecated Code introduced by
The method Dompdf\Dompdf::set_paper() has been deprecated.

This method has been deprecated.

Loading history...
76
77 2
        $pdf->load_html(
0 ignored issues
show
Deprecated Code introduced by
The method Dompdf\Dompdf::load_html() has been deprecated.

This method has been deprecated.

Loading history...
78 2
            $this->generateHTMLHeader(false) .
79 2
            $this->generateSheetData() .
80 2
            $this->generateHTMLFooter()
81
        );
82 2
        $pdf->render();
83
84
        //  Write to file
85 2
        fwrite($fileHandle, $pdf->output());
86
87 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...
88 2
    }
89
}
90