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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-token-stream.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class PHP_Token_FUNCTION extends PHP_TokenWithScopeAndVisibility
{
/**
* @var array
*/
protected $arguments;
/**
* @var int
*/
protected $ccn;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $signature;
/**
* @var bool
*/
private $anonymous = false;
/**
* @return array
*/
public function getArguments()
{
if ($this->arguments !== null) {
return $this->arguments;
}
$this->arguments = [];
$tokens = $this->tokenStream->tokens();
$typeDeclaration = null;
// Search for first token inside brackets
$i = $this->id + 2;
while (!$tokens[$i - 1] instanceof PHP_Token_OPEN_BRACKET) {
$i++;
}
while (!$tokens[$i] instanceof PHP_Token_CLOSE_BRACKET) {
if ($tokens[$i] instanceof PHP_Token_STRING) {
$typeDeclaration = (string) $tokens[$i];
} elseif ($tokens[$i] instanceof PHP_Token_VARIABLE) {
$this->arguments[(string) $tokens[$i]] = $typeDeclaration;
$typeDeclaration = null;
}
$i++;
}
return $this->arguments;
}
/**
* @return string
*/
public function getName()
{
if ($this->name !== null) {
return $this->name;
}
$tokens = $this->tokenStream->tokens();
$i = $this->id + 1;
if ($tokens[$i] instanceof PHP_Token_WHITESPACE) {
$i++;
}
if ($tokens[$i] instanceof PHP_Token_AMPERSAND) {
$i++;
}
if ($tokens[$i + 1] instanceof PHP_Token_OPEN_BRACKET) {
$this->name = (string) $tokens[$i];
} elseif ($tokens[$i + 1] instanceof PHP_Token_WHITESPACE && $tokens[$i + 2] instanceof PHP_Token_OPEN_BRACKET) {
$this->name = (string) $tokens[$i];
} else {
$this->anonymous = true;
$this->name = \sprintf(
'anonymousFunction:%s#%s',
$this->getLine(),
$this->getId()
);
}
if (!$this->isAnonymous()) {
for ($i = $this->id; $i; --$i) {
if ($tokens[$i] instanceof PHP_Token_NAMESPACE) {
$this->name = $tokens[$i]->getName() . '\\' . $this->name;
break;
}
if ($tokens[$i] instanceof PHP_Token_INTERFACE) {
break;
}
}
}
return $this->name;
}
/**
* @return int
*/
public function getCCN()
{
if ($this->ccn !== null) {
return $this->ccn;
}
$this->ccn = 1;
$end = $this->getEndTokenId();
$tokens = $this->tokenStream->tokens();
for ($i = $this->id; $i <= $end; $i++) {
switch (\get_class($tokens[$i])) {
case PHP_Token_IF::class:
case PHP_Token_ELSEIF::class:
case PHP_Token_FOR::class:
case PHP_Token_FOREACH::class:
case PHP_Token_WHILE::class:
case PHP_Token_CASE::class:
case PHP_Token_CATCH::class:
case PHP_Token_BOOLEAN_AND::class:
case PHP_Token_LOGICAL_AND::class:
case PHP_Token_BOOLEAN_OR::class:
case PHP_Token_LOGICAL_OR::class:
case PHP_Token_QUESTION_MARK::class:
$this->ccn++;
break;
}
}
return $this->ccn;
}
/**
* @return string
*/
public function getSignature()
{
if ($this->signature !== null) {
return $this->signature;
}
if ($this->isAnonymous()) {
$this->signature = 'anonymousFunction';
$i = $this->id + 1;
} else {
$this->signature = '';
$i = $this->id + 2;
}
$tokens = $this->tokenStream->tokens();
while (isset($tokens[$i]) &&
!$tokens[$i] instanceof PHP_Token_OPEN_CURLY &&
!$tokens[$i] instanceof PHP_Token_SEMICOLON) {
$this->signature .= $tokens[$i++];
}
$this->signature = \trim($this->signature);
return $this->signature;
}
/**
* @return bool
*/
public function isAnonymous()
{
return $this->anonymous;
}
}