Completed
Push — master ( 815517...75567a )
by Nassif
13:36
created

TranslationsSheetCoordinates   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 187
Duplicated Lines 16.04 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 30
loc 187
ccs 0
cts 135
cp 0
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 15 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
    private function __construct()
20
    {
21
    }
22
23
    public function setSheetId($sheetId)
24
    {
25
        $this->sheetId = $sheetId;
26
27
        return $this;
28
    }
29
30
    public function setSheetTitle($sheetTitle)
31
    {
32
        $this->sheetTitle = $sheetTitle;
33
34
        return $this;
35
    }
36
37
    public static function emptySheet($columnsCount, $localesCount, $headerRowsCount)
38
    {
39
        $instance = new self;
40
41
        $instance->headerRowsCount = $headerRowsCount;
42
        $instance->dataRowsCount = 10;
43
        $instance->columnsCount = $columnsCount;
44
        $instance->localesCount = $localesCount;
45
46
        return $instance;
47
    }
48
49
    public static function sheetWithData($dataRowsCount, $columnsCount, $localesCount, $headerRowsCount)
50
    {
51
        $instance = new self;
52
53
        $instance->headerRowsCount = $headerRowsCount;
54
        $instance->dataRowsCount = $dataRowsCount;
55
        $instance->columnsCount = $columnsCount;
56
        $instance->localesCount = $localesCount;
57
58
        return $instance;
59
    }
60
61
    public function headerShortRange()
62
    {
63
        return $this->sheetTitle.'!A1:'.self::stringFromColumnIndex($this->getColumnsCount()).'1';
64
    }
65
66 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
            'sheetId' => $this->sheetId,
70
            'startRowIndex' => 0,
71
            'endRowIndex' => 1,
72
            'startColumnIndex' => 0,
73
            'endColumnIndex' => $this->getColumnsCount(),
74
        ];
75
    }
76
77 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
            'sheetId' => $this->sheetId,
81
            'startRowIndex' => 1,
82
            'endRowIndex' => $this->getRowsCount(),
83
            'startColumnIndex' => 0,
84
            'endColumnIndex' => 1,
85
        ];
86
    }
87
88
    public function metaColumnsRange()
89
    {
90
        return [
91
            'sheetId' => $this->sheetId,
92
            'startRowIndex' => 1,
93
            'endRowIndex' => $this->getRowsCount(),
94
            'startColumnIndex' => $this->getLocalesCount() + 1,
95
            'endColumnIndex' => $this->getColumnsCount(),
96
        ];
97
    }
98
99
    public function dataShortRange($firstRow = 2, $noLastRow = false)
100
    {
101
        $firstColumn = 'A';
102
        $lastColumn = self::stringFromColumnIndex($this->getColumnsCount());
103
        $lastRow = $this->getRowsCount();
104
105
        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
    public function translationsRange($firstColumn = 1, $rowCount = null)
120
    {
121
        return [
122
            'sheetId' => $this->sheetId,
123
            'startRowIndex' => 1,
124
            'endRowIndex' => $rowCount ?: $this->getRowsCount(),
125
            'startColumnIndex' => $firstColumn,
126
            'endColumnIndex' => $firstColumn + $this->getLocalesCount(),
127
        ];
128
    }
129
130
    public function getColumnsCount()
131
    {
132
        return $this->columnsCount;
133
    }
134
135
    public function getRowsCount()
136
    {
137
        return $this->dataRowsCount + $this->headerRowsCount;
138
    }
139
140
    public function getLocalesCount()
141
    {
142
        return $this->localesCount;
143
    }
144
145
    public function namespaceColumnIndex()
146
    {
147
        return $this->getLocalesCount() + 1;
148
    }
149
150
    public function groupColumnIndex()
151
    {
152
        return $this->getLocalesCount() + 2;
153
    }
154
155
    public function keyColumnIndex()
156
    {
157
        return $this->getLocalesCount() + 3;
158
    }
159
160
    public function sourceFileColumnIndex()
161
    {
162
        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
    public static function stringFromColumnIndex($columnIndex)
177
    {
178
        static $indexCache = [];
179
        if (!isset($indexCache[$columnIndex])) {
180
            $indexValue = $columnIndex;
181
            $base26 = null;
182
            do {
183
                $characterValue = ($indexValue % 26) ?: 26;
184
                $indexValue = ($indexValue - $characterValue) / 26;
185
                $base26 = chr($characterValue + 64) . ($base26 ?: '');
186
            } while ($indexValue > 0);
187
            $indexCache[$columnIndex] = $base26;
188
        }
189
        return $indexCache[$columnIndex];
190
    }
191
}
192