Completed
Push — master ( 704618...36bc4a )
by Enrico
04:20
created

FunctionDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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