1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Whitelist\Tokenizer; |
10
|
|
|
|
11
|
|
|
use Maslosoft\Whitelist\Interfaces\TokenInterface; |
12
|
|
|
use Maslosoft\Whitelist\Tokenizer\Collectors\FunctionCallsCollector; |
13
|
|
|
use Maslosoft\Whitelist\Tokenizer\Collectors\MethodCallsCollector; |
14
|
|
|
use Maslosoft\Whitelist\Tokenizer\Collectors\StaticMethodCallsCollector; |
15
|
|
|
use Maslosoft\Whitelist\Tokenizer\Composite\FunctionCall; |
16
|
|
|
use Maslosoft\Whitelist\Tokenizer\Tokens\SimpleToken; |
17
|
|
|
use Maslosoft\Whitelist\Tokenizer\Tokens\Token; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tokenizer |
21
|
|
|
* |
22
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
23
|
|
|
*/ |
24
|
|
|
class Tokenizer |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
private $rawTokens = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var TokenInterface[] |
32
|
|
|
*/ |
33
|
|
|
private $tokens = []; |
34
|
|
|
|
35
|
|
|
public function __construct($code) |
36
|
|
|
{ |
37
|
|
|
$this->rawTokens = token_get_all($code); |
38
|
|
|
foreach ($this->rawTokens as $index => $token) |
39
|
|
|
{ |
40
|
|
|
if (is_array($token)) |
41
|
|
|
{ |
42
|
|
|
$this->tokens[$index] = new Token($token, $this->tokens, $index); |
43
|
|
|
} |
44
|
|
|
else |
45
|
|
|
{ |
46
|
|
|
$this->tokens[$index] = new SimpleToken($token, $this->tokens, $index); |
47
|
|
|
} |
48
|
|
|
$this->tokens[$index]->name = token_name($this->tokens[$index]->type); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @return TokenInterface[]; |
55
|
|
|
*/ |
56
|
|
|
public function getTokens() |
57
|
|
|
{ |
58
|
|
|
return $this->tokens; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get function tokens. This includes: |
63
|
|
|
* |
64
|
|
|
* * Simple functions |
65
|
|
|
* * eval |
66
|
|
|
* * echo |
67
|
|
|
* * print |
68
|
|
|
* |
69
|
|
|
* This does not include: |
70
|
|
|
* |
71
|
|
|
* * Method calls |
72
|
|
|
* * Variable name function calls |
73
|
|
|
* * Requires and includes |
74
|
|
|
* |
75
|
|
|
* @return TokenInterface[]|Token[]|SimpleToken[] |
76
|
|
|
*/ |
77
|
|
|
public function getFunctions() |
78
|
|
|
{ |
79
|
|
|
return (new FunctionCallsCollector)->collect($this->tokens); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get static method calls |
84
|
|
|
* @return array|TokenInterface[] |
85
|
|
|
*/ |
86
|
|
|
public function getStaticMethodCalls() |
87
|
|
|
{ |
88
|
|
|
return (new StaticMethodCallsCollector)->collect($this->tokens); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getMethodCalls() |
92
|
|
|
{ |
93
|
|
|
return (new MethodCallsCollector)->collect($this->tokens); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|