Skip to content

Commit e51dabe

Browse files
author
MarkBaker
committed
Merge branch 'master' into 2.0-Development
2 parents 304e2b8 + 54d82cd commit e51dabe

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4107,7 +4107,7 @@ parameters:
41074107

41084108
-
41094109
message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#"
4110-
count: 1
4110+
count: 2
41114111
path: src/PhpSpreadsheet/Worksheet/Worksheet.php
41124112

41134113
-

src/PhpSpreadsheet/Shared/Font.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public static function calculateColumnWidth(
229229
$cellText = '',
230230
$rotation = 0,
231231
?FontStyle $defaultFont = null,
232-
bool $filterAdjustment = false
232+
bool $filterAdjustment = false,
233+
int $indentAdjustment = 0
233234
): int {
234235
// If it is rich text, use plain text
235236
if ($cellText instanceof RichText) {
@@ -248,12 +249,12 @@ public static function calculateColumnWidth(
248249
}
249250

250251
// Try to get the exact text width in pixels
251-
$approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
252+
$approximate = self::$autoSizeMethod === self::AUTOSIZE_METHOD_APPROX;
252253
$columnWidth = 0;
253254
if (!$approximate) {
254255
$columnWidthAdjust = ceil(
255256
self::getTextWidthPixelsExact(
256-
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
257+
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
257258
$font,
258259
0
259260
) * 1.07
@@ -270,7 +271,7 @@ public static function calculateColumnWidth(
270271

271272
if ($approximate) {
272273
$columnWidthAdjust = self::getTextWidthPixelsApprox(
273-
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
274+
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
274275
$font,
275276
0
276277
);

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,11 @@ public function calculateColumnWidths()
756756
//By default merged cells should be ignored
757757
$isMergedButProceed = false;
758758

759-
//The only exception is if it's a merge range value cell of a 'vertical' randge (1 column wide)
759+
//The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide)
760760
if ($isMerged && $cell->isMergeRangeValueCell()) {
761761
$range = $cell->getMergeRange();
762762
$rangeBoundaries = Coordinate::rangeDimension($range);
763-
if ($rangeBoundaries[0] == 1) {
763+
if ($rangeBoundaries[0] === 1) {
764764
$isMergedButProceed = true;
765765
}
766766
}
@@ -774,6 +774,8 @@ public function calculateColumnWidths()
774774
$filterAdjustment = true;
775775
}
776776

777+
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent();
778+
777779
// Calculated value
778780
// To formatted string
779781
$cellValue = NumberFormat::toFormattedString(
@@ -791,7 +793,8 @@ public function calculateColumnWidths()
791793
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
792794
->getAlignment()->getTextRotation(),
793795
$this->getParent()->getDefaultStyle()->getFont(),
794-
$filterAdjustment
796+
$filterAdjustment,
797+
$indentAdjustment
795798
)
796799
);
797800
}
@@ -3011,6 +3014,10 @@ public function getHashCode()
30113014
*/
30123015
public static function extractSheetTitle($range, $returnRange = false)
30133016
{
3017+
if ($range === null) {
3018+
return $returnRange ? [null, null] : null;
3019+
}
3020+
30143021
// Sheet title included?
30153022
if (($sep = strrpos($range, '!')) === false) {
30163023
return $returnRange ? ['', $range] : '';

tests/PhpSpreadsheetTests/Shared/FontTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,35 @@ public function testVerdanaRotation(): void
9999
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
100100
self::assertEquals(4, $width);
101101
}
102+
103+
/**
104+
* @dataProvider providerCalculateApproximateColumnWidth
105+
*/
106+
public function testCalculateApproximateColumnWidth(
107+
int $expectedWidth,
108+
StyleFont $font,
109+
string $text,
110+
int $rotation,
111+
StyleFont $defaultFont,
112+
bool $filter,
113+
int $indent
114+
): void {
115+
$columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent);
116+
self::assertEquals($expectedWidth, $columnWidth);
117+
}
118+
119+
public function providerCalculateApproximateColumnWidth(): array
120+
{
121+
return [
122+
[13, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
123+
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
124+
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
125+
[18, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
126+
[20, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
127+
[6, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
128+
[9, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
129+
[17, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
130+
[19, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
131+
];
132+
}
102133
}

tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testSaveLoadWithDrawingInComment(): void
8888
$sheet = $spreadsheet->getActiveSheet();
8989
$comment = $sheet->getComment('A1');
9090

91-
self::assertTrue($comment instanceof Comment);
91+
self::assertInstanceOf(Comment::class, $comment);
9292
self::assertTrue($comment->hasBackgroundImage());
9393
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
9494
self::assertEquals($comment->getBackgroundImage()->getWidth(), 178);
@@ -127,7 +127,7 @@ public function testDrawingInCommentImageFormatsConversions(): void
127127
$comment->setSizeAsBackgroundImage();
128128
self::assertEquals($comment->getWidth(), '112.5pt');
129129
self::assertEquals($comment->getHeight(), '112.5pt');
130-
self::assertTrue($comment instanceof Comment);
130+
self::assertInstanceOf(Comment::class, $comment);
131131
self::assertTrue($comment->hasBackgroundImage());
132132
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
133133
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_GIF);
@@ -144,7 +144,7 @@ public function testDrawingInCommentImageFormatsConversions(): void
144144
$comment->setSizeAsBackgroundImage();
145145
self::assertEquals($comment->getWidth(), '52.5pt');
146146
self::assertEquals($comment->getHeight(), '52.5pt');
147-
self::assertTrue($comment instanceof Comment);
147+
self::assertInstanceOf(Comment::class, $comment);
148148
self::assertTrue($comment->hasBackgroundImage());
149149
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
150150
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_BMP);
@@ -162,7 +162,7 @@ public function testDrawingInCommentImageFormatsConversions(): void
162162
$comment = $sheet->getComment('A1');
163163
self::assertEquals($comment->getWidth(), '112.5pt');
164164
self::assertEquals($comment->getHeight(), '112.5pt');
165-
self::assertTrue($comment instanceof Comment);
165+
self::assertInstanceOf(Comment::class, $comment);
166166
self::assertTrue($comment->hasBackgroundImage());
167167
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
168168
self::assertEquals($comment->getBackgroundImage()->getWidth(), 150);
@@ -173,7 +173,7 @@ public function testDrawingInCommentImageFormatsConversions(): void
173173
$comment = $sheet->getComment('A2');
174174
self::assertEquals($comment->getWidth(), '52.5pt');
175175
self::assertEquals($comment->getHeight(), '52.5pt');
176-
self::assertTrue($comment instanceof Comment);
176+
self::assertInstanceOf(Comment::class, $comment);
177177
self::assertTrue($comment->hasBackgroundImage());
178178
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
179179
self::assertEquals($comment->getBackgroundImage()->getWidth(), 70);
@@ -208,7 +208,7 @@ public function testBuildWithDifferentImageFormats(): void
208208
self::assertEquals($comment->getHeight(), '75pt');
209209

210210
$comment = $sheet->getComment('A1');
211-
self::assertTrue($comment instanceof Comment);
211+
self::assertInstanceOf(Comment::class, $comment);
212212
self::assertTrue($comment->hasBackgroundImage());
213213
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
214214
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_PNG);
@@ -227,7 +227,7 @@ public function testBuildWithDifferentImageFormats(): void
227227
self::assertEquals($comment->getHeight(), '112.5pt');
228228

229229
$comment = $sheet->getComment('A2');
230-
self::assertTrue($comment instanceof Comment);
230+
self::assertInstanceOf(Comment::class, $comment);
231231
self::assertTrue($comment->hasBackgroundImage());
232232
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
233233
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_GIF);
@@ -246,7 +246,7 @@ public function testBuildWithDifferentImageFormats(): void
246246
self::assertEquals($comment->getHeight(), '37.5pt');
247247

248248
$comment = $sheet->getComment('A3');
249-
self::assertTrue($comment instanceof Comment);
249+
self::assertInstanceOf(Comment::class, $comment);
250250
self::assertTrue($comment->hasBackgroundImage());
251251
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
252252
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_JPEG);
@@ -265,7 +265,7 @@ public function testBuildWithDifferentImageFormats(): void
265265
self::assertEquals($comment->getHeight(), '52.5pt');
266266

267267
$comment = $sheet->getComment('A4');
268-
self::assertTrue($comment instanceof Comment);
268+
self::assertInstanceOf(Comment::class, $comment);
269269
self::assertTrue($comment->hasBackgroundImage());
270270
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
271271
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_BMP);
@@ -284,7 +284,7 @@ public function testBuildWithDifferentImageFormats(): void
284284
self::assertEquals($comment->getHeight(), '52.5pt');
285285

286286
$comment = $sheet->getComment('A5');
287-
self::assertTrue($comment instanceof Comment);
287+
self::assertInstanceOf(Comment::class, $comment);
288288
self::assertTrue($comment->hasBackgroundImage());
289289
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
290290
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_BMP);
@@ -304,7 +304,7 @@ public function testBuildWithDifferentImageFormats(): void
304304
self::assertEquals($comment->getHeight(), '52.5pt');
305305

306306
$comment = $sheet->getComment('A6');
307-
self::assertTrue($comment instanceof Comment);
307+
self::assertInstanceOf(Comment::class, $comment);
308308
self::assertTrue($comment->hasBackgroundImage());
309309
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
310310
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_BMP);
@@ -317,7 +317,7 @@ public function testBuildWithDifferentImageFormats(): void
317317
self::assertStringContainsString('purple_square.tiff', $drawing->getFilename());
318318
self::assertFalse($drawing->getIsUrl());
319319
$comment = $sheet->getComment('A7');
320-
self::assertTrue($comment instanceof Comment);
320+
self::assertInstanceOf(Comment::class, $comment);
321321
self::assertFalse($comment->hasBackgroundImage());
322322
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
323323
self::assertEquals($comment->getBackgroundImage()->getType(), IMAGETYPE_UNKNOWN);
@@ -326,39 +326,39 @@ public function testBuildWithDifferentImageFormats(): void
326326
$comment->setBackgroundImage($drawing);
327327
self::fail('Should throw exception when attempting to add tiff');
328328
} catch (PhpSpreadsheetException $e) {
329-
self::assertTrue($e instanceof PhpSpreadsheetException);
329+
self::assertInstanceOf(PhpSpreadsheetException::class, $e);
330330
self::assertEquals($e->getMessage(), 'Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
331331
}
332332

333333
try {
334334
$drawing->getImageTypeForSave();
335335
self::fail('Should throw exception when attempting to get image type for tiff');
336336
} catch (PhpSpreadsheetException $e) {
337-
self::assertTrue($e instanceof PhpSpreadsheetException);
337+
self::assertInstanceOf(PhpSpreadsheetException::class, $e);
338338
self::assertEquals($e->getMessage(), 'Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
339339
}
340340

341341
try {
342342
$drawing->getMediaFilename();
343343
self::fail('Should throw exception when attempting to get media file name for tiff');
344344
} catch (PhpSpreadsheetException $e) {
345-
self::assertTrue($e instanceof PhpSpreadsheetException);
345+
self::assertInstanceOf(PhpSpreadsheetException::class, $e);
346346
self::assertEquals($e->getMessage(), 'Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
347347
}
348348

349349
try {
350350
$drawing->getImageFileExtensionForSave();
351351
self::fail('Should throw exception when attempting to get image file extention for tiff');
352352
} catch (PhpSpreadsheetException $e) {
353-
self::assertTrue($e instanceof PhpSpreadsheetException);
353+
self::assertInstanceOf(PhpSpreadsheetException::class, $e);
354354
self::assertEquals($e->getMessage(), 'Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
355355
}
356356

357357
try {
358358
$drawing->getImageMimeType();
359359
self::fail('Should throw exception when attempting to get image mime type for tiff');
360360
} catch (PhpSpreadsheetException $e) {
361-
self::assertTrue($e instanceof PhpSpreadsheetException);
361+
self::assertInstanceOf(PhpSpreadsheetException::class, $e);
362362
self::assertEquals($e->getMessage(), 'Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
363363
}
364364

@@ -375,7 +375,7 @@ public function testBuildWithDifferentImageFormats(): void
375375
$comment = $sheet->getComment('A1');
376376
self::assertEquals($comment->getWidth(), '75pt');
377377
self::assertEquals($comment->getHeight(), '75pt');
378-
self::assertTrue($comment instanceof Comment);
378+
self::assertInstanceOf(Comment::class, $comment);
379379
self::assertTrue($comment->hasBackgroundImage());
380380
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
381381
self::assertEquals($comment->getBackgroundImage()->getWidth(), 100);
@@ -386,7 +386,7 @@ public function testBuildWithDifferentImageFormats(): void
386386
$comment = $sheet->getComment('A2');
387387
self::assertEquals($comment->getWidth(), '112.5pt');
388388
self::assertEquals($comment->getHeight(), '112.5pt');
389-
self::assertTrue($comment instanceof Comment);
389+
self::assertInstanceOf(Comment::class, $comment);
390390
self::assertTrue($comment->hasBackgroundImage());
391391
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
392392
self::assertEquals($comment->getBackgroundImage()->getWidth(), 150);
@@ -397,7 +397,7 @@ public function testBuildWithDifferentImageFormats(): void
397397
$comment = $sheet->getComment('A3');
398398
self::assertEquals($comment->getWidth(), '37.5pt');
399399
self::assertEquals($comment->getHeight(), '37.5pt');
400-
self::assertTrue($comment instanceof Comment);
400+
self::assertInstanceOf(Comment::class, $comment);
401401
self::assertTrue($comment->hasBackgroundImage());
402402
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
403403
self::assertEquals($comment->getBackgroundImage()->getWidth(), 50);
@@ -408,7 +408,7 @@ public function testBuildWithDifferentImageFormats(): void
408408
$comment = $sheet->getComment('A4');
409409
self::assertEquals($comment->getWidth(), '52.5pt');
410410
self::assertEquals($comment->getHeight(), '52.5pt');
411-
self::assertTrue($comment instanceof Comment);
411+
self::assertInstanceOf(Comment::class, $comment);
412412
self::assertTrue($comment->hasBackgroundImage());
413413
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
414414
self::assertEquals($comment->getBackgroundImage()->getWidth(), 70);
@@ -419,7 +419,7 @@ public function testBuildWithDifferentImageFormats(): void
419419
$comment = $sheet->getComment('A5');
420420
self::assertEquals($comment->getWidth(), '52.5pt');
421421
self::assertEquals($comment->getHeight(), '52.5pt');
422-
self::assertTrue($comment instanceof Comment);
422+
self::assertInstanceOf(Comment::class, $comment);
423423
self::assertTrue($comment->hasBackgroundImage());
424424
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
425425
self::assertEquals($comment->getBackgroundImage()->getWidth(), 70);
@@ -430,7 +430,7 @@ public function testBuildWithDifferentImageFormats(): void
430430
$comment = $sheet->getComment('A6');
431431
self::assertEquals($comment->getWidth(), '52.5pt');
432432
self::assertEquals($comment->getHeight(), '52.5pt');
433-
self::assertTrue($comment instanceof Comment);
433+
self::assertInstanceOf(Comment::class, $comment);
434434
self::assertTrue($comment->hasBackgroundImage());
435435
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
436436
self::assertEquals($comment->getBackgroundImage()->getWidth(), 70);
@@ -439,7 +439,7 @@ public function testBuildWithDifferentImageFormats(): void
439439

440440
// Check seventh image in comment background
441441
$comment = $sheet->getComment('A7');
442-
self::assertTrue($comment instanceof Comment);
442+
self::assertInstanceOf(Comment::class, $comment);
443443
self::assertFalse($comment->hasBackgroundImage());
444444
self::assertTrue($comment->getBackgroundImage() instanceof Drawing);
445445
self::assertEquals($comment->getBackgroundImage()->getWidth(), 0);

0 commit comments

Comments
 (0)