Skip to content

Commit 50eeec0

Browse files
authored
String Increments and Php8.5 - release222 branch (#4614)
Fix #4600. String incrementation through the `++` operator is deprecated in Php 8.5. Because we make use of that operator to iterate through columns, we are particularly hard hit by that change - unaddressed, it causes over 2,000 errors in our test suite! It is, fortunately, not as difficult as I feared to correct. Replacing the `++` operator with a call to new method `StringHelper::stringIncrement` in 79 statements scattered over 31 source modules (in src, samples, test, and infra) eliminates all the messages in the test suite. It is possible that others are lurking, but I don't know a systematic way of determining if there are others. We'll stick with this for now, and deal with any others as they show up. This PR will be applied to the master, release390, and release222 branches. It will not be applied to the release210 or release1291 branches, which will now accept security changes only.
1 parent 54b81a1 commit 50eeec0

31 files changed

+144
-91
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# PhpSpreadsheet
22

33
[![Build Status](https://github.com/PHPOffice/PhpSpreadsheet/workflows/main/badge.svg)](https://github.com/PHPOffice/PhpSpreadsheet/actions)
4-
[![Code Quality](https://scrutinizer-ci.com/g/PHPOffice/PhpSpreadsheet/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/PHPOffice/PhpSpreadsheet/?branch=master)
5-
[![Code Coverage](https://scrutinizer-ci.com/g/PHPOffice/PhpSpreadsheet/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/PHPOffice/PhpSpreadsheet/?branch=master)
4+
[![Code Coverage](https://coveralls.io/repos/github/PHPOffice/PhpSpreadsheet/badge.svg?branch=master)](https://coveralls.io/github/PHPOffice/PhpSpreadsheet?branch=master)
65
[![Total Downloads](https://img.shields.io/packagist/dt/PHPOffice/PhpSpreadsheet)](https://packagist.org/packages/phpoffice/phpspreadsheet)
76
[![Latest Stable Version](https://img.shields.io/github/v/release/PHPOffice/PhpSpreadsheet)](https://packagist.org/packages/phpoffice/phpspreadsheet)
87
[![License](https://img.shields.io/github/license/PHPOffice/PhpSpreadsheet)](https://packagist.org/packages/phpoffice/phpspreadsheet)
@@ -11,6 +10,17 @@
1110
PhpSpreadsheet is a library written in pure PHP and offers a set of classes that
1211
allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc.
1312

13+
This branch (release222) is maintained (for security and some bug fixes), but it is *not* the latest version of PhpSpreadsheet, and may therefore lack features and bug fixes found in the latest version.
14+
15+
## PHP Version Support
16+
17+
LTS: For maintained branches, support for PHP versions will only be maintained for a period of six months beyond the
18+
[end of life](https://www.php.net/supported-versions) of that PHP version.
19+
20+
Currently the required PHP minimum version is PHP __8.1__, and we [will support that version](https://www.php.net/supported-versions.php) until 30th June 2026.
21+
22+
See the `composer.json` for other requirements.
23+
1424
## Installation
1525

1626
See the [install instructions](https://phpspreadsheet.readthedocs.io/en/latest/#installation).

infra/LocaleGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use PhpOffice\PhpSpreadsheet\Cell\Cell;
77
use PhpOffice\PhpSpreadsheet\IOFactory;
8+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
89
use PhpOffice\PhpSpreadsheet\Spreadsheet;
910
use PhpOffice\PhpSpreadsheet\Worksheet\Column;
1011
use PhpOffice\PhpSpreadsheet\Worksheet\Row;
@@ -276,7 +277,7 @@ protected function mapLanguageColumns(Worksheet $translationWorksheet): array
276277
$this->log("Mapping Languages for {$sheetName}:");
277278

278279
$baseColumn = self::ENGLISH_REFERENCE_COLUMN;
279-
$languagesList = $translationWorksheet->getColumnIterator(++$baseColumn);
280+
$languagesList = $translationWorksheet->getColumnIterator(StringHelper::stringIncrement($baseColumn));
280281

281282
$languageNameMap = [];
282283
foreach ($languagesList as $languageColumn) {

samples/Basic1/13_Calculation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
45
use PhpOffice\PhpSpreadsheet\Spreadsheet;
56

67
mt_srand(1234567890);
@@ -155,7 +156,7 @@
155156

156157
// Calculated data
157158
$helper->log('Calculated data');
158-
for ($col = 'B'; $col != 'G'; ++$col) {
159+
for ($col = 'B'; $col != 'G'; StringHelper::stringIncrement($col)) {
159160
for ($row = 14; $row <= 41; ++$row) {
160161
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
161162
if (

samples/Basic1/13_CalculationCyclicFormulae.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
45
use PhpOffice\PhpSpreadsheet\Spreadsheet;
56

67
require __DIR__ . '/../Header.php';
@@ -21,7 +22,7 @@
2122
// Calculated data
2223
$helper->log('Calculated data');
2324
for ($row = 1; $row <= 2; ++$row) {
24-
for ($col = 'A'; $col != 'C'; ++$col) {
25+
for ($col = 'A'; $col != 'C'; StringHelper::stringIncrement($col)) {
2526
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
2627
if (
2728
is_string($formula)

samples/Basic3/39_Dropdown.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
44
use PhpOffice\PhpSpreadsheet\NamedRange;
5+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
56
use PhpOffice\PhpSpreadsheet\Spreadsheet;
67

78
require __DIR__ . '/../Header.php';
@@ -57,7 +58,7 @@ function transpose(string $value): array
5758
$spreadsheet->getActiveSheet()
5859
->setCellValue($continentColumn . ($key + 1), $continent);
5960

60-
++$column;
61+
StringHelper::stringIncrement($column);
6162
}
6263

6364
// Hide the dropdown data

samples/ConditionalFormatting/05_Date_Comparisons.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
34
use PhpOffice\PhpSpreadsheet\Spreadsheet;
45
use PhpOffice\PhpSpreadsheet\Style\Alignment;
56
use PhpOffice\PhpSpreadsheet\Style\Color;
@@ -96,7 +97,7 @@
9697
->fromArray($dateFunctionArray, null, 'B1', true);
9798
$spreadsheet->getActiveSheet()
9899
->fromArray($dateTitleArray, null, 'A2', true);
99-
for ($column = 'B'; $column !== 'L'; ++$column) {
100+
for ($column = 'B'; $column !== 'L'; StringHelper::stringIncrement($column)) {
100101
$spreadsheet->getActiveSheet()
101102
->fromArray($dataArray, null, "{$column}2", true);
102103
}
@@ -116,7 +117,7 @@
116117

117118
// Set conditional formatting rules and styles
118119
$helper->log('Define conditional formatting and set styles');
119-
for ($column = 'B'; $column !== 'L'; ++$column) {
120+
for ($column = 'B'; $column !== 'L'; StringHelper::stringIncrement($column)) {
120121
$wizardFactory = new Wizard("{$column}2:{$column}19");
121122
/** @var Wizard\DateValue $dateWizard */
122123
$dateWizard = $wizardFactory->newRule(Wizard::DATES_OCCURRING);
@@ -139,7 +140,7 @@
139140
$helper->log('Set some additional styling for date formats');
140141

141142
$spreadsheet->getActiveSheet()->getStyle('B:B')->getNumberFormat()->setFormatCode('ddd dd-mmm-yyyy');
142-
for ($column = 'A'; $column !== 'L'; ++$column) {
143+
for ($column = 'A'; $column !== 'L'; StringHelper::stringIncrement($column)) {
143144
if ($column !== 'A') {
144145
$spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")
145146
->getNumberFormat()->setFormatCode('ddd dd-mmm-yyyy');

samples/LookupRef/VLOOKUP.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
34
use PhpOffice\PhpSpreadsheet\Spreadsheet;
45

56
require __DIR__ . '/../Header.php';
@@ -40,7 +41,7 @@
4041
$worksheet->getCell('I5')->setValue('=VLOOKUP(I3, B3:E9, 4, FALSE)');
4142
$worksheet->getCell('J5')->setValue('=VLOOKUP(J3, B3:E9, 4, FALSE)');
4243

43-
for ($column = 'H'; $column !== 'K'; ++$column) {
44+
for ($column = 'H'; $column !== 'K'; StringHelper::stringIncrement($column)) {
4445
for ($row = 4; $row <= 5; ++$row) {
4546
$cell = $worksheet->getCell("{$column}{$row}");
4647
$helper->log("{$column}{$row}: " . $cell->getValue() . ' => ' . $cell->getCalculatedValue());

src/PhpSpreadsheet/Calculation/Engine/Operands/StructuredReference.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
77
use PhpOffice\PhpSpreadsheet\Cell\Cell;
88
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
910
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
1011
use Stringable;
1112

@@ -177,8 +178,8 @@ private function getColumns(Cell $cell, array $tableRange): array
177178
$cellReference = $cell->getCoordinate();
178179

179180
$columns = [];
180-
$lastColumn = ++$tableRange[1][0];
181-
for ($column = $tableRange[0][0]; $column !== $lastColumn; ++$column) {
181+
$lastColumn = StringHelper::stringIncrement($tableRange[1][0]);
182+
for ($column = $tableRange[0][0]; $column !== $lastColumn; StringHelper::stringIncrement($column)) {
182183
$columns[$column] = $worksheet
183184
->getCell($column . ($this->headersRow ?? ($this->firstDataRow - 1)))
184185
->getCalculatedValue();

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private function loadStringOrFile2(string $filename, Spreadsheet $spreadsheet, b
446446
// Set cell value
447447
$sheet->getCell($columnLetter . $outRow)->setValue($rowDatum);
448448
}
449-
++$columnLetter;
449+
StringHelper::stringIncrement($columnLetter);
450450
}
451451
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
452452
++$currentRow;

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpOffice\PhpSpreadsheet\Helper\Dimension as CssDimension;
1616
use PhpOffice\PhpSpreadsheet\Helper\Html as HelperHtml;
1717
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
18+
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
1819
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1920
use PhpOffice\PhpSpreadsheet\Style\Border;
2021
use PhpOffice\PhpSpreadsheet\Style\Color;
@@ -485,7 +486,7 @@ private function processDomElementTable(Worksheet $sheet, int &$row, string &$co
485486
$this->processDomElement($child, $sheet, $row, $column, $cellContent);
486487
$column = $this->releaseTableStartColumn();
487488
if ($this->tableLevel > 1) {
488-
++$column;
489+
StringHelper::stringIncrement($column);
489490
} else {
490491
++$row;
491492
}
@@ -498,7 +499,7 @@ private function processDomElementTr(Worksheet $sheet, int &$row, string &$colum
498499
{
499500
if ($child->nodeName === 'col') {
500501
$this->applyInlineStyle($sheet, -1, $this->currentColumn, $attributeArray);
501-
++$this->currentColumn;
502+
StringHelper::stringIncrement($this->currentColumn);
502503
} elseif ($child->nodeName === 'tr') {
503504
$column = $this->getTableStartColumn();
504505
$cellContent = '';
@@ -575,7 +576,7 @@ private function processDomElementDataFormat(Worksheet $sheet, int $row, string
575576
private function processDomElementThTd(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void
576577
{
577578
while (isset($this->rowspan[$column . $row])) {
578-
++$column;
579+
StringHelper::stringIncrement($column);
579580
}
580581
$this->processDomElement($child, $sheet, $row, $column, $cellContent);
581582

@@ -595,7 +596,7 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col
595596
//create merging rowspan and colspan
596597
$columnTo = $column;
597598
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
598-
++$columnTo;
599+
StringHelper::stringIncrement($columnTo);
599600
}
600601
$range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1);
601602
foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
@@ -614,13 +615,13 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col
614615
//create merging colspan
615616
$columnTo = $column;
616617
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
617-
++$columnTo;
618+
StringHelper::stringIncrement($columnTo);
618619
}
619620
$sheet->mergeCells($column . $row . ':' . $columnTo . $row);
620621
$column = $columnTo;
621622
}
622623

623-
++$column;
624+
StringHelper::stringIncrement($column);
624625
}
625626

626627
protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$row, string &$column, string &$cellContent): void
@@ -857,7 +858,7 @@ private function applyInlineStyle(Worksheet &$sheet, int $row, string $column, a
857858
} elseif (isset($attributeArray['rowspan'], $attributeArray['colspan'])) {
858859
$columnTo = $column;
859860
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
860-
++$columnTo;
861+
StringHelper::stringIncrement($columnTo);
861862
}
862863
$range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1);
863864
$cellStyle = $sheet->getStyle($range);
@@ -867,7 +868,7 @@ private function applyInlineStyle(Worksheet &$sheet, int $row, string $column, a
867868
} elseif (isset($attributeArray['colspan'])) {
868869
$columnTo = $column;
869870
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
870-
++$columnTo;
871+
StringHelper::stringIncrement($columnTo);
871872
}
872873
$range = $column . $row . ':' . $columnTo . $row;
873874
$cellStyle = $sheet->getStyle($range);

0 commit comments

Comments
 (0)