Skip to content

Commit f8f78a7

Browse files
committed
String Increments and Php8.5
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 b99dc60 commit f8f78a7

37 files changed

+141
-117
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This makes it easier to see exactly what is being tested when reviewing the PR.
4343
4. By default, Github removes markdown headings in the Release Notes. You can either edit to restore these, or, probably preferably, change the default comment character on your system - `git config core.commentChar ";"`.
4444

4545
> **Note:** Tagged releases are made from the `master` branch. Only in an emergency should a tagged release be made from the `release` branch. (i.e. cherry-picked hot-fixes.) However, there are 4 branches which have been updated to apply security patches, and those may be tagged if future security updates are needed.
46-
- release1291
47-
- release210
46+
- release1291 (no further updates aside from security patches, including code changes needed for Php 8.5 compatibility)
47+
- release210 (no further updates aside from security patches, including code changes needed for Php 8.5 compatibility)
4848
- release222
4949
- release390

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 is the master branch, and is maintained for security and bug fixes.
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).

docs/topics/accessing-cells.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFrom
472472
echo '<table>' . "\n";
473473
for ($row = 1; $row <= $highestRow; ++$row) {
474474
echo '<tr>' . PHP_EOL;
475+
// Use StringHelper::stringIncrement($col) rather than ++$col if using Php8.5+.
475476
for ($col = 1; $col <= $highestColumnIndex; ++$col) {
476477
$value = $worksheet->getCell([$col, $row])->getValue();
477478
echo '<td>' . $value . '</td>' . PHP_EOL;
@@ -494,11 +495,12 @@ $worksheet = $spreadsheet->getActiveSheet();
494495
$highestRow = $worksheet->getHighestDataRow(); // e.g. 10
495496
$highestColumn = $worksheet->getHighestDataColumn(); // e.g 'F'
496497
// Increment the highest column letter
497-
++$highestColumn;
498+
++$highestColumn; // StringHelper::stringIncrement($highestColumn); if using Php8.5+.
498499

499500
echo '<table>' . "\n";
500501
for ($row = 1; $row <= $highestRow; ++$row) {
501502
echo '<tr>' . PHP_EOL;
503+
// Use StringHelper::stringIncrement($col) rather than ++$col if using Php8.5+.
502504
for ($col = 'A'; $col != $highestColumn; ++$col) {
503505
echo '<td>' .
504506
$worksheet->getCell($col . $row)

docs/topics/migration-from-PHPExcel.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,15 @@ So the code must be adapted with something like:
418418
// Before
419419
$cell = $worksheet->getCellByColumnAndRow($column, $row);
420420

421+
// Use StringHelper::stringIncrement($column) rather than ++$column if using Php8.5+.
421422
for ($column = 0; $column < $max; ++$column) {
422423
$worksheet->setCellValueByColumnAndRow($column, $row, 'value');
423424
}
424425

425426
// After
426427
$cell = $worksheet->getCell([$column + 1, $row]);
427428

429+
// Use StringHelper::stringIncrement($column) rather than ++$column if using Php8.5+.
428430
for ($column = 1; $column <= $max; ++$column) {
429431
$worksheet->setCellValue([$column, $row], 'value');
430432
}

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\Worksheet;
1011

@@ -283,7 +284,7 @@ protected function mapLanguageColumns(Worksheet $translationWorksheet): array
283284
$this->log("Mapping Languages for {$sheetName}:");
284285

285286
$baseColumn = self::ENGLISH_REFERENCE_COLUMN;
286-
$languagesList = $translationWorksheet->getColumnIterator(++$baseColumn);
287+
$languagesList = $translationWorksheet->getColumnIterator(StringHelper::stringIncrement($baseColumn));
287288

288289
$languageNameMap = [];
289290
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);
@@ -156,7 +157,7 @@
156157

157158
// Calculated data
158159
$helper->log('Calculated data');
159-
for ($col = 'B'; $col != 'G'; ++$col) {
160+
for ($col = 'B'; $col != 'G'; StringHelper::stringIncrement($col)) {
160161
for ($row = 14; $row <= 41; ++$row) {
161162
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
162163
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';
@@ -22,7 +23,7 @@
2223
// Calculated data
2324
$helper->log('Calculated data');
2425
for ($row = 1; $row <= 2; ++$row) {
25-
for ($col = 'A'; $col != 'C'; ++$col) {
26+
for ($col = 'A'; $col != 'C'; StringHelper::stringIncrement($col)) {
2627
$formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue();
2728
if (
2829
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';
@@ -59,7 +60,7 @@ function transpose(string $value): array
5960
$spreadsheet->getActiveSheet()
6061
->setCellValue($continentColumn . ($key + 1), $continent);
6162

62-
++$column;
63+
StringHelper::stringIncrement($column);
6364
}
6465

6566
// 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;
@@ -97,7 +98,7 @@
9798
->fromArray($dateFunctionArray, null, 'B1', true);
9899
$spreadsheet->getActiveSheet()
99100
->fromArray($dateTitleArray, null, 'A2', true);
100-
for ($column = 'B'; $column !== 'L'; ++$column) {
101+
for ($column = 'B'; $column !== 'L'; StringHelper::stringIncrement($column)) {
101102
$spreadsheet->getActiveSheet()
102103
->fromArray($dataArray, null, "{$column}2", true);
103104
}
@@ -117,7 +118,7 @@
117118

118119
// Set conditional formatting rules and styles
119120
$helper->log('Define conditional formatting and set styles');
120-
for ($column = 'B'; $column !== 'L'; ++$column) {
121+
for ($column = 'B'; $column !== 'L'; StringHelper::stringIncrement($column)) {
121122
$wizardFactory = new Wizard("{$column}2:{$column}19");
122123
/** @var Wizard\DateValue $dateWizard */
123124
$dateWizard = $wizardFactory->newRule(Wizard::DATES_OCCURRING);
@@ -141,7 +142,7 @@
141142
$helper->log('Set some additional styling for date formats');
142143

143144
$spreadsheet->getActiveSheet()->getStyle('B:B')->getNumberFormat()->setFormatCode('ddd dd-mmm-yyyy');
144-
for ($column = 'A'; $column !== 'L'; ++$column) {
145+
for ($column = 'A'; $column !== 'L'; StringHelper::stringIncrement($column)) {
145146
if ($column !== 'A') {
146147
$spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")
147148
->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->getValueString() . ' => ' . $cell->getCalculatedValueString());

0 commit comments

Comments
 (0)