Evaluator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LisPhp;
4
5
class Evaluator
6
{
7
    protected $environment;
8
9 15
    public function __construct($environment)
10
    {
11 15
        $this->environment = $environment;
12 15
    }
13
14 15
    public function run($program)
15
    {
16 15
        if (is_string($program) && isset($this->environment[$program])) {
17 3
            return $this->environment[$program];
18 15
        } elseif (is_string($program) || is_numeric($program) || !is_array($program)) {
19 15
            return $program;
20 15
        } elseif ($program[0] == 'if') {
21 6
            list(, $test, $consequence, $alternative) = $program;
22 6
            $expression = ($this->run($test) ? $consequence : $alternative);
23
24 6
            return $this->run($expression);
25
        } else {
26 15
            $function = $this->environment[$program[0]];
27
28 15
            $arguments = array_map(function ($value) {
29 15
                return $this->run($value);
30 15
            }, array_slice($program, 1));
31
32 15
            return $function($arguments);
33
        }
34
    }
35
}
36