Skip to content

Commit 7d71a7c

Browse files
authored
New Error Reported with Phpstan 1.3 (#2481)
* New Error Reported with Phpstan 1.3 Dependabot opened a number of PRs. Most are successful, but this change is necessary to allow PR #2477 to complete successfully, and that is apparently a necessity for PR #2479. Phpstan 1.3 objected to: ```php trigger_error($errorMessage, E_USER_ERROR); return false; ``` It claims the return statement is unreachable. This isn't precisely true - an error handler might allow the return to be reached. At any rate, I have slightly restructured the code so that Phpstan will not object either with 1.2 or 1.3, which should allow the blocked PRs to succeed. There had been no previous tests for what happens when there is a formula error when suppressFormulaErrors is true. * Scrutinizer Didn't like effect of changes which Phpstan liked. Hopefully this will work better. * Improvement Eliminate added function.
1 parent 1fdd7e5 commit 7d71a7c

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,6 @@ parameters:
160160
count: 1
161161
path: src/PhpSpreadsheet/Calculation/Calculation.php
162162

163-
-
164-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:raiseFormulaError\\(\\) has no return type specified\\.$#"
165-
count: 1
166-
path: src/PhpSpreadsheet/Calculation/Calculation.php
167-
168-
-
169-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:raiseFormulaError\\(\\) has parameter \\$errorMessage with no type specified\\.$#"
170-
count: 1
171-
path: src/PhpSpreadsheet/Calculation/Calculation.php
172-
173163
-
174164
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:validateBinaryOperand\\(\\) has no return type specified\\.$#"
175165
count: 1

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5229,15 +5229,22 @@ private function executeNumericBinaryOperation($operand1, $operand2, $operation,
52295229
return $result;
52305230
}
52315231

5232-
// trigger an error, but nicely, if need be
5233-
protected function raiseFormulaError($errorMessage)
5232+
/**
5233+
* Trigger an error, but nicely, if need be.
5234+
*
5235+
* @return false
5236+
*/
5237+
protected function raiseFormulaError(string $errorMessage)
52345238
{
52355239
$this->formulaError = $errorMessage;
52365240
$this->cyclicReferenceStack->clear();
52375241
if (!$this->suppressFormulaErrors) {
52385242
throw new Exception($errorMessage);
52395243
}
5240-
trigger_error($errorMessage, E_USER_ERROR);
5244+
5245+
if (strlen($errorMessage) > 0) {
5246+
trigger_error($errorMessage, E_USER_ERROR);
5247+
}
52415248

52425249
return false;
52435250
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4+
5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
7+
use PHPUnit\Framework\TestCase;
8+
use Throwable;
9+
10+
class CalculationErrorTest extends TestCase
11+
{
12+
public function testCalculationException(): void
13+
{
14+
$this->expectException(CalcException::class);
15+
$this->expectExceptionMessage('Formula Error:');
16+
$calculation = Calculation::getInstance();
17+
$result = $calculation->_calculateFormulaValue('=SUM(');
18+
self::assertFalse($result);
19+
}
20+
21+
public function testCalculationError(): void
22+
{
23+
$calculation = Calculation::getInstance();
24+
$calculation->suppressFormulaErrors = true;
25+
$error = false;
26+
27+
try {
28+
$calculation->_calculateFormulaValue('=SUM(');
29+
} catch (Throwable $e) {
30+
self::assertSame("Formula Error: Expecting ')'", $e->getMessage());
31+
self::assertSame('PHPUnit\\Framework\\Error\\Error', get_class($e));
32+
$error = true;
33+
}
34+
self::assertTrue($error);
35+
}
36+
37+
/**
38+
* @param mixed $args
39+
*/
40+
public static function errhandler2(...$args): bool
41+
{
42+
return $args[0] === E_USER_ERROR;
43+
}
44+
45+
public function testCalculationErrorTrulySuppressed(): void
46+
{
47+
$calculation = Calculation::getInstance();
48+
$calculation->suppressFormulaErrors = true;
49+
set_error_handler([self::class, 'errhandler2']);
50+
$result = $calculation->_calculateFormulaValue('=SUM(');
51+
restore_error_handler();
52+
self::assertFalse($result);
53+
}
54+
}

0 commit comments

Comments
 (0)