Completed
Push — develop ( 685e29...09d456 )
by Adrien
14:14
created

DomPDF::save()   C

Complexity

Conditions 9
Paths 96

Size

Total Lines 53
Code Lines 31

Duplication

Lines 16
Ratio 30.19 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
c 2
b 0
f 0
nc 96
nop 1
dl 16
loc 53
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace PhpSpreadsheet\Writer\PDF;
4
5
/*  Require DomPDF library */
6
$pdfRendererClassFile = \PhpSpreadsheet\Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
7
if (file_exists($pdfRendererClassFile)) {
8
    require_once $pdfRendererClassFile;
9
} else {
10
    throw new \PhpSpreadsheet\Writer\Exception('Unable to load PDF Rendering library');
11
}
12
13
/**
14
 *  Copyright (c) 2006 - 2015 PhpSpreadsheet
15
 *
16
 *  This library is free software; you can redistribute it and/or
17
 *  modify it under the terms of the GNU Lesser General Public
18
 *  License as published by the Free Software Foundation; either
19
 *  version 2.1 of the License, or (at your option) any later version.
20
 *
21
 *  This library is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
 *  Lesser General Public License for more details.
25
 *
26
 *  You should have received a copy of the GNU Lesser General Public
27
 *  License along with this library; if not, write to the Free Software
28
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29
 *
30
 *  @category    PhpSpreadsheet
31
 *  @copyright   Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
32
 *  @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
33
 *  @version     ##VERSION##, ##DATE##
34
 */
35
class DomPDF extends Core implements \PhpSpreadsheet\Writer\IWriter
36
{
37
    /**
38
     *  Create a new DomPDF Writer instance
39
     *
40
     *  @param   \PhpSpreadsheet\Spreadsheet    $spreadsheet    Spreadsheet object
41
     */
42
    public function __construct(\PhpSpreadsheet\Spreadsheet $spreadsheet)
43
    {
44
        parent::__construct($spreadsheet);
45
    }
46
47
    /**
48
     *  Save Spreadsheet to file
49
     *
50
     *  @param   string     $pFilename   Name of the file to save as
51
     *  @throws  \PhpSpreadsheet\Writer\Exception
52
     */
53
    public function save($pFilename = null)
54
    {
55
        $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...
56
57
        //  Default PDF paper size
58
        $paperSize = 'LETTER';    //    Letter    (8.5 in. by 11 in.)
59
60
        //  Check for paper size and page orientation
61 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...
62
            $orientation = ($this->phpSpreadsheet->getSheet(0)->getPageSetup()->getOrientation()
63
                == \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
64
            $printPaperSize = $this->phpSpreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
65
            $printMargins = $this->phpSpreadsheet->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...
66
        } else {
67
            $orientation = ($this->phpSpreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
68
                == \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
69
            $printPaperSize = $this->phpSpreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
70
            $printMargins = $this->phpSpreadsheet->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...
71
        }
72
73
        $orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
74
75
        //  Override Page Orientation
76 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...
77
            $orientation = ($this->getOrientation() == \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_DEFAULT)
78
                ? \PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT
79
                : $this->getOrientation();
80
        }
81
        //  Override Paper Size
82
        if (!is_null($this->getPaperSize())) {
83
            $printPaperSize = $this->getPaperSize();
84
        }
85
86
        if (isset(self::$paperSizes[$printPaperSize])) {
87
            $paperSize = self::$paperSizes[$printPaperSize];
88
        }
89
90
        //  Create PDF
91
        $pdf = new self();
92
        $pdf->set_paper(strtolower($paperSize), $orientation);
93
94
        $pdf->load_html(
95
            $this->generateHTMLHeader(false) .
96
            $this->generateSheetData() .
97
            $this->generateHTMLFooter()
98
        );
99
        $pdf->render();
100
101
        //  Write to file
102
        fwrite($fileHandle, $pdf->output());
103
104
        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...
105
    }
106
}
107