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