LambdaType::useCreationEnvironment()   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 0
crap 1
1
<?php
2
namespace Desmond\data_types;
3
use Desmond\Evaluator;
4
use Desmond\Environment;
5
use Desmond\functions\DesmondFunction;
6
7
class LambdaType extends DesmondFunction
8
{
9
    private $evaluator;
10
    private $preRunEnv;
11
    private $creationEnv;
12
    private $functionEnv;
13
    private $functionEnvId;
14
    private $args;
15
    private $body;
16
17 15
    public function __construct($evaluator, $args, $body)
18
    {
19 15
        $this->evaluator = $evaluator;
20 15
        $this->creationEnv = clone($evaluator->currentEnv);
21 15
        $this->args = $args;
22 15
        $this->body = $body;
23 15
    }
24
25 5
    public function updateCreationEnv(Environment $env)
26
    {
27 5
        $this->creationEnv = clone($env);
28 5
    }
29
30 1
    public function id()
31
    {
32 1
        return 'anonymous';
33
    }
34
35 11
    public function run(array $params)
36
    {
37 11
        $this->saveCurrentEnvironment();
38 11
        $this->useCreationEnvironment();
39 11
        $this->createChildEnvironment();
40 11
        for ($i=0; $i<$this->args->count(); $i++) {
41 11
            $this->functionEnv->set(
42 11
                $this->args->get($i)->value(), $params[$i]);
43
        }
44 11
        $funcVal = $this->evaluator->getReturn($this->body);
45 11
        $this->destroyFunctionEnvironment();
46 11
        $this->revertToLiveEnvironment();
47 11
        return $funcVal;
48
    }
49
50 11
    private function saveCurrentEnvironment()
51
    {
52 11
        $this->preRunEnv = $this->evaluator->currentEnv;
53 11
    }
54
55 11
    public function useCreationEnvironment()
56
    {
57 11
        $this->evaluator->currentEnv = $this->creationEnv;
58 11
    }
59
60 11
    private function createChildEnvironment()
61
    {
62 11
        $this->functionEnvId = $this->evaluator->currentEnv->makeChild();
63 11
        $this->functionEnv = $this->evaluator->currentEnv->values[$this->functionEnvId];
64 11
        $this->evaluator->currentEnv = $this->functionEnv;
65 11
    }
66
67 11
    private function destroyFunctionEnvironment()
68
    {
69 11
        $this->evaluator->currentEnv->destroyChild($this->functionEnvId);
70 11
    }
71
72 11
    private function revertToLiveEnvironment()
73
    {
74 11
        $this->evaluator->currentEnv = $this->preRunEnv;
75 11
    }
76
77 7
    public function __toString()
78
    {
79 7
        return "#<function>";
80
    }
81
}
82