Skip to content

Commit 8013034

Browse files
committed
Fix Some Phpstan V2 Issues as Well
Performance of Phpstan V2 on my machine is dreadful. I will not be upgrading till I figure out how to fix it.
1 parent 3319770 commit 8013034

File tree

8 files changed

+81
-42
lines changed

8 files changed

+81
-42
lines changed

src/PhpSpreadsheet/Cell/AdvancedValueBinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public function bindValue(Cell $cell, mixed $value = null): bool
6161
if (preg_match(FormattedNumber::currencyMatcherRegexp(), (string) preg_replace('/(\d)' . $thousandsSeparator . '(\d)/u', '$1$2', $value), $matches, PREG_UNMATCHED_AS_NULL)) {
6262
// Convert value to number
6363
$sign = ($matches['PrefixedSign'] ?? $matches['PrefixedSign2'] ?? $matches['PostfixedSign']) ?? null;
64-
$currencyCode = $matches['PrefixedCurrency'] ?? $matches['PostfixedCurrency'];
64+
$currencyCode = $matches['PrefixedCurrency'] ?? $matches['PostfixedCurrency'] ?? '';
6565
/** @var string */
6666
$temp = str_replace([$decimalSeparatorNoPreg, $currencyCode, ' ', '-'], ['.', '', '', ''], (string) preg_replace('/(\d)' . $thousandsSeparator . '(\d)/u', '$1$2', $value));
6767
$value = (float) ($sign . trim($temp));
6868

69-
return $this->setCurrency($value, $cell, $currencyCode ?? '');
69+
return $this->setCurrency($value, $cell, $currencyCode);
7070
}
7171

7272
// Check for time without seconds e.g. '9:45', '09:45'

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ class Csv extends BaseReader
6969
*/
7070
private ?string $escapeCharacter = null;
7171

72-
/**
73-
* The character that will be supplied to fgetcsv
74-
* when escapeCharacter is null.
75-
* It is anticipated that it will conditionally be set
76-
* to null-string for Php9 and above.
77-
*/
78-
private static string $defaultEscapeCharacter = PHP_VERSION_ID < 90000 ? '\\' : '';
79-
8072
/**
8173
* Callback for setting defaults in construction.
8274
*
@@ -200,7 +192,7 @@ protected function inferSeparator(): void
200192
return;
201193
}
202194

203-
$inferenceEngine = new Delimiter($this->fileHandle, $this->escapeCharacter ?? self::$defaultEscapeCharacter, $this->enclosure);
195+
$inferenceEngine = new Delimiter($this->fileHandle, $this->getEscapeCharacter(), $this->enclosure);
204196

205197
// If number of lines is 0, nothing to infer : fall back to the default
206198
if ($inferenceEngine->linesCounted() === 0) {
@@ -323,10 +315,10 @@ public function setTestAutoDetect(bool $value): self
323315
return $this;
324316
}
325317

326-
private function setAutoDetect(?string $value): ?string
318+
private function setAutoDetect(?string $value, int $version = PHP_VERSION_ID): ?string
327319
{
328320
$retVal = null;
329-
if ($value !== null && $this->testAutodetect && PHP_VERSION_ID < 90000) {
321+
if ($value !== null && $this->testAutodetect && $version < 90000) {
330322
$retVal2 = @ini_set('auto_detect_line_endings', $value);
331323
if (is_string($retVal2)) {
332324
$retVal = $retVal2;
@@ -566,9 +558,9 @@ public function getContiguous(): bool
566558
* Not yet ready to mark deprecated in order to give users
567559
* a migration path.
568560
*/
569-
public function setEscapeCharacter(string $escapeCharacter): self
561+
public function setEscapeCharacter(string $escapeCharacter, int $version = PHP_VERSION_ID): self
570562
{
571-
if (PHP_VERSION_ID >= 90000 && $escapeCharacter !== '') {
563+
if ($version >= 90000 && $escapeCharacter !== '') {
572564
throw new ReaderException('Escape character must be null string for Php9+');
573565
}
574566

@@ -577,9 +569,9 @@ public function setEscapeCharacter(string $escapeCharacter): self
577569
return $this;
578570
}
579571

580-
public function getEscapeCharacter(): string
572+
public function getEscapeCharacter(int $version = PHP_VERSION_ID): string
581573
{
582-
return $this->escapeCharacter ?? self::$defaultEscapeCharacter;
574+
return $this->escapeCharacter ?? self::getDefaultEscapeCharacter($version);
583575
}
584576

585577
/**
@@ -705,10 +697,11 @@ private static function getCsv(
705697
?int $length = null,
706698
string $separator = ',',
707699
string $enclosure = '"',
708-
?string $escape = null
700+
?string $escape = null,
701+
int $version = PHP_VERSION_ID
709702
): array|false {
710-
$escape = $escape ?? self::$defaultEscapeCharacter;
711-
if (PHP_VERSION_ID >= 80400 && $escape !== '') {
703+
$escape = $escape ?? self::getDefaultEscapeCharacter();
704+
if ($version >= 80400 && $escape !== '') {
712705
return @fgetcsv($stream, $length, $separator, $enclosure, $escape);
713706
}
714707

@@ -720,10 +713,11 @@ public static function affectedByPhp9(
720713
string $inputEncoding = 'UTF-8',
721714
?string $delimiter = null,
722715
string $enclosure = '"',
723-
string $escapeCharacter = '\\'
716+
string $escapeCharacter = '\\',
717+
int $version = PHP_VERSION_ID
724718
): bool {
725-
if (PHP_VERSION_ID < 70400 || PHP_VERSION_ID >= 90000) {
726-
throw new ReaderException('Function valid only for Php7.4 or Php8'); // @codeCoverageIgnore
719+
if ($version < 70400 || $version >= 90000) {
720+
throw new ReaderException('Function valid only for Php7.4 or Php8');
727721
}
728722
$reader1 = new self();
729723
$reader1->setInputEncoding($inputEncoding)
@@ -749,4 +743,15 @@ public static function affectedByPhp9(
749743

750744
return $array1 !== $array2;
751745
}
746+
747+
/**
748+
* The character that will be supplied to fgetcsv
749+
* when escapeCharacter is null.
750+
* It is anticipated that it will conditionally be set
751+
* to null-string for Php9 and above.
752+
*/
753+
private static function getDefaultEscapeCharacter(int $version = PHP_VERSION_ID): string
754+
{
755+
return $version < 90000 ? '\\' : '';
756+
}
752757
}

src/PhpSpreadsheet/Style/Borders.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@ public function getDiagonalDirection(): int
324324
*/
325325
public function setDiagonalDirection(int $direction): static
326326
{
327-
if ($direction == '') {
328-
$direction = self::DIAGONAL_NONE;
329-
}
330327
if ($this->isSupervisor) {
331328
$styleArray = $this->getStyleArray(['diagonalDirection' => $direction]);
332329
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);

tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ protected function tearDown(): void
2424
}
2525

2626
#[DataProvider('providerEndings')]
27-
public function testEndings(string $ending): void
27+
#[DataProvider('providerEndings2')]
28+
public function testEndings(string $ending, int $version = PHP_VERSION_ID): void
2829
{
29-
if ($ending === "\r" && PHP_VERSION_ID >= 90000) {
30+
if ($ending === "\r" && $version >= 90000) {
3031
self::markTestSkipped('Mac line endings not supported for Php9+');
3132
}
3233
$this->tempFile = $filename = File::temporaryFilename();
@@ -75,4 +76,11 @@ public static function providerEndings(): array
7576
'Windows endings' => ["\r\n"],
7677
];
7778
}
79+
80+
public static function providerEndings2(): array
81+
{
82+
return [
83+
'Mac endings Php9+' => ["\r", 90000],
84+
];
85+
}
7886
}

tests/PhpSpreadsheetTests/Reader/Csv/CsvTest.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
use PhpOffice\PhpSpreadsheet\Reader\Csv;
88
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
1011

1112
class CsvTest extends TestCase
1213
{
13-
#[\PHPUnit\Framework\Attributes\DataProvider('providerDelimiterDetection')]
14+
#[DataProvider('providerDelimiterDetection')]
1415
public function testDelimiterDetection(string $filename, string $expectedDelimiter, string $cell, string|float|int|null $expectedValue): void
1516
{
1617
$reader = new Csv();
@@ -85,7 +86,7 @@ public static function providerDelimiterDetection(): array
8586
];
8687
}
8788

88-
#[\PHPUnit\Framework\Attributes\DataProvider('providerCanLoad')]
89+
#[DataProvider('providerCanLoad')]
8990
public function testCanLoad(bool $expected, string $filename): void
9091
{
9192
$reader = new Csv();
@@ -108,13 +109,19 @@ public static function providerCanLoad(): array
108109
];
109110
}
110111

111-
public function testEscapeCharacters(): void
112+
#[DataProvider('providerVersion')]
113+
public function testEscapeCharacters(int $version): void
112114
{
113-
if (PHP_VERSION_ID >= 90000) {
115+
if ($version >= 90000) {
114116
$this->expectException(ReaderException::class);
115117
$this->expectExceptionMessage('Escape character must be null string');
116118
}
117-
$reader = (new Csv())->setEscapeCharacter('"');
119+
$reader = new Csv();
120+
if ($version === PHP_VERSION_ID) {
121+
$reader->setEscapeCharacter('"');
122+
} else {
123+
$reader->setEscapeCharacter('"', $version);
124+
}
118125
$worksheet = $reader->load('tests/data/Reader/CSV/backslash.csv')
119126
->getActiveSheet();
120127

@@ -127,6 +134,14 @@ public function testEscapeCharacters(): void
127134
self::assertSame($expected, $worksheet->toArray());
128135
}
129136

137+
public static function providerVersion(): array
138+
{
139+
return [
140+
[PHP_VERSION_ID],
141+
[90000],
142+
];
143+
}
144+
130145
public function testInvalidWorkSheetInfo(): void
131146
{
132147
$this->expectException(ReaderException::class);
@@ -225,15 +240,19 @@ public function testReadNonexistentFileName(): void
225240
$reader->load('tests/data/Reader/CSV/encoding.utf8.csvxxx');
226241
}
227242

228-
#[\PHPUnit\Framework\Attributes\DataProvider('providerEscapes')]
229-
public function testInferSeparator(string $escape, string $delimiter): void
243+
#[DataProvider('providerEscapes')]
244+
public function testInferSeparator(string $escape, string $delimiter, int $version = PHP_VERSION_ID): void
230245
{
231-
if (PHP_VERSION_ID >= 90000 && $escape !== '') {
246+
if ($version >= 90000 && $escape !== '') {
232247
$this->expectException(ReaderException::class);
233248
$this->expectExceptionMessage('Escape character must be null string');
234249
}
235250
$reader = new Csv();
236-
$reader->setEscapeCharacter($escape);
251+
if ($version === PHP_VERSION_ID) {
252+
$reader->setEscapeCharacter($escape);
253+
} else {
254+
$reader->setEscapeCharacter($escape, $version);
255+
}
237256
$filename = 'tests/data/Reader/CSV/escape.csv';
238257
$reader->listWorksheetInfo($filename);
239258
self::assertEquals($delimiter, $reader->getDelimiter());
@@ -245,6 +264,7 @@ public static function providerEscapes(): array
245264
['\\', ';'],
246265
["\x0", ','],
247266
['', ','],
267+
['\\', ';', 90000],
248268
];
249269
}
250270

tests/PhpSpreadsheetTests/Reader/Csv/Php9Test.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
use PhpOffice\PhpSpreadsheet\Reader\Csv;
88
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
1011

1112
class Php9Test extends TestCase
1213
{
13-
public function testAffectedByPhp9(): void
14+
#[DataProvider('providerVersion')]
15+
public function testAffectedByPhp9(int $version): void
1416
{
15-
if (PHP_VERSION_ID >= 90000) {
17+
if ($version >= 90000) {
1618
$this->expectException(ReaderException::class);
1719
$this->expectExceptionMessage('Php7.4 or Php8');
1820
}
@@ -26,12 +28,20 @@ public function testAffectedByPhp9(): void
2628
if (str_contains($base, 'utf') && !str_contains($base, 'bom')) {
2729
$encoding = 'guess';
2830
}
29-
$result = Csv::affectedByPhp9($file, $encoding);
31+
$result = Csv::affectedByPhp9($file, $encoding, version: $version);
3032
if ($result) {
3133
$affected[] = $base;
3234
}
3335
}
3436
$expected = ['backslash.csv', 'escape.csv', 'linend.mac.csv'];
3537
self::assertSame($expected, $affected);
3638
}
39+
40+
public static function providerVersion(): array
41+
{
42+
return [
43+
[PHP_VERSION_ID],
44+
[90000],
45+
];
46+
}
3747
}

tests/PhpSpreadsheetTests/SpreadsheetSerializeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function testReadSerialize(): void
8989
$ser = (string) file_get_contents($inputFileName);
9090
unlink($inputFileName);
9191
$this->spreadsheet = unserialize($ser);
92+
self::assertInstanceOf(Spreadsheet::class, $this->spreadsheet);
9293
$sheet = $this->spreadsheet->getActiveSheet();
9394
self::assertSame('=SUM(summedcells)', $sheet->getCell('C1')->getValue());
9495
self::assertSame(15, $sheet->getCell('C1')->getCalculatedValue());

tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function testPercentageStoredAsString(): void
8585
$this->spreadsheet = $spreadsheet = $reader->load('tests/data/Writer/XLSX/issue.3811b.xlsx');
8686
$sheet = $spreadsheet->getActiveSheet();
8787
self::assertSame('48,34%', $sheet->getCell('L2')->getValue());
88-
self::assertIsString($sheet->getCell('L2')->getValue());
8988
self::assertSame('=(10%+L2)/2', $sheet->getCell('L1')->getValue());
9089
self::assertEqualsWithDelta(0.2917, $sheet->getCell('L1')->getCalculatedValue(), 1E-8);
9190
self::assertIsFloat($sheet->getCell('L1')->getCalculatedValue());
@@ -121,7 +120,6 @@ public function testPercentageStoredAsString2(): void
121120
$this->spreadsheet = $spreadsheet = $reader->load('tests/data/Writer/XLSX/issue.3811b.xlsx');
122121
$sheet = $spreadsheet->getActiveSheet();
123122
self::assertSame('48,34%', $sheet->getCell('L2')->getValue());
124-
self::assertIsString($sheet->getCell('L2')->getValue());
125123
self::assertSame('=(10%+L2)/2', $sheet->getCell('L1')->getValue());
126124
self::assertEqualsWithDelta(0.2917, $sheet->getCell('L1')->getCalculatedValue(), 1E-8);
127125
self::assertIsFloat($sheet->getCell('L1')->getCalculatedValue());

0 commit comments

Comments
 (0)