Completed
Pull Request — master (#229)
by Enrico
06:28
created

FunctionDefinition   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
dl 0
loc 106
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B compile() 0 33 5
A getFilepath() 0 4 1
A setFilepath() 0 4 1
A getNamespace() 0 4 1
A getParams() 0 4 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PhpParser\Node;
11
use PHPSA\Compiler\Event;
12
13
/**
14
 * Class FunctionDefinition
15
 * @package PHPSA\Definition
16
 */
17
class FunctionDefinition extends ParentDefinition
18
{
19
    /**
20
     * @todo Use Finder
21
     *
22
     * @var string
23
     */
24
    protected $filepath;
25
26
    /**
27
     * @var Node\Stmt\Function_
28
     */
29
    protected $statement;
30
31
    /**
32
     * @var int
33
     */
34
    protected $returnTypes = CompiledExpression::MIXED;
35
36
    /**
37
     * @var array
38
     */
39
    protected $possibleReturnTypes = array();
40
41
    /**
42
     * @param string $name
43
     * @param Node\Stmt\Function_ $statement
44
     */
45 5
    public function __construct($name, Node\Stmt\Function_ $statement)
46
    {
47 5
        $this->name = $name;
48 5
        $this->statement = $statement;
49 5
    }
50
51
    /**
52
     * Compile function to check it
53
     *
54
     * @param Context $context
55
     * @return bool
56
     */
57 5
    public function compile(Context $context)
58
    {
59 5
        if ($this->compiled) {
60
            return true;
61
        }
62
63 5
        $context->getEventManager()->fire(
64 5
            Event\StatementBeforeCompile::EVENT_NAME,
65 5
            new Event\StatementBeforeCompile(
66 5
                $this->statement,
67
                $context
68 5
            )
69 5
        );
70
71 5
        $context->setFilepath($this->filepath);
72 5
        $this->compiled = true;
73
74 5
        $context->scopePointer = $this->getPointer();
75 5
        $context->setScope(null);
76
77 5
        if (count($this->statement->params) > 0) {
78
            /** @var  Node\Param $parameter */
79 3
            foreach ($this->statement->params as $parameter) {
80 3
                $context->addSymbol($parameter->name);
81 3
            }
82 3
        }
83
84 5
        foreach ($this->statement->stmts as $st) {
85 4
            \PHPSA\nodeVisitorFactory($st, $context);
86 5
        }
87
88 5
        return true;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 5
    public function getFilepath()
95
    {
96 5
        return $this->filepath;
97
    }
98
99
    /**
100
     * @param string $filepath
101
     */
102 5
    public function setFilepath($filepath)
103
    {
104 5
        $this->filepath = $filepath;
105 5
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getNamespace()
111
    {
112
        return $this->namespace;
113
    }
114
115
    /**
116
    * @return Node\Param[]
117
    */
118 1
    public function getParams()
119
    {
120 1
        return $this->statement->params;
121
    }
122
}
123