Completed
Pull Request — master (#193)
by Enrico
06:57 queued 02:48
created

FunctionDefinition::compile()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.3541

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 7
nop 1
dl 0
loc 41
ccs 22
cts 28
cp 0.7856
crap 6.3541
rs 8.439
c 0
b 0
f 0
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 2
    public function __construct($name, Node\Stmt\Function_ $statement)
46
    {
47 2
        $this->name = $name;
48 2
        $this->statement = $statement;
49 2
    }
50
51
    /**
52
     * Compile function to check it
53
     *
54
     * @param Context $context
55
     * @return bool
56
     */
57 2
    public function compile(Context $context)
58
    {
59 2
        if ($this->compiled) {
60
            return true;
61
        }
62
63 2
        $context->getEventManager()->fire(
64 2
            Event\StatementBeforeCompile::EVENT_NAME,
65 2
            new Event\StatementBeforeCompile(
66 2
                $this->statement,
67
                $context
68 2
            )
69 2
        );
70
71 2
        $context->setFilepath($this->filepath);
72 2
        $this->compiled = true;
73
74 2
        $context->scopePointer = $this->getPointer();
75 2
        $context->setScope(null);
76
77 2
        if (count($this->statement->stmts) == 0) {
78
            return $context->notice(
79
                'not-implemented-function',
80
                sprintf('Function %s() is not implemented', $this->name),
81
                $this->statement
82
            );
83
        }
84
85 2
        if (count($this->statement->params) > 0) {
86
            /** @var  Node\Param $parameter */
87 1
            foreach ($this->statement->params as $parameter) {
88 1
                $context->addSymbol($parameter->name);
89 1
            }
90 1
        }
91
92 2
        foreach ($this->statement->stmts as $st) {
93 2
            \PHPSA\nodeVisitorFactory($st, $context);
94 2
        }
95
96 2
        return true;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 2
    public function getFilepath()
103
    {
104 2
        return $this->filepath;
105
    }
106
107
    /**
108
     * @param string $filepath
109
     */
110 2
    public function setFilepath($filepath)
111
    {
112 2
        $this->filepath = $filepath;
113 2
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getNamespace()
119
    {
120
        return $this->namespace;
121
    }
122
123
    /**
124
    * @return Node\Param[]
125
    */
126 1
    public function getParams()
127
    {
128 1
        return $this->statement->params;
129
    }
130
}
131