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

TcPDF::save()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 67
Code Lines 40

Duplication

Lines 11
Ratio 16.42 %

Code Coverage

Tests 32
CRAP Score 8.512

Importance

Changes 0
Metric Value
cc 8
eloc 40
c 0
b 0
f 0
nc 48
nop 1
dl 11
loc 67
ccs 32
cts 40
cp 0.8
crap 8.512
rs 6.6523

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 TcPDF 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 1
    public function save($pFilename = null)
37
    {
38 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...
39
40
        //  Default PDF paper size
41 1
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
42
43
        //  Check for paper size and page orientation
44 1 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();
49
        } else {
50 1
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
51 1
                == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
52 1
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
53 1
            $printMargins = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageMargins();
54
        }
55
56
        //  Override Page Orientation
57 1
        if (!is_null($this->getOrientation())) {
58
            $orientation = ($this->getOrientation() == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE)
59
                ? 'L'
60
                : 'P';
61
        }
62
        //  Override Paper Size
63 1
        if (!is_null($this->getPaperSize())) {
64
            $printPaperSize = $this->getPaperSize();
65
        }
66
67 1
        if (isset(self::$paperSizes[$printPaperSize])) {
68 1
            $paperSize = self::$paperSizes[$printPaperSize];
69
        }
70
71
        //  Create PDF
72 1
        $pdf = new \TCPDF($orientation, 'pt', $paperSize);
73 1
        $pdf->setFontSubsetting(false);
74
        //    Set margins, converting inches to points (using 72 dpi)
75 1
        $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);
76 1
        $pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72);
77
78 1
        $pdf->setPrintHeader(false);
79 1
        $pdf->setPrintFooter(false);
80
81 1
        $pdf->AddPage();
82
83
        //  Set the appropriate font
84 1
        $pdf->SetFont($this->getFont());
85 1
        $pdf->writeHTML(
86 1
            $this->generateHTMLHeader(false) .
87 1
            $this->generateSheetData() .
88 1
            $this->generateHTMLFooter()
89
        );
90
91
        //  Document info
92 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
93 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
94 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
95 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
96 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
97
98
        //  Write to file
99 1
        fwrite($fileHandle, $pdf->output($pFilename, 'S'));
100
101 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...
102 1
    }
103
}
104