LambdaType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 75
ccs 41
cts 41
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A updateCreationEnv() 0 4 1
A run() 0 14 2
A saveCurrentEnvironment() 0 4 1
A useCreationEnvironment() 0 4 1
A createChildEnvironment() 0 6 1
A destroyFunctionEnvironment() 0 4 1
A revertToLiveEnvironment() 0 4 1
A __toString() 0 4 1
A id() 0 4 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