Test Failed
Push — feature/53-multi-sheet-writing ( 58a074 )
by Stefan
07:08 queued 14s
created

Workbook   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 18
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setPrintTitleRange() 0 7 4
A getWorkbookXml() 0 7 1
A getWorkbookRelsXml() 9 9 2
A getSheetsXml() 9 9 2
A getDefinedNamesXml() 0 13 3

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 OneSheet;
4
5
use OneSheet\Xml\DefaultXml;
6
use OneSheet\Xml\WorkbookXml;
7
8
class Workbook
9
{
10
    /**
11
     * @var int
12
     */
13
    private $printTitleStart;
14
15
    /**
16
     * @var int
17
     */
18
    private $printTitleEnd;
19
20
    /**
21
     * Set the range of rows to repeat when printing the excel file.
22
     * $startRow=1 and $endRow=1 will repeat the first row only.
23
     *
24
     * @param int $startRow
25
     * @param int $endRow
26
     */
27
    public function setPrintTitleRange($startRow, $endRow)
28
    {
29
        if ($startRow <= $endRow && is_numeric($startRow) && is_numeric($endRow)) {
30
            $this->printTitleStart = (int)$startRow;
31
            $this->printTitleEnd = (int)$endRow;
32
        }
33
    }
34
35
    /**
36
     * @param array $sheetIds
37
     * @return string
38
     */
39
    public function getWorkbookXml(array $sheetIds)
40
    {
41
        $sheets = $this->getSheetsXml($sheetIds);
42
        $definedNames = $this->getDefinedNamesXml();
43
44
        return sprintf(WorkbookXml::WORKBOOK_XML, $sheets, $definedNames);
45
    }
46
47
    /**
48
     * @param array $sheetIds
49
     * @return string
50
     */
51 View Code Duplication
    public function getWorkbookRelsXml($sheetIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
    {
53
        $sheets = '';
54
        foreach ($sheetIds as $key => $sheetId) {
55
            $sheets .= sprintf(WorkbookXml::WORKBOOK_REL_XML, $key + 1, $sheetId);
56
        }
57
58
        return sprintf(WorkbookXml::WORKBOOK_RELS_XML, $sheets);
59
    }
60
61
    /**
62
     * @param array $sheetIds
63
     * @return string
64
     */
65 View Code Duplication
    private function getSheetsXml($sheetIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67
        $sheets = '';
68
        foreach ($sheetIds as $key => $sheetId) {
69
            $sheets .= sprintf(WorkbookXml::WORKBOOK_SHEETS_XML, $sheetId, $key + 1, $key + 1);
70
        }
71
72
        return $sheets;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    private function getDefinedNamesXml()
79
    {
80
        $definedNames = '';
81
        if ($this->printTitleStart && $this->printTitleEnd) {
82
            $definedNames = sprintf(
83
                WorkbookXml::DEFINED_NAMES_XML,
84
                $this->printTitleStart,
85
                $this->printTitleEnd
86
            );
87
        }
88
89
        return $definedNames;
90
    }
91
}
92