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

MPDF   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 21.62 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 74
ccs 33
cts 39
cp 0.8462
rs 10
wmc 8
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
C save() 16 63 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
/**
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 MPDF 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
     * @throws \PhpOffice\PhpSpreadsheet\Exception
36
     */
37 1
    public function save($pFilename = null)
38
    {
39 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...
40
41
        //  Default PDF paper size
42 1
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
43
44
        //  Check for paper size and page orientation
45 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...
46
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
47
                == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
48
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
49
            $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...
50
        } else {
51 1
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
52 1
                == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
53 1
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
54 1
            $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...
55
        }
56 1
        $this->setOrientation($orientation);
57
58
        //  Override Page Orientation
59 1 View Code Duplication
        if (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 1
            $orientation = ($this->getOrientation() == \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_DEFAULT)
61
                ? \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT
62 1
                : $this->getOrientation();
63
        }
64 1
        $orientation = strtoupper($orientation);
65
66
        //  Override Paper Size
67 1
        if (null !== $this->getPaperSize()) {
68
            $printPaperSize = $this->getPaperSize();
69
        }
70
71 1
        if (isset(self::$paperSizes[$printPaperSize])) {
72 1
            $paperSize = self::$paperSizes[$printPaperSize];
73
        }
74
75
        //  Create PDF
76 1
        $pdf = new \mPDF();
77 1
        $ortmp = $orientation;
78 1
        $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
79 1
        $pdf->DefOrientation = $orientation;
80 1
        $pdf->AddPage($orientation);
81
82
        //  Document info
83 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
84 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
85 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
86 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
87 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
88
89 1
        $pdf->WriteHTML(
90 1
            $this->generateHTMLHeader(false) .
91 1
            $this->generateSheetData() .
92 1
            $this->generateHTMLFooter()
93
        );
94
95
        //  Write to file
96 1
        fwrite($fileHandle, $pdf->Output('', 'S'));
97
98 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...
99 1
    }
100
}
101