Completed
Push — master ( 0864bb...f3fc5f )
by Fede
08:54 queued 07:04
created

Evaluator::__construct()   A

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;
0 ignored issues
show
Unused Code introduced by
The assignment to $_ is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
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