Passed
Push — feature/53-multi-sheet-writing ( 61d6ab...617449 )
by Stefan
02:17
created

Workbook::createSingleDefinedNameXml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Xml\WorkbookXml;
6
7
class Workbook
8
{
9
    /**
10
     * @var int[]
11
     */
12
    private $printTitleStarts;
13
14
    /**
15
     * @var int[]
16
     */
17
    private $printTitleEnds;
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
     * @param string $sheetName
26
     */
27 3
    public function setPrintTitleRange($startRow, $endRow, $sheetName)
28
    {
29 3
        if ($startRow <= $endRow && is_numeric($startRow) && is_numeric($endRow)) {
30 3
            $this->printTitleStarts[$sheetName] = (int)$startRow;
31 3
            $this->printTitleEnds[$sheetName] = (int)$endRow;
32
        }
33 3
    }
34
35
    /**
36
     * @param string[] $sheetNames
37
     * @return string
38
     */
39 7
    public function getWorkbookXml(array $sheetNames)
40
    {
41 7
        $sheets = $this->getSheetsXml($sheetNames);
42 7
        $definedNames = $this->getDefinedNamesXml($sheetNames);
43
44 7
        return sprintf(WorkbookXml::WORKBOOK_XML, $sheets, $definedNames);
45
    }
46
47
    /**
48
     * @param string[] $sheetNames
49
     * @return string
50
     */
51 4 View Code Duplication
    public function getWorkbookRelsXml($sheetNames)
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 4
        $sheets = '';
54 4
        foreach ($sheetNames as $key => $sheetName) {
55 4
            $sheets .= sprintf(WorkbookXml::WORKBOOK_REL_XML, $key + 1, $sheetName);
56
        }
57
58 4
        return sprintf(WorkbookXml::WORKBOOK_RELS_XML, $sheets);
59
    }
60
61
    /**
62
     * @param string[] $sheetNames
63
     * @return string
64
     */
65 7 View Code Duplication
    private function getSheetsXml($sheetNames)
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 7
        $sheets = '';
68 7
        foreach ($sheetNames as $key => $sheetName) {
69 7
            $sheets .= sprintf(WorkbookXml::WORKBOOK_SHEETS_XML, $sheetName, $key + 1, $key + 1);
70
        }
71
72 7
        return $sheets;
73
    }
74
75
    /**
76
     * @param string[] $sheetNames
77
     * @return string
78
     */
79 7
    private function getDefinedNamesXml(array $sheetNames)
80
    {
81 7
        $definedNames = '';
82 7
        if ($this->printTitleStarts && $this->printTitleEnds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->printTitleStarts of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $this->printTitleEnds of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83 2
            $definedNameTags = '';
84 2
            foreach ($sheetNames as $key => $sheetName) {
85 2
                $definedNameTags .= $this->createSingleDefinedNameXml($sheetName, $key);
86
            }
87 2
            $definedNames = sprintf(WorkbookXml::DEFINED_NAMES_XML, $definedNameTags);
88
        }
89
90 7
        return $definedNames;
91
    }
92
93
    /**
94
     * $localSheetId is the only one that has to start from 0 instead of 1.
95
     *
96
     * @param string $sheetName
97
     * @param int    $localSheetId
98
     * @return string
99
     */
100 2
    private function createSingleDefinedNameXml($sheetName, $localSheetId)
101
    {
102 2
        return isset($this->printTitleEnds[$sheetName])
103 2
            ? sprintf(
104 2
                WorkbookXml::DEFINED_NAME_XML,
105
                $localSheetId,
106
                $sheetName,
107 2
                $this->printTitleStarts[$sheetName],
108 2
                $this->printTitleEnds[$sheetName])
109 2
            : '';
110
    }
111
}
112