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