Skip to content

Commit 3188569

Browse files
committed
Ensure stateset doesn't get into endless recursion
1 parent 9c9c22e commit 3188569

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/StateSet/InMemoryStateSet.php

+20-9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ class InMemoryStateSet implements StateSetInterface
66
{
77
/**
88
* Key: State
9-
* Value: Children
10-
* @var array<int, array>
9+
* Value: array<parent,mappedChar>
10+
*
11+
* @var array<int, array<int,int>>
1112
*/
1213
private array $states = [];
1314

15+
/**
16+
* @var array<int, array<int>>
17+
*/
18+
private array $children = [];
19+
1420
/**
1521
* Key: State
1622
* Value: Mapped char
@@ -27,20 +33,25 @@ class InMemoryStateSet implements StateSetInterface
2733

2834
public function add(int $state, int $parentState, int $mappedChar): self
2935
{
36+
$this->states[$state] = [$parentState, $mappedChar];
3037
$this->mappedChars[$state] = $mappedChar;
31-
32-
if (! isset($this->states[$parentState])) {
33-
$this->states[$parentState] = [];
34-
}
35-
36-
$this->states[$parentState][] = $state;
38+
$this->children[$parentState][$state] = true;
3739

3840
return $this;
3941
}
4042

43+
public function all(): array
44+
{
45+
return $this->states;
46+
}
47+
4148
public function getChildrenOfState(int $state): array
4249
{
43-
return $this->states[$state] ?? [];
50+
if (!isset($this->children[$state])) {
51+
return [];
52+
}
53+
54+
return array_keys($this->children[$state]);
4455
}
4556

4657
public function getCharForState(int $state): int

0 commit comments

Comments
 (0)