Failed Conditions
Push — master ( 42761f...d02352 )
by Adrien
16:26 queued 08:32
created

Xls::oneAnchor2twoAnchor()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 66
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 9.1706

Importance

Changes 0
Metric Value
cc 9
eloc 39
nc 80
nop 6
dl 0
loc 66
ccs 34
cts 39
cp 0.8718
crap 9.1706
rs 7.7404
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
8
class Xls
9
{
10
    /**
11
     * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
12
     * x is the width in intrinsic Excel units (measuring width in number of normal characters)
13
     * This holds for Arial 10.
14
     *
15
     * @param Worksheet $sheet The sheet
16
     * @param string $col The column
17
     *
18
     * @return int The width in pixels
19
     */
20 10
    public static function sizeCol($sheet, $col = 'A')
21
    {
22
        // default font of the workbook
23 10
        $font = $sheet->getParent()->getDefaultStyle()->getFont();
24
25 10
        $columnDimensions = $sheet->getColumnDimensions();
26
27
        // first find the true column width in pixels (uncollapsed and unhidden)
28 10
        if (isset($columnDimensions[$col]) && $columnDimensions[$col]->getWidth() != -1) {
29
            // then we have column dimension with explicit width
30 9
            $columnDimension = $columnDimensions[$col];
31 9
            $width = $columnDimension->getWidth();
32 9
            $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
33 10
        } elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
34
            // then we have default column dimension with explicit width
35 1
            $defaultColumnDimension = $sheet->getDefaultColumnDimension();
36 1
            $width = $defaultColumnDimension->getWidth();
37 1
            $pixelWidth = Drawing::cellDimensionToPixels($width, $font);
38
        } else {
39
            // we don't even have any default column dimension. Width depends on default font
40 9
            $pixelWidth = Font::getDefaultColumnWidthByFont($font, true);
41
        }
42
43
        // now find the effective column width in pixels
44 10
        if (isset($columnDimensions[$col]) && !$columnDimensions[$col]->getVisible()) {
45
            $effectivePixelWidth = 0;
46
        } else {
47 10
            $effectivePixelWidth = $pixelWidth;
48
        }
49
50 10
        return $effectivePixelWidth;
51
    }
52
53
    /**
54
     * Convert the height of a cell from user's units to pixels. By interpolation
55
     * the relationship is: y = 4/3x. If the height hasn't been set by the user we
56
     * use the default value. If the row is hidden we use a value of zero.
57
     *
58
     * @param Worksheet $sheet The sheet
59
     * @param int $row The row index (1-based)
60
     *
61
     * @return int The width in pixels
62
     */
63 10
    public static function sizeRow($sheet, $row = 1)
64
    {
65
        // default font of the workbook
66 10
        $font = $sheet->getParent()->getDefaultStyle()->getFont();
67
68 10
        $rowDimensions = $sheet->getRowDimensions();
69
70
        // first find the true row height in pixels (uncollapsed and unhidden)
71 10
        if (isset($rowDimensions[$row]) && $rowDimensions[$row]->getRowHeight() != -1) {
72
            // then we have a row dimension
73 2
            $rowDimension = $rowDimensions[$row];
74 2
            $rowHeight = $rowDimension->getRowHeight();
75 2
            $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
76 10
        } elseif ($sheet->getDefaultRowDimension()->getRowHeight() != -1) {
77
            // then we have a default row dimension with explicit height
78 3
            $defaultRowDimension = $sheet->getDefaultRowDimension();
79 3
            $rowHeight = $defaultRowDimension->getRowHeight();
80 3
            $pixelRowHeight = Drawing::pointsToPixels($rowHeight);
0 ignored issues
show
Bug introduced by
$rowHeight of type double is incompatible with the type integer expected by parameter $pValue of PhpOffice\PhpSpreadsheet...awing::pointsToPixels(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $pixelRowHeight = Drawing::pointsToPixels(/** @scrutinizer ignore-type */ $rowHeight);
Loading history...
81
        } else {
82
            // we don't even have any default row dimension. Height depends on default font
83 7
            $pointRowHeight = Font::getDefaultRowHeightByFont($font);
84 7
            $pixelRowHeight = Font::fontSizeToPixels($pointRowHeight);
0 ignored issues
show
Bug introduced by
$pointRowHeight of type double is incompatible with the type integer expected by parameter $fontSizeInPoints of PhpOffice\PhpSpreadsheet...ont::fontSizeToPixels(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $pixelRowHeight = Font::fontSizeToPixels(/** @scrutinizer ignore-type */ $pointRowHeight);
Loading history...
85
        }
86
87
        // now find the effective row height in pixels
88 10
        if (isset($rowDimensions[$row]) && !$rowDimensions[$row]->getVisible()) {
89
            $effectivePixelRowHeight = 0;
90
        } else {
91 10
            $effectivePixelRowHeight = $pixelRowHeight;
92
        }
93
94 10
        return $effectivePixelRowHeight;
95
    }
96
97
    /**
98
     * Get the horizontal distance in pixels between two anchors
99
     * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets.
100
     *
101
     * @param string $startColumn
102
     * @param int $startOffsetX Offset within start cell measured in 1/1024 of the cell width
103
     * @param string $endColumn
104
     * @param int $endOffsetX Offset within end cell measured in 1/1024 of the cell width
105
     *
106
     * @return int Horizontal measured in pixels
107
     */
108 6
    public static function getDistanceX(Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
109
    {
110 6
        $distanceX = 0;
111
112
        // add the widths of the spanning columns
113 6
        $startColumnIndex = Coordinate::columnIndexFromString($startColumn);
114 6
        $endColumnIndex = Coordinate::columnIndexFromString($endColumn);
115 6
        for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
116 6
            $distanceX += self::sizeCol($sheet, Coordinate::stringFromColumnIndex($i));
117
        }
118
119
        // correct for offsetX in startcell
120 6
        $distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
121
122
        // correct for offsetX in endcell
123 6
        $distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
124
125 6
        return $distanceX;
126
    }
127
128
    /**
129
     * Get the vertical distance in pixels between two anchors
130
     * The distanceY is found as sum of all the spanning rows minus two offsets.
131
     *
132
     * @param int $startRow (1-based)
133
     * @param int $startOffsetY Offset within start cell measured in 1/256 of the cell height
134
     * @param int $endRow (1-based)
135
     * @param int $endOffsetY Offset within end cell measured in 1/256 of the cell height
136
     *
137
     * @return int Vertical distance measured in pixels
138
     */
139 6
    public static function getDistanceY(Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
140
    {
141 6
        $distanceY = 0;
142
143
        // add the widths of the spanning rows
144 6
        for ($row = $startRow; $row <= $endRow; ++$row) {
145 6
            $distanceY += self::sizeRow($sheet, $row);
146
        }
147
148
        // correct for offsetX in startcell
149 6
        $distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256);
150
151
        // correct for offsetX in endcell
152 6
        $distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
153
154 6
        return $distanceY;
155
    }
156
157
    /**
158
     * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
159
     * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications.
160
     *
161
     * Calculate the vertices that define the position of the image as required by
162
     * the OBJ record.
163
     *
164
     *         +------------+------------+
165
     *         |     A      |      B     |
166
     *   +-----+------------+------------+
167
     *   |     |(x1,y1)     |            |
168
     *   |  1  |(A1)._______|______      |
169
     *   |     |    |              |     |
170
     *   |     |    |              |     |
171
     *   +-----+----|    BITMAP    |-----+
172
     *   |     |    |              |     |
173
     *   |  2  |    |______________.     |
174
     *   |     |            |        (B2)|
175
     *   |     |            |     (x2,y2)|
176
     *   +---- +------------+------------+
177
     *
178
     * Example of a bitmap that covers some of the area from cell A1 to cell B2.
179
     *
180
     * Based on the width and height of the bitmap we need to calculate 8 vars:
181
     *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
182
     * The width and height of the cells are also variable and have to be taken into
183
     * account.
184
     * The values of $col_start and $row_start are passed in from the calling
185
     * function. The values of $col_end and $row_end are calculated by subtracting
186
     * the width and height of the bitmap from the width and height of the
187
     * underlying cells.
188
     * The vertices are expressed as a percentage of the underlying cell width as
189
     * follows (rhs values are in pixels):
190
     *
191
     *       x1 = X / W *1024
192
     *       y1 = Y / H *256
193
     *       x2 = (X-1) / W *1024
194
     *       y2 = (Y-1) / H *256
195
     *
196
     *       Where:  X is distance from the left side of the underlying cell
197
     *               Y is distance from the top of the underlying cell
198
     *               W is the width of the cell
199
     *               H is the height of the cell
200
     *
201
     * @param Worksheet $sheet
202
     * @param string $coordinates E.g. 'A1'
203
     * @param int $offsetX Horizontal offset in pixels
204
     * @param int $offsetY Vertical offset in pixels
205
     * @param int $width Width in pixels
206
     * @param int $height Height in pixels
207
     *
208
     * @return array
209
     */
210 9
    public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
211
    {
212 9
        [$col_start, $row] = Coordinate::indexesFromString($coordinates);
213 9
        $row_start = $row - 1;
214
215 9
        $x1 = $offsetX;
216 9
        $y1 = $offsetY;
217
218
        // Initialise end cell to the same as the start cell
219 9
        $col_end = $col_start; // Col containing lower right corner of object
220 9
        $row_end = $row_start; // Row containing bottom right corner of object
221
222
        // Zero the specified offset if greater than the cell dimensions
223 9
        if ($x1 >= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start))) {
224 2
            $x1 = 0;
225
        }
226 9
        if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
227
            $y1 = 0;
228
        }
229
230 9
        $width = $width + $x1 - 1;
231 9
        $height = $height + $y1 - 1;
232
233
        // Subtract the underlying cell widths to find the end cell of the image
234 9
        while ($width >= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end))) {
235 7
            $width -= self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end));
236 7
            ++$col_end;
237
        }
238
239
        // Subtract the underlying cell heights to find the end cell of the image
240 9
        while ($height >= self::sizeRow($sheet, $row_end + 1)) {
241 9
            $height -= self::sizeRow($sheet, $row_end + 1);
242 9
            ++$row_end;
243
        }
244
245
        // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
246
        // with zero height or width.
247 9
        if (self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start)) == 0) {
248
            return;
249
        }
250 9
        if (self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end)) == 0) {
251
            return;
252
        }
253 9
        if (self::sizeRow($sheet, $row_start + 1) == 0) {
254
            return;
255
        }
256 9
        if (self::sizeRow($sheet, $row_end + 1) == 0) {
257
            return;
258
        }
259
260
        // Convert the pixel values to the percentage value expected by Excel
261 9
        $x1 = $x1 / self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_start)) * 1024;
262 9
        $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
263 9
        $x2 = ($width + 1) / self::sizeCol($sheet, Coordinate::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
264 9
        $y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object
265
266 9
        $startCoordinates = Coordinate::stringFromColumnIndex($col_start) . ($row_start + 1);
267 9
        $endCoordinates = Coordinate::stringFromColumnIndex($col_end) . ($row_end + 1);
268
269
        return [
270 9
            'startCoordinates' => $startCoordinates,
271 9
            'startOffsetX' => $x1,
272 9
            'startOffsetY' => $y1,
273 9
            'endCoordinates' => $endCoordinates,
274 9
            'endOffsetX' => $x2,
275 9
            'endOffsetY' => $y2,
276
        ];
277
    }
278
}
279