Skip to content

Commit 8126e24

Browse files
author
MarkBaker
committed
Performance tweaks to cell collection
I suspect that scrutiniser may complain that the pass-by-reference values in an expression like `sscanf($coord, '%[A-Z]%d', $column, $row)` don't exist; but PHP handles that without issue, creating the variables as needed, and phpstan has no problems with that, so scrutiniser shouldn't treat it as an issue. There's no point in adding code (even if it's just pre-defining call-by-reference arguments) when it's unnecessary overhead.
1 parent f48044c commit 8126e24

File tree

4 files changed

+5
-34
lines changed

4 files changed

+5
-34
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,11 +1175,6 @@ parameters:
11751175
count: 1
11761176
path: src/PhpSpreadsheet/Cell/Coordinate.php
11771177

1178-
-
1179-
message: "#^Cannot use array destructuring on array\\|null\\.$#"
1180-
count: 1
1181-
path: src/PhpSpreadsheet/Cell/Coordinate.php
1182-
11831178
-
11841179
message: "#^Parameter \\#4 \\$currentRow of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Coordinate\\:\\:validateRange\\(\\) expects int, string given\\.$#"
11851180
count: 1
@@ -3120,11 +3115,6 @@ parameters:
31203115
count: 1
31213116
path: src/PhpSpreadsheet/Reader/Xml/Style.php
31223117

3123-
-
3124-
message: "#^Cannot use array destructuring on array\\|null\\.$#"
3125-
count: 4
3126-
path: src/PhpSpreadsheet/ReferenceHelper.php
3127-
31283118
-
31293119
message: "#^Elseif condition is always true\\.$#"
31303120
count: 1

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ private static function sortCellReferenceArray(array $cellList): array
383383
// Sort the result by column and row
384384
$sortKeys = [];
385385
foreach ($cellList as $coord) {
386-
[$column, $row] = sscanf($coord, '%[A-Z]%d');
386+
sscanf($coord, '%[A-Z]%d', $column, $row);
387387
$sortKeys[sprintf('%3s%09d', $column, $row)] = $coord;
388388
}
389389
ksort($sortKeys);

src/PhpSpreadsheet/Collection/Cells.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Generator;
66
use PhpOffice\PhpSpreadsheet\Cell\Cell;
7-
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
87
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
98
use PhpOffice\PhpSpreadsheet\Settings;
109
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
@@ -152,8 +151,6 @@ public function getSortedCoordinates()
152151
{
153152
$sortKeys = [];
154153
foreach ($this->getCoordinates() as $coord) {
155-
$column = '';
156-
$row = 0;
157154
sscanf($coord, '%[A-Z]%d', $column, $row);
158155
$sortKeys[sprintf('%09d%3s', $row, $column)] = $coord;
159156
}
@@ -172,8 +169,6 @@ public function getHighestRowAndColumn()
172169
// Lookup highest column and highest row
173170
$col = ['A' => '1A'];
174171
$row = [1];
175-
$c = '';
176-
$r = 0;
177172
foreach ($this->getCoordinates() as $coord) {
178173
sscanf($coord, '%[A-Z]%d', $c, $r);
179174
$row[$r] = $r;
@@ -207,9 +202,6 @@ public function getCurrentCoordinate()
207202
*/
208203
public function getCurrentColumn()
209204
{
210-
$column = '';
211-
$row = 0;
212-
213205
sscanf($this->currentCoordinate ?? '', '%[A-Z]%d', $column, $row);
214206

215207
return $column;
@@ -222,9 +214,6 @@ public function getCurrentColumn()
222214
*/
223215
public function getCurrentRow()
224216
{
225-
$column = '';
226-
$row = 0;
227-
228217
sscanf($this->currentCoordinate ?? '', '%[A-Z]%d', $column, $row);
229218

230219
return (int) $row;
@@ -245,8 +234,6 @@ public function getHighestColumn($row = null)
245234
}
246235

247236
$maxColumn = '1A';
248-
$c = '';
249-
$r = 0;
250237
foreach ($this->getCoordinates() as $coord) {
251238
sscanf($coord, '%[A-Z]%d', $c, $r);
252239
if ($r != $row) {
@@ -273,8 +260,6 @@ public function getHighestRow($column = null)
273260
}
274261

275262
$maxRow = 1;
276-
$c = '';
277-
$r = 0;
278263
foreach ($this->getCoordinates() as $coord) {
279264
sscanf($coord, '%[A-Z]%d', $c, $r);
280265
if ($c != $column) {
@@ -341,8 +326,6 @@ public function cloneCellCollection(Worksheet $worksheet)
341326
*/
342327
public function removeRow($row): void
343328
{
344-
$c = '';
345-
$r = 0;
346329
foreach ($this->getCoordinates() as $coord) {
347330
sscanf($coord, '%[A-Z]%d', $c, $r);
348331
if ($r == $row) {
@@ -358,8 +341,6 @@ public function removeRow($row): void
358341
*/
359342
public function removeColumn($column): void
360343
{
361-
$c = '';
362-
$r = 0;
363344
foreach ($this->getCoordinates() as $coord) {
364345
sscanf($coord, '%[A-Z]%d', $c, $r);
365346
if ($c == $column) {

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public static function columnReverseSort($a, $b)
9090
*/
9191
public static function cellSort($a, $b)
9292
{
93-
[$ac, $ar] = sscanf($a, '%[A-Z]%d');
94-
[$bc, $br] = sscanf($b, '%[A-Z]%d');
93+
sscanf($a, '%[A-Z]%d', $ac, $ar);
94+
sscanf($b, '%[A-Z]%d', $bc, $br);
9595

9696
if ($ar === $br) {
9797
return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
@@ -111,8 +111,8 @@ public static function cellSort($a, $b)
111111
*/
112112
public static function cellReverseSort($a, $b)
113113
{
114-
[$ac, $ar] = sscanf($a, '%[A-Z]%d');
115-
[$bc, $br] = sscanf($b, '%[A-Z]%d');
114+
sscanf($a, '%[A-Z]%d', $ac, $ar);
115+
sscanf($b, '%[A-Z]%d', $bc, $br);
116116

117117
if ($ar === $br) {
118118
return -strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);

0 commit comments

Comments
 (0)