Skip to content

Commit 2f1092c

Browse files
authored
Merge pull request #4646 from oleibman/iocoverage
Minor Improvements to Reader/Writer Coverage
2 parents 996de17 + 9bb090d commit 2f1092c

39 files changed

+495
-72
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
- name: Setup PHP, with composer and extensions
8282
uses: shivammathur/setup-php@v2
8383
with:
84-
php-version: 8.3
84+
php-version: 8.4
8585
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
8686
coverage: none
8787

@@ -98,7 +98,7 @@ jobs:
9898
- name: Setup PHP, with composer and extensions
9999
uses: shivammathur/setup-php@v2
100100
with:
101-
php-version: 8.3
101+
php-version: 8.4
102102
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
103103
coverage: none
104104
tools: cs2pr
@@ -129,7 +129,7 @@ jobs:
129129
- name: Setup PHP, with composer and extensions
130130
uses: shivammathur/setup-php@v2
131131
with:
132-
php-version: 8.3
132+
php-version: 8.4
133133
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
134134
coverage: none
135135
tools: cs2pr
@@ -160,7 +160,7 @@ jobs:
160160
- name: Setup PHP, with composer and extensions
161161
uses: shivammathur/setup-php@v2
162162
with:
163-
php-version: 8.3
163+
php-version: 8.4
164164
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
165165
coverage: none
166166
tools: cs2pr
@@ -191,7 +191,7 @@ jobs:
191191
- name: Setup PHP, with composer and extensions
192192
uses: shivammathur/setup-php@v2
193193
with:
194-
php-version: 8.3
194+
php-version: 8.4
195195
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
196196
coverage: none
197197
tools: cs2pr
@@ -230,7 +230,7 @@ jobs:
230230
- name: Setup PHP, with composer and extensions
231231
uses: shivammathur/setup-php@v2
232232
with:
233-
php-version: 8.3
233+
php-version: 8.4
234234
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
235235
coverage: pcov
236236

-272 Bytes
Binary file not shown.

src/PhpSpreadsheet/Calculation/MathTrig/Round.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ public static function up($number, $digits): array|string|float
7777
);
7878
}
7979

80+
// @codeCoverageIgnoreStart
8081
if ($number < 0.0) {
8182
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
8283
}
8384

8485
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
86+
// @codeCoverageIgnoreEnd
8587
}
8688

8789
/**
@@ -121,11 +123,13 @@ public static function down($number, $digits): array|string|float
121123
);
122124
}
123125

126+
// @codeCoverageIgnoreStart
124127
if ($number < 0.0) {
125128
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
126129
}
127130

128131
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
132+
// @codeCoverageIgnoreEnd
129133
}
130134

131135
/**

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,13 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
727727
// Reload the HTML file into the DOM object
728728
try {
729729
$convert = $this->getSecurityScannerOrThrow()->scanFile($filename);
730-
$convert = self::replaceNonAsciiIfNeeded($convert);
730+
$convert = static::replaceNonAsciiIfNeeded($convert);
731731
$loaded = ($convert === null) ? false : $dom->loadHTML($convert);
732732
} catch (Throwable $e) {
733733
$loaded = false;
734734
}
735735
if ($loaded === false) {
736-
throw new Exception('Failed to load ' . $filename . ' as a DOM Document', 0, $e ?? null);
736+
throw new Exception('Failed to load file ' . $filename . ' as a DOM Document', 0, $e ?? null);
737737
}
738738
self::loadProperties($dom, $spreadsheet);
739739

@@ -821,7 +821,7 @@ private static function replaceNonAscii(array $matches): string
821821
return '&#' . mb_ord($matches[0], 'UTF-8') . ';';
822822
}
823823

824-
private static function replaceNonAsciiIfNeeded(string $convert): ?string
824+
protected static function replaceNonAsciiIfNeeded(string $convert): ?string
825825
{
826826
if (preg_match(self::STARTS_WITH_BOM, $convert) !== 1 && preg_match(self::DECLARES_CHARSET, $convert) !== 1) {
827827
$lowend = "\u{80}";
@@ -846,7 +846,7 @@ public function loadFromString(string $content, ?Spreadsheet $spreadsheet = null
846846
// Reload the HTML file into the DOM object
847847
try {
848848
$convert = $this->getSecurityScannerOrThrow()->scan($content);
849-
$convert = self::replaceNonAsciiIfNeeded($convert);
849+
$convert = static::replaceNonAsciiIfNeeded($convert);
850850
$loaded = ($convert === null) ? false : $dom->loadHTML($convert);
851851
} catch (Throwable $e) {
852852
$loaded = false;
@@ -1183,13 +1183,13 @@ private static function getStyleArray(array $attributes): array
11831183
if (substr($arrayValue, -2) === 'px') {
11841184
$arrayValue = (string) (((float) substr($arrayValue, 0, -2)));
11851185
} else {
1186-
$arrayValue = (new CssDimension($arrayValue))->width();
1186+
$arrayValue = (new CssDimension($arrayValue))->toUnit(CssDimension::UOM_PIXELS);
11871187
}
11881188
} elseif ($arrayKey === 'height') {
11891189
if (substr($arrayValue, -2) === 'px') {
11901190
$arrayValue = substr($arrayValue, 0, -2);
11911191
} else {
1192-
$arrayValue = (new CssDimension($arrayValue))->height();
1192+
$arrayValue = (new CssDimension($arrayValue))->toUnit(CssDimension::UOM_PIXELS);
11931193
}
11941194
}
11951195
$styleArray[$arrayKey] = $arrayValue;

src/PhpSpreadsheet/Writer/BaseWriter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,18 @@ public function openFileHandle($filename): void
123123
$this->shouldCloseFile = true;
124124
}
125125

126+
protected function tryClose(): bool
127+
{
128+
return fclose($this->fileHandle);
129+
}
130+
126131
/**
127132
* Close file handle only if we opened it ourselves.
128133
*/
129134
protected function maybeCloseFileHandle(): void
130135
{
131136
if ($this->shouldCloseFile) {
132-
if (!fclose($this->fileHandle)) {
137+
if (!$this->tryClose()) {
133138
throw new Exception('Could not close file after writing.');
134139
}
135140
}

src/PhpSpreadsheet/Writer/Ods/Settings.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Composer\Pcre\Preg;
66
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
77
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
8+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
89
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
910
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1011
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
@@ -88,9 +89,7 @@ private function writeWorksheetSettings(XMLWriter $objWriter, Worksheet $workshe
8889
$objWriter->writeAttribute('config:name', $worksheet->getTitle());
8990

9091
$this->writeSelectedCells($objWriter, $worksheet);
91-
if ($worksheet->getFreezePane() !== null) {
92-
$this->writeFreezePane($objWriter, $worksheet);
93-
}
92+
$this->writeFreezePane($objWriter, $worksheet);
9493

9594
$objWriter->endElement(); // config:config-item-map-entry Worksheet
9695
}
@@ -125,7 +124,7 @@ private function writeSplitValue(XMLWriter $objWriter, string $splitMode, string
125124

126125
private function writeFreezePane(XMLWriter $objWriter, Worksheet $worksheet): void
127126
{
128-
$freezePane = CellAddress::fromCellAddress($worksheet->getFreezePane() ?? '');
127+
$freezePane = CellAddress::fromCellAddress($worksheet->getFreezePane() ?? 'A1');
129128
if ($freezePane->cellAddress() === 'A1') {
130129
return;
131130
}
@@ -139,7 +138,7 @@ private function writeFreezePane(XMLWriter $objWriter, Worksheet $worksheet): vo
139138
$this->writeSplitValue($objWriter, 'PositionLeft', 'short', '0');
140139
$this->writeSplitValue($objWriter, 'PositionRight', 'short', (string) ($columnId - 1));
141140

142-
for ($column = 'A'; $column !== $columnName; ++$column) {
141+
for ($column = 'A'; $column !== $columnName; StringHelper::stringIncrement($column)) {
143142
$worksheet->getColumnDimension($column)->setAutoSize(true);
144143
}
145144

src/PhpSpreadsheet/Writer/Xls/CellDataValidation.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,20 @@ public static function type(DataValidation $dataValidation): int
4747
{
4848
$validationType = $dataValidation->getType();
4949

50-
if (array_key_exists($validationType, self::$validationTypeMap)) {
51-
return self::$validationTypeMap[$validationType];
52-
}
53-
54-
return self::$validationTypeMap[DataValidation::TYPE_NONE];
50+
return self::$validationTypeMap[$validationType] ?? self::$validationTypeMap[DataValidation::TYPE_NONE];
5551
}
5652

5753
public static function errorStyle(DataValidation $dataValidation): int
5854
{
5955
$errorStyle = $dataValidation->getErrorStyle();
6056

61-
if (array_key_exists($errorStyle, self::$errorStyleMap)) {
62-
return self::$errorStyleMap[$errorStyle];
63-
}
64-
65-
return self::$errorStyleMap[DataValidation::STYLE_STOP];
57+
return self::$errorStyleMap[$errorStyle] ?? self::$errorStyleMap[DataValidation::STYLE_STOP];
6658
}
6759

6860
public static function operator(DataValidation $dataValidation): int
6961
{
7062
$operator = $dataValidation->getOperator();
7163

72-
if (array_key_exists($operator, self::$operatorMap)) {
73-
return self::$operatorMap[$operator];
74-
}
75-
76-
return self::$operatorMap[DataValidation::OPERATOR_BETWEEN];
64+
return self::$operatorMap[$operator] ?? self::$operatorMap[DataValidation::OPERATOR_BETWEEN];
7765
}
7866
}

src/PhpSpreadsheet/Writer/Xls/ErrorCode.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ class ErrorCode
1919

2020
public static function error(string $errorCode): int
2121
{
22-
if (array_key_exists($errorCode, self::$errorCodeMap)) {
23-
return self::$errorCodeMap[$errorCode];
24-
}
25-
26-
return 0;
22+
return self::$errorCodeMap[$errorCode] ?? 0;
2723
}
2824
}

src/PhpSpreadsheet/Writer/Xlsx/StringTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private function writeChartTextColor(XMLWriter $objWriter, ?ChartColor $underlin
306306
$objWriter->writeAttribute('val', $value);
307307
$alpha = $underlineColor->getAlpha();
308308
if (is_numeric($alpha)) {
309-
$objWriter->startElement('a:alpha');
309+
$objWriter->startElement($prefix . 'alpha');
310310
$objWriter->writeAttribute('val', ChartColor::alphaToXml((int) $alpha));
311311
$objWriter->endElement();
312312
}

tests/PhpSpreadsheetTests/Chart/ChartBorderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function readCharts(XlsxReader $reader): void
2121
public function writeCharts(XlsxWriter $writer): void
2222
{
2323
$writer->setIncludeCharts(true);
24+
$writer->setUseDiskCaching(true, sys_get_temp_dir());
2425
}
2526

2627
public function testChartBorder(): void

0 commit comments

Comments
 (0)