Skip to content

Commit

Permalink
Ensure stateset doesn't get into endless recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jun 23, 2023
1 parent 9c9c22e commit 3188569
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/StateSet/InMemoryStateSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ class InMemoryStateSet implements StateSetInterface
{
/**
* Key: State
* Value: Children
* @var array<int, array>
* Value: array<parent,mappedChar>
*
* @var array<int, array<int,int>>
*/
private array $states = [];

/**
* @var array<int, array<int>>
*/
private array $children = [];

/**
* Key: State
* Value: Mapped char
Expand All @@ -27,20 +33,25 @@ class InMemoryStateSet implements StateSetInterface

public function add(int $state, int $parentState, int $mappedChar): self
{
$this->states[$state] = [$parentState, $mappedChar];
$this->mappedChars[$state] = $mappedChar;

if (! isset($this->states[$parentState])) {
$this->states[$parentState] = [];
}

$this->states[$parentState][] = $state;
$this->children[$parentState][$state] = true;

return $this;
}

public function all(): array
{
return $this->states;
}

public function getChildrenOfState(int $state): array
{
return $this->states[$state] ?? [];
if (!isset($this->children[$state])) {
return [];
}

return array_keys($this->children[$state]);
}

public function getCharForState(int $state): int
Expand Down

0 comments on commit 3188569

Please sign in to comment.