Skip to content

Commit ca90379

Browse files
authored
2 Minor Phpstan-related Fixes (#3030)
For one of the Phpstan upgrades, some message text had changed so drastically that the only practical solution at the time was to move the messages from phpstan-baseline.neon to phpstan.neon.dist. This was not ideal, but it allowed us time to move on and study the errors, which I have now done. At one point, Parser is expecting a variable to be an array, and that was not clear from the code. If not an array, the code will error out (which was Phpstan's concern); I have changed it to throw an exception instead. This satisfies Phpstan, and I can get the message out of neon.dist (without needing to restore it to baseline). Unsurprisingly, the exception was never thrown in the existing test suite, although I added a couple of tests to exercise that code. In Helper/Dimension, Phpstan flagged a statement inappropriately. I suppressed the message using an annotation and filed a bug report phpstan/phpstan#7563. A fix for the problem was merged yesterday, which is good, but it puts us in a tenuous position. The annotation is needed now, but, when the fix is inevitably pushed to the version we use, the no-longer-needed annotation will trigger a different message. Recode so that neither the current nor the future versions will issue a message, eliminating the annotation in the process.
1 parent 389ca80 commit ca90379

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,3 @@ parameters:
2121
# Accept a bit anything for assert methods
2222
- '~^Parameter \#2 .* of static method PHPUnit\\Framework\\Assert\:\:assert\w+\(\) expects .*, .* given\.$~'
2323
- '~^Method PhpOffice\\PhpSpreadsheetTests\\.*\:\:test.*\(\) has parameter \$args with no type specified\.$~'
24-
25-
# Some issues in Xls/Parser between 1.6.3 and 1.7.7
26-
-
27-
message: "#^Offset '(left|right|value)' does not exist on (non-empty-array\\|string|array\\|null)\\.$#"
28-
path: src/PhpSpreadsheet/Writer/Xls/Parser.php

src/PhpSpreadsheet/Helper/Dimension.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ class Dimension
5555
*/
5656
protected $unit;
5757

58+
/**
59+
* Phpstan bug has been fixed; this function allows us to
60+
* pass Phpstan whether fixed or not.
61+
*
62+
* @param mixed $value
63+
*/
64+
private static function stanBugFixed($value): array
65+
{
66+
return is_array($value) ? $value : [null, null];
67+
}
68+
5869
public function __construct(string $dimension)
5970
{
60-
// @phpstan-ignore-next-line
61-
[$size, $unit] = sscanf($dimension, '%[1234567890.]%s');
71+
[$size, $unit] = self::stanBugFixed(sscanf($dimension, '%[1234567890.]%s'));
6272
$unit = strtolower(trim($unit ?? ''));
6373
$size = (float) $size;
6474

src/PhpSpreadsheet/Writer/Xls/Parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Parser
7878
/**
7979
* The parse tree to be generated.
8080
*
81-
* @var string
81+
* @var array|string
8282
*/
8383
public $parseTree;
8484

@@ -1445,6 +1445,9 @@ public function toReversePolish($tree = [])
14451445
if (empty($tree)) { // If it's the first call use parseTree
14461446
$tree = $this->parseTree;
14471447
}
1448+
if (!is_array($tree) || !isset($tree['left'], $tree['right'], $tree['value'])) {
1449+
throw new WriterException('Unexpected non-array');
1450+
}
14481451

14491452
if (is_array($tree['left'])) {
14501453
$converted_tree = $this->toReversePolish($tree['left']);
@@ -1475,7 +1478,7 @@ public function toReversePolish($tree = [])
14751478
$left_tree = '';
14761479
}
14771480

1478-
// add it's left subtree and return.
1481+
// add its left subtree and return.
14791482
return $left_tree . $this->convertFunction($tree['value'], $tree['right']);
14801483
}
14811484
$converted_tree = $this->convert($tree['value']);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
7+
use PhpOffice\PhpSpreadsheet\Writer\Xls\Parser;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ParserTest extends TestCase
11+
{
12+
/** @var ?Spreadsheet */
13+
private $spreadsheet;
14+
15+
protected function tearDown(): void
16+
{
17+
if ($this->spreadsheet !== null) {
18+
$this->spreadsheet->disconnectWorksheets();
19+
$this->spreadsheet = null;
20+
}
21+
}
22+
23+
public function testNonArray(): void
24+
{
25+
$this->expectException(WriterException::class);
26+
$this->expectExceptionMessage('Unexpected non-array');
27+
$this->spreadsheet = new Spreadsheet();
28+
$parser = new Parser($this->spreadsheet);
29+
$parser->toReversePolish();
30+
}
31+
32+
public function testMissingIndex(): void
33+
{
34+
$this->expectException(WriterException::class);
35+
$this->expectExceptionMessage('Unexpected non-array');
36+
$this->spreadsheet = new Spreadsheet();
37+
$parser = new Parser($this->spreadsheet);
38+
$parser->toReversePolish(['left' => 0]);
39+
}
40+
41+
public function testParseError(): void
42+
{
43+
$this->expectException(WriterException::class);
44+
$this->expectExceptionMessage('Unknown token +');
45+
$this->spreadsheet = new Spreadsheet();
46+
$parser = new Parser($this->spreadsheet);
47+
$parser->toReversePolish(['left' => 1, 'right' => 2, 'value' => '+']);
48+
}
49+
50+
public function testGoodParse(): void
51+
{
52+
$this->spreadsheet = new Spreadsheet();
53+
$parser = new Parser($this->spreadsheet);
54+
self::assertSame('1e01001e02001e0300', bin2hex($parser->toReversePolish(['left' => 1, 'right' => 2, 'value' => 3])));
55+
}
56+
}

0 commit comments

Comments
 (0)