Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

TranslationsSheetCoordinates   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 188
Duplicated Lines 15.96 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 30
loc 188
ccs 76
cts 84
cp 0.9048
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setSheetId() 0 6 1
A setSheetTitle() 0 6 1
A emptySheet() 0 11 1
A sheetWithData() 0 11 1
A headerShortRange() 0 4 1
A headerRange() 10 10 1
A fullKeyColumnRange() 10 10 1
A metaColumnsRange() 0 10 1
A dataShortRange() 0 8 2
A dataRange() 10 10 1
A translationsRange() 0 10 2
A getColumnsCount() 0 4 1
A getRowsCount() 0 4 1
A getLocalesCount() 0 4 1
A namespaceColumnIndex() 0 4 1
A groupColumnIndex() 0 4 1
A keyColumnIndex() 0 4 1
A sourceFileColumnIndex() 0 4 1
A stringFromColumnIndex() 0 16 5

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 Nikaia\TranslationSheet\Sheet;
4
5
class TranslationsSheetCoordinates
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    protected $headerRowsCount = 1;
8
9
    protected $dataRowsCount = 0;
10
11
    protected $columnsCount = 0;
12
13
    protected $localesCount = 1;
14
15
    protected $sheetId;
16
17
    protected $sheetTitle;
18
19 8
    private function __construct()
20
    {
21 8
    }
22
23 3
    public function setSheetId($sheetId)
24
    {
25 3
        $this->sheetId = $sheetId;
26
27 3
        return $this;
28
    }
29
30 3
    public function setSheetTitle($sheetTitle)
31
    {
32 3
        $this->sheetTitle = $sheetTitle;
33
34 3
        return $this;
35
    }
36
37 6
    public static function emptySheet($columnsCount, $localesCount, $headerRowsCount)
38
    {
39 6
        $instance = new self;
40
41 6
        $instance->headerRowsCount = $headerRowsCount;
42 6
        $instance->dataRowsCount = 10;
43 6
        $instance->columnsCount = $columnsCount;
44 6
        $instance->localesCount = $localesCount;
45
46 6
        return $instance;
47
    }
48
49 2
    public static function sheetWithData($dataRowsCount, $columnsCount, $localesCount, $headerRowsCount)
50
    {
51 2
        $instance = new self;
52
53 2
        $instance->headerRowsCount = $headerRowsCount;
54 2
        $instance->dataRowsCount = $dataRowsCount;
55 2
        $instance->columnsCount = $columnsCount;
56 2
        $instance->localesCount = $localesCount;
57
58 2
        return $instance;
59
    }
60
61
    public function headerShortRange()
62
    {
63
        return $this->sheetTitle.'!A1:'.self::stringFromColumnIndex($this->getColumnsCount()).'1';
64
    }
65
66 1 View Code Duplication
    public function headerRange()
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...
67
    {
68
        return [
69 1
            'sheetId' => $this->sheetId,
70 1
            'startRowIndex' => 0,
71 1
            'endRowIndex' => 1,
72 1
            'startColumnIndex' => 0,
73 1
            'endColumnIndex' => $this->getColumnsCount(),
74
        ];
75
    }
76
77 1 View Code Duplication
    public function fullKeyColumnRange()
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...
78
    {
79
        return [
80 1
            'sheetId' => $this->sheetId,
81 1
            'startRowIndex' => 1,
82 1
            'endRowIndex' => $this->getRowsCount(),
83 1
            'startColumnIndex' => 0,
84 1
            'endColumnIndex' => 1,
85
        ];
86
    }
87
88 1
    public function metaColumnsRange()
89
    {
90
        return [
91 1
            'sheetId' => $this->sheetId,
92 1
            'startRowIndex' => 1,
93 1
            'endRowIndex' => $this->getRowsCount(),
94 1
            'startColumnIndex' => $this->getLocalesCount() + 1,
95 1
            'endColumnIndex' => $this->getColumnsCount(),
96
        ];
97
    }
98
99 2
    public function dataShortRange($firstRow = 2, $noLastRow = false)
100
    {
101 2
        $firstColumn = 'A';
102 2
        $lastColumn = self::stringFromColumnIndex($this->getColumnsCount());
103 2
        $lastRow = $this->getRowsCount();
104
105 2
        return $this->sheetTitle.'!'.$firstColumn.$firstRow.':'.$lastColumn.($noLastRow ? '' : $lastRow);
106
    }
107
108 View Code Duplication
    public function dataRange($endRow, $firstRow = 2)
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...
109
    {
110
        return [
111
            'sheetId' => $this->sheetId,
112
            'startRowIndex' => $firstRow,
113
            'endRowIndex' => $endRow,
114
            'startColumnIndex' => 0,
115
            'endColumnIndex' => $this->getColumnsCount(),
116
        ];
117
    }
118
119 3
    public function translationsRange($firstColumn = 1, $rowCount = null)
120
    {
121
        return [
122 3
            'sheetId' => $this->sheetId,
123 3
            'startRowIndex' => 1,
124 3
            'endRowIndex' => $rowCount ?: $this->getRowsCount(),
125 3
            'startColumnIndex' => $firstColumn,
126 3
            'endColumnIndex' => $firstColumn + $this->getLocalesCount(),
127
        ];
128
    }
129
130 3
    public function getColumnsCount()
131
    {
132 3
        return $this->columnsCount;
133
    }
134
135 5
    public function getRowsCount()
136
    {
137 5
        return $this->dataRowsCount + $this->headerRowsCount;
138
    }
139
140 3
    public function getLocalesCount()
141
    {
142 3
        return $this->localesCount;
143
    }
144
145 1
    public function namespaceColumnIndex()
146
    {
147 1
        return $this->getLocalesCount() + 1;
148
    }
149
150 1
    public function groupColumnIndex()
151
    {
152 1
        return $this->getLocalesCount() + 2;
153
    }
154
155 1
    public function keyColumnIndex()
156
    {
157 1
        return $this->getLocalesCount() + 3;
158
    }
159
160 1
    public function sourceFileColumnIndex()
161
    {
162 1
        return $this->getLocalesCount() + 4;
163
    }
164
165
    /**
166
     * String from column index.
167
     *
168
     * @see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Cell/Coordinate.php Source of implementation.
169
     *
170
     * @license https://raw.githubusercontent.com/PHPOffice/PhpSpreadsheet/master/LICENSE LGPL (GNU LESSER GENERAL PUBLIC LICENSE)
171
     *
172
     * @param int $columnIndex Column index (A = 1)
173
     *
174
     * @return string
175
     */
176 2
    public static function stringFromColumnIndex($columnIndex)
177
    {
178 2
        static $indexCache = [];
179 2
        if (! isset($indexCache[$columnIndex])) {
180 1
            $indexValue = $columnIndex;
181 1
            $base26 = null;
182
            do {
183 1
                $characterValue = ($indexValue % 26) ?: 26;
184 1
                $indexValue = ($indexValue - $characterValue) / 26;
185 1
                $base26 = chr($characterValue + 64).($base26 ?: '');
186 1
            } while ($indexValue > 0);
187 1
            $indexCache[$columnIndex] = $base26;
188
        }
189
190 2
        return $indexCache[$columnIndex];
191
    }
192
}
193