Skip to content

Commit 2d1a772

Browse files
authored
[TASK] Use native type declarations for the exception classes (#922)
Also improve the type annotations. Also fix the callers to get the tests green with the native types. Part of #811
1 parent d7be021 commit 2d1a772

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Please also have a look at our
2626
- Only allow `string` for some `OutputFormat` properties (#885)
2727
- Make all non-private properties `@internal` (#886)
2828
- Use more native type declarations and strict mode
29-
(#641, #772, #774, #778, #804, #841, #873, #875, #891)
29+
(#641, #772, #774, #778, #804, #841, #873, #875, #891, #922)
3030
- Add visibility to all class/interface constants (#469)
3131

3232
### Deprecated

src/Parsing/OutputException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
final class OutputException extends SourceException
1111
{
1212
/**
13-
* @param string $sMessage
1413
* @param int<0, max> $lineNumber
1514
*/
16-
public function __construct($sMessage, $lineNumber = 0)
15+
public function __construct(string $sMessage, int $lineNumber = 0)
1716
{
1817
parent::__construct($sMessage, $lineNumber);
1918
}

src/Parsing/ParserState.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function parseIdentifier($bIgnoreCase = true)
146146
}
147147
$sResult = $this->parseCharacter(true);
148148
if ($sResult === null) {
149-
throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->lineNumber);
149+
throw new UnexpectedTokenException('', $this->peek(5), 'identifier', $this->lineNumber);
150150
}
151151
$sCharacter = null;
152152
while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
@@ -294,14 +294,19 @@ public function consume($mValue = 1): string
294294
$iLineCount = \substr_count($mValue, "\n");
295295
$iLength = $this->strlen($mValue);
296296
if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
297-
throw new UnexpectedTokenException($mValue, $this->peek(\max($iLength, 5)), $this->lineNumber);
297+
throw new UnexpectedTokenException(
298+
$mValue,
299+
$this->peek(\max($iLength, 5)),
300+
'literal',
301+
$this->lineNumber
302+
);
298303
}
299304
$this->lineNumber += $iLineCount;
300305
$this->iCurrentPosition += $this->strlen($mValue);
301306
return $mValue;
302307
} else {
303308
if ($this->iCurrentPosition + $mValue > $this->iLength) {
304-
throw new UnexpectedEOFException($mValue, $this->peek(5), 'count', $this->lineNumber);
309+
throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber);
305310
}
306311
$sResult = $this->substr($this->iCurrentPosition, $mValue);
307312
$iLineCount = \substr_count($sResult, "\n");

src/Parsing/SourceException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
class SourceException extends \Exception
88
{
99
/**
10-
* @var int
10+
* @var int<0, max>
1111
*/
1212
private $lineNumber;
1313

1414
/**
15-
* @param string $sMessage
1615
* @param int<0, max> $lineNumber
1716
*/
18-
public function __construct($sMessage, $lineNumber = 0)
17+
public function __construct(string $sMessage, int $lineNumber = 0)
1918
{
2019
$this->lineNumber = $lineNumber;
2120
if ($lineNumber !== 0) {

src/Parsing/UnexpectedTokenException.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ class UnexpectedTokenException extends SourceException
2020
private $sFound;
2121

2222
/**
23-
* Possible values: literal, identifier, count, expression, search
24-
*
25-
* @var string
23+
* @var 'literal'|'identifier'|'count'|'expression'|'search'|'custom'
2624
*/
2725
private $sMatchType;
2826

2927
/**
30-
* @param string $sExpected
31-
* @param string $sFound
32-
* @param string $sMatchType
28+
* @param 'literal'|'identifier'|'count'|'expression'|'search'|'custom' $sMatchType
3329
* @param int<0, max> $lineNumber
3430
*/
35-
public function __construct($sExpected, $sFound, $sMatchType = 'literal', $lineNumber = 0)
31+
public function __construct(string $sExpected, string $sFound, string $sMatchType = 'literal', int $lineNumber = 0)
3632
{
3733
$this->sExpected = $sExpected;
3834
$this->sFound = $sFound;

0 commit comments

Comments
 (0)