forked from Wardormeur/wpphpbbunicorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SafeFunction.php
52 lines (44 loc) · 1.01 KB
/
SafeFunction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
class SafeFunction extends PhpParser\NodeVisitorAbstract
{
private $functions;
public function __construct(array $functions){
$this->functions = $functions;
}
public function leaveNode(Node $node) {
if($node instanceof Stmt\Function_){
$is_function = array_search($node->name,$this->functions);
if ( $is_function!== FALSE){
$node = $this->encapsulate($node);
$this->removeFunction($is_function);
return $node;
}
}
}
private function encapsulate($node){
$encapsulated_node =
new Node\Stmt\If_(
new Expr\BooleanNot(
new Node\Expr\FuncCall(
new Name\FullyQualified('function_exists'),
[
new Node\Arg(new Scalar\String_($node->name))
]
)
)
,[
'stmts' =>[$node]
]
);
return $encapsulated_node;
}
private function removeFunction($function_index){
unset($this->function[$function_index]);
}
}
?>