Skip to content

Commit 92d4c1b

Browse files
committed
TASK: Avoid errors if an empty string is parsed
1 parent 930e052 commit 92d4c1b

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Lexer
3636
public function __construct($string)
3737
{
3838
$this->string = $string;
39-
$this->currentCharacter = $string{0};
39+
$this->currentCharacter = ($string !== '') ? $string{0} : null;
4040
$this->characterPosition = 0;
4141
}
4242

tests/ParserTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
class ParserTest extends TestCase
66
{
77

8+
/**
9+
* @test
10+
*/
11+
public function shouldParseEmptyCode()
12+
{
13+
$parser = new Parser('');
14+
15+
$this->assertEquals(
16+
[],
17+
$parser->parse()
18+
);
19+
}
20+
21+
/**
22+
* @test
23+
*/
24+
public function shouldParseBlankCode()
25+
{
26+
$parser = new Parser(' ');
27+
28+
$this->assertEquals(
29+
[
30+
[
31+
'type' => 'text',
32+
'payload' => ' '
33+
]
34+
],
35+
$parser->parse()
36+
);
37+
}
38+
839
/**
940
* @test
1041
*/

0 commit comments

Comments
 (0)