LoopCommand::setEndLoopIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Example\Brainfuck\Command;
6
7
use Remorhaz\UniLex\Example\Brainfuck\Exception;
8
use Remorhaz\UniLex\Example\Brainfuck\Runtime;
9
10
class LoopCommand extends AbstractCommand
11
{
12
    private $endLoopIndex;
13
14
    public function __construct(Runtime $runtime)
15
    {
16
        parent::__construct($runtime);
17
    }
18
19
    /**
20
     * @throws Exception
21
     */
22
    public function exec(): void
23
    {
24
        if ($this->runtime->isValueZero()) {
25
            $this->runtime->setCommandIndex($this->getEndLoopIndex() + 1);
26
            return;
27
        }
28
        $this->runtime->shiftCommandIndex(1);
29
    }
30
31
    public function setEndLoopIndex(int $index): void
32
    {
33
        $this->endLoopIndex = $index;
34
    }
35
36
    /**
37
     * @return int
38
     * @throws Exception
39
     */
40
    private function getEndLoopIndex(): int
41
    {
42
        if (!isset($this->endLoopIndex)) {
43
            throw new Exception("End loop index is not defined");
44
        }
45
        return $this->endLoopIndex;
46
    }
47
}
48