Completed
Pull Request — master (#264)
by Enrico
11:14
created

FunctionDefinition::getFilepath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 2
cts 2
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 PHPSA\Compiler\Parameter;
11
use PHPSA\Compiler\Types;
12
use PhpParser\Node;
13
use PHPSA\Compiler\Event;
14
15
/**
16
 * Class FunctionDefinition
17
 * @package PHPSA\Definition
18
 */
19
class FunctionDefinition extends ParentDefinition
20
{
21
    /**
22
     * @todo Use Finder
23
     *
24
     * @var string
25
     */
26
    protected $filepath;
27
28
    /**
29
     * @var Node\Stmt\Function_
30
     */
31
    protected $statement;
32
33
    /**
34
     * @var int
35
     */
36
    protected $returnTypes = CompiledExpression::MIXED;
37
38
    /**
39
     * @var array
40
     */
41
    protected $possibleReturnTypes = [];
42
43
    /**
44
     * @param string $name
45
     * @param Node\Stmt\Function_ $statement
46
     */
47 5
    public function __construct($name, Node\Stmt\Function_ $statement)
48
    {
49 5
        $this->name = $name;
50 5
        $this->statement = $statement;
51 5
    }
52
53
    /**
54
     * Compile function to check it
55
     *
56
     * @param Context $context
57
     * @return bool
58
     */
59 5
    public function compile(Context $context)
60
    {
61 5
        if ($this->compiled) {
62
            return true;
63
        }
64
65 5
        $context->setFilepath($this->filepath);
66 5
        $this->compiled = true;
67
68 5
        $context->scopePointer = $this->getPointer();
69 5
        $context->setScope(null);
70
71 5
        $context->getEventManager()->fire(
72 5
            Event\StatementBeforeCompile::EVENT_NAME,
73 5
            new Event\StatementBeforeCompile(
74 5
                $this->statement,
75
                $context
76 5
            )
77 5
        );
78
79 5
        if (count($this->statement->params) > 0) {
80
            /** @var  Node\Param $parameter */
81 3
            foreach ($this->statement->params as $parameter) {
82 3
                $type = CompiledExpression::UNKNOWN;
83
84 3
                if ($parameter->type) {
85
                    if (is_string($parameter->type)) {
86
                        $type = Types::getType($parameter->type);
87
                    } elseif ($parameter->type instanceof Node\Name) {
88
                        $type = CompiledExpression::OBJECT;
89
                    }
90
                }
91
92 3
                $context->addVariable(
93 3
                    new Parameter($parameter->name, null, $type, $parameter->byRef)
94 3
                );
95 3
            }
96 3
        }
97
98 5
        foreach ($this->statement->stmts as $st) {
99 4
            \PHPSA\nodeVisitorFactory($st, $context);
100 5
        }
101
102 5
        return true;
103
    }
104
105
    /**
106
     * @return string
107
     */
108 5
    public function getFilepath()
109
    {
110 5
        return $this->filepath;
111
    }
112
113
    /**
114
     * @param string $filepath
115
     */
116 5
    public function setFilepath($filepath)
117
    {
118 5
        $this->filepath = $filepath;
119 5
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getNamespace()
125
    {
126
        return $this->namespace;
127
    }
128
129
    /**
130
    * @return Node\Param[]
131
    */
132 1
    public function getParams()
133
    {
134 1
        return $this->statement->params;
135
    }
136
}
137