Passed
Push — feature/53-multi-sheet-writing ( 9533f7...61d6ab )
by Stefan
06:26 queued 02:06
created

Workbook   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 20.45 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 18
loc 88
ccs 28
cts 28
cp 1
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 16 4

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