Skip to content

Commit 827121e

Browse files
authored
Merge pull request #4708 from RobinvanderVliet/simplify-code
Apply small code style improvements
2 parents f6f3daa + 965e51c commit 827121e

File tree

13 files changed

+17
-28
lines changed

13 files changed

+17
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
3333
- Unexpected Exception in Php DateTime. [Issue #4696](https://github.com/PHPOffice/PhpSpreadsheet/issues/4696) [Issue #917](https://github.com/PHPOffice/PhpSpreadsheet/issues/917) [PR #4697](https://github.com/PHPOffice/PhpSpreadsheet/pull/4697)
3434
- Add missing Dutch translation to translation file. [PR #4707](https://github.com/PHPOffice/PhpSpreadsheet/pull/4707)
3535
- Fix lots of typos throughout codebase. [PR #4705](https://github.com/PHPOffice/PhpSpreadsheet/pull/4705)
36+
- Apply small code style improvements. [PR #4708](https://github.com/PHPOffice/PhpSpreadsheet/pull/4708)
3637
- Implement missing `INFO` function. [PR #4709](https://github.com/PHPOffice/PhpSpreadsheet/pull/4709)
3738

3839
## 2025-10-25 - 5.2.0

src/PhpSpreadsheet/Calculation/FormulaParser.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,8 @@ private function parseToTokens(): void
463463
$tokenCount = count($tokens1);
464464
for ($i = 0; $i < $tokenCount; ++$i) {
465465
$token = $tokens1[$i];
466-
if (isset($tokens1[$i - 1])) {
467-
$previousToken = $tokens1[$i - 1];
468-
} else {
469-
$previousToken = null;
470-
}
471-
if (isset($tokens1[$i + 1])) {
472-
$nextToken = $tokens1[$i + 1];
473-
} else {
474-
$nextToken = null;
475-
}
466+
$previousToken = $tokens1[$i - 1] ?? null;
467+
$nextToken = $tokens1[$i + 1] ?? null;
476468

477469
if ($token->getTokenType() != FormulaToken::TOKEN_TYPE_WHITESPACE) {
478470
$tokens2[] = $token;
@@ -518,11 +510,7 @@ private function parseToTokens(): void
518510
$tokenCount = count($tokens2);
519511
for ($i = 0; $i < $tokenCount; ++$i) {
520512
$token = $tokens2[$i];
521-
if (isset($tokens2[$i - 1])) {
522-
$previousToken = $tokens2[$i - 1];
523-
} else {
524-
$previousToken = null;
525-
}
513+
$previousToken = $tokens2[$i - 1] ?? null;
526514

527515
if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == '-') {
528516
if ($i == 0) {

src/PhpSpreadsheet/Calculation/Information/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public static function asNumber($value = null)
265265
if (is_bool($value)) {
266266
return (int) $value;
267267
}
268-
if (is_string($value) && substr($value, 0, 1) === '#') {
268+
if (is_string($value) && str_starts_with($value, '#')) {
269269
return $value;
270270
}
271271

src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function INDIRECT($cellAddress, mixed $a1fmt, Cell $cell): string|
106106
*/
107107
private static function extractRequiredCells(?Worksheet $worksheet, string $cellAddress): array
108108
{
109-
return Calculation::getInstance($worksheet !== null ? $worksheet->getParent() : null)
109+
return Calculation::getInstance($worksheet?->getParent())
110110
->extractCellRange($cellAddress, $worksheet, false, createCell: true);
111111
}
112112

src/PhpSpreadsheet/Calculation/LookupRef/Offset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function OFFSET(?string $cellAddress = null, $rows = 0, $columns =
102102
/** @return mixed[] */
103103
private static function extractRequiredCells(?Worksheet $worksheet, string $cellAddress): array
104104
{
105-
return Calculation::getInstance($worksheet !== null ? $worksheet->getParent() : null)
105+
return Calculation::getInstance($worksheet?->getParent())
106106
->extractCellRange($cellAddress, $worksheet, false);
107107
}
108108

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private static function validateReferenceAndGetData($reference): array
316316

317317
$worksheet = $matches['worksheet'];
318318
if ($worksheet !== '') {
319-
if (substr($worksheet, 0, 1) === "'" && substr($worksheet, -1, 1) === "'") {
319+
if (str_starts_with($worksheet, "'") && str_ends_with($worksheet, "'")) {
320320
$worksheet = substr($worksheet, 1, -1);
321321
}
322322
$data['worksheet'] = strtolower($worksheet);

src/PhpSpreadsheet/Collection/Cells.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function getHighestColumn($row = null): string
281281
continue;
282282
}
283283
$column = ($coordinate % self::MAX_COLUMN_ID) ?: self::MAX_COLUMN_ID;
284-
$maxColumn = $maxColumn > $column ? $maxColumn : $column;
284+
$maxColumn = max($column, $maxColumn);
285285
}
286286

287287
return Coordinate::stringFromColumnIndex($maxColumn);

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ private function insertImage(Worksheet $sheet, string $column, int $row, array $
11291129
$styleArray = self::getStyleArray($attributes);
11301130

11311131
$src = $attributes['src'];
1132-
if (substr($src, 0, 5) !== 'data:') {
1132+
if (!str_starts_with($src, 'data:')) {
11331133
$src = urldecode($src);
11341134
}
11351135
$width = isset($attributes['width']) ? (float) $attributes['width'] : ($styleArray['width'] ?? null);
@@ -1195,13 +1195,13 @@ private static function getStyleArray(array $attributes): array
11951195
$arrayKey = trim($value[0]);
11961196
$arrayValue = trim($value[1]);
11971197
if ($arrayKey === 'width') {
1198-
if (substr($arrayValue, -2) === 'px') {
1198+
if (str_ends_with($arrayValue, 'px')) {
11991199
$arrayValue = (string) (((float) substr($arrayValue, 0, -2)));
12001200
} else {
12011201
$arrayValue = (new CssDimension($arrayValue))->toUnit(CssDimension::UOM_PIXELS);
12021202
}
12031203
} elseif ($arrayKey === 'height') {
1204-
if (substr($arrayValue, -2) === 'px') {
1204+
if (str_ends_with($arrayValue, 'px')) {
12051205
$arrayValue = substr($arrayValue, 0, -2);
12061206
} else {
12071207
$arrayValue = (new CssDimension($arrayValue))->toUnit(CssDimension::UOM_PIXELS);

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function toUtf8(string $xml): string
6464

6565
private function findCharSet(string $xml): string
6666
{
67-
if (substr($xml, 0, 4) === "\x4c\x6f\xa7\x94") {
67+
if (str_starts_with($xml, "\x4c\x6f\xa7\x94")) {
6868
throw new Reader\Exception('EBCDIC encoding not permitted');
6969
}
7070
$encoding = Reader\Csv::guessEncodingBom('', $xml);

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ private function readBackgroundImage(
24302430
$attrs = $rel->attributes() ?? [];
24312431
$rid = (string) ($attrs['Id'] ?? '');
24322432
$target = (string) ($attrs['Target'] ?? '');
2433-
if ($rid === $id && substr($target, 0, 2) === '..') {
2433+
if ($rid === $id && str_starts_with($target, '..')) {
24342434
$target = 'xl' . substr($target, 2);
24352435
$content = $this->getFromZipArchive($this->zip, $target);
24362436
$docSheet->setBackgroundImage($content);

0 commit comments

Comments
 (0)