Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileScanner.php
Go to the documentation of this file.
1 <?php
8 
12 class FileScanner extends \Zend\Code\Scanner\FileScanner
13 {
17  private $tokenType;
18 
22  protected function scan()
23  {
24  if ($this->isScanned) {
25  return;
26  }
27 
28  if (!$this->tokens) {
29  throw new \Zend\Code\Exception\RuntimeException('No tokens were provided');
30  }
31 
35  if (!defined('T_TRAIT')) {
36  define('T_TRAIT', 42001);
37  }
38 
43  $tokens = &$this->tokens; // localize
44  $infos = &$this->infos; // localize
45  $tokenIndex = null;
46  $token = null;
47  $this->tokenType = null;
48  $tokenContent = null;
49  $tokenLine = null;
50  $namespace = null;
51  $docCommentIndex = false;
52  $infoIndex = 0;
53 
54  /*
55  * MACRO creation
56  */
57  $macroTokenAdvance = function () use (&$tokens, &$tokenIndex, &$token, &$tokenContent, &$tokenLine) {
58  $tokenIndex = ($tokenIndex === null) ? 0 : $tokenIndex + 1;
59  if (!isset($tokens[$tokenIndex])) {
60  $token = false;
61  $tokenContent = false;
62  $this->tokenType = false;
63  $tokenLine = false;
64 
65  return false;
66  }
67  if (is_string($tokens[$tokenIndex]) && $tokens[$tokenIndex] === '"') {
68  do {
69  $tokenIndex++;
70  } while (!(is_string($tokens[$tokenIndex]) && $tokens[$tokenIndex] === '"'));
71  }
72  $token = $tokens[$tokenIndex];
73  if (is_array($token)) {
74  list($this->tokenType, $tokenContent, $tokenLine) = $token;
75  } else {
76  $this->tokenType = null;
77  $tokenContent = $token;
78  }
79 
80  return $tokenIndex;
81  };
82  $macroTokenLogicalStartIndex = function () use (&$tokenIndex, &$docCommentIndex) {
83  return ($docCommentIndex === false) ? $tokenIndex : $docCommentIndex;
84  };
85  $macroDocCommentStart = function () use (&$tokenIndex, &$docCommentIndex) {
86  $docCommentIndex = $tokenIndex;
87 
88  return $docCommentIndex;
89  };
90  $macroDocCommentValidate = function () use (&$docCommentIndex) {
91  static $validTrailingTokens = null;
92  if ($validTrailingTokens === null) {
93  $validTrailingTokens = [T_WHITESPACE, T_FINAL, T_ABSTRACT, T_INTERFACE, T_CLASS, T_FUNCTION];
94  }
95  if ($docCommentIndex !== false && !in_array($this->tokenType, $validTrailingTokens)) {
96  $docCommentIndex = false;
97  }
98 
99  return $docCommentIndex;
100  };
101  $macroInfoAdvance = function () use (&$infoIndex, &$infos, &$tokenIndex, &$tokenLine) {
102  $infos[$infoIndex]['tokenEnd'] = $tokenIndex;
103  $infos[$infoIndex]['lineEnd'] = $tokenLine;
104  $infoIndex++;
105 
106  return $infoIndex;
107  };
108 
113  // Initialize token
114  $macroTokenAdvance();
115 
116  SCANNER_TOP:
117 
118  if ($token === false) {
119  goto SCANNER_END;
120  }
121 
122  // Validate current doc comment index
123  $macroDocCommentValidate();
124 
125  switch ($this->tokenType) {
126  case T_DOC_COMMENT:
127  $macroDocCommentStart();
128  goto SCANNER_CONTINUE;
129  //goto no break needed
130 
131  case T_NAMESPACE:
132  $infos[$infoIndex] = [
133  'type' => 'namespace',
134  'tokenStart' => $macroTokenLogicalStartIndex(),
135  'tokenEnd' => null,
136  'lineStart' => $token[2],
137  'lineEnd' => null,
138  'namespace' => null,
139  ];
140 
141  // start processing with next token
142  if ($macroTokenAdvance() === false) {
143  goto SCANNER_END;
144  }
145 
146  SCANNER_NAMESPACE_TOP:
147 
148  if ($this->tokenType === null && $tokenContent === ';' || $tokenContent === '{') {
149  goto SCANNER_NAMESPACE_END;
150  }
151 
152  if ($this->tokenType === T_WHITESPACE) {
153  goto SCANNER_NAMESPACE_CONTINUE;
154  }
155 
156  if ($this->tokenType === T_NS_SEPARATOR || $this->tokenType === T_STRING) {
157  $infos[$infoIndex]['namespace'] .= $tokenContent;
158  }
159 
160  SCANNER_NAMESPACE_CONTINUE:
161 
162  if ($macroTokenAdvance() === false) {
163  goto SCANNER_END;
164  }
165  goto SCANNER_NAMESPACE_TOP;
166 
167  SCANNER_NAMESPACE_END:
168 
169  $namespace = $infos[$infoIndex]['namespace'];
170 
171  $macroInfoAdvance();
172  goto SCANNER_CONTINUE;
173  //goto no break needed
174 
175  case T_USE:
176  $infos[$infoIndex] = [
177  'type' => 'use',
178  'tokenStart' => $macroTokenLogicalStartIndex(),
179  'tokenEnd' => null,
180  'lineStart' => $tokens[$tokenIndex][2],
181  'lineEnd' => null,
182  'namespace' => $namespace,
183  'statements' => [0 => ['use' => null, 'as' => null]],
184  ];
185 
186  $useStatementIndex = 0;
187  $useAsContext = false;
188 
189  // start processing with next token
190  if ($macroTokenAdvance() === false) {
191  goto SCANNER_END;
192  }
193 
194  SCANNER_USE_TOP:
195 
196  if ($this->tokenType === null) {
197  if ($tokenContent === ';') {
198  goto SCANNER_USE_END;
199  } elseif ($tokenContent === ',') {
200  $useAsContext = false;
201  $useStatementIndex++;
202  $infos[$infoIndex]['statements'][$useStatementIndex] = ['use' => null, 'as' => null];
203  }
204  }
205 
206  // ANALYZE
207  if ($this->tokenType !== null) {
208  if ($this->tokenType == T_AS) {
209  $useAsContext = true;
210  goto SCANNER_USE_CONTINUE;
211  }
212 
213  if ($this->tokenType == T_NS_SEPARATOR || $this->tokenType == T_STRING) {
214  if ($useAsContext == false) {
215  $infos[$infoIndex]['statements'][$useStatementIndex]['use'] .= $tokenContent;
216  } else {
217  $infos[$infoIndex]['statements'][$useStatementIndex]['as'] = $tokenContent;
218  }
219  }
220  }
221 
222  SCANNER_USE_CONTINUE:
223 
224  if ($macroTokenAdvance() === false) {
225  goto SCANNER_END;
226  }
227  goto SCANNER_USE_TOP;
228 
229  SCANNER_USE_END:
230 
231  $macroInfoAdvance();
232  goto SCANNER_CONTINUE;
233  //goto no break needed
234 
235  case T_INCLUDE:
236  case T_INCLUDE_ONCE:
237  case T_REQUIRE:
238  case T_REQUIRE_ONCE:
239  // Static for performance
240  static $includeTypes = [
241  T_INCLUDE => 'include',
242  T_INCLUDE_ONCE => 'include_once',
243  T_REQUIRE => 'require',
244  T_REQUIRE_ONCE => 'require_once'
245  ];
246 
247  $infos[$infoIndex] = [
248  'type' => 'include',
249  'tokenStart' => $macroTokenLogicalStartIndex(),
250  'tokenEnd' => null,
251  'lineStart' => $tokens[$tokenIndex][2],
252  'lineEnd' => null,
253  'includeType' => $includeTypes[$tokens[$tokenIndex][0]],
254  'path' => '',
255  ];
256 
257  // start processing with next token
258  if ($macroTokenAdvance() === false) {
259  goto SCANNER_END;
260  }
261 
262  SCANNER_INCLUDE_TOP:
263 
264  if ($this->tokenType === null && $tokenContent === ';') {
265  goto SCANNER_INCLUDE_END;
266  }
267 
268  $infos[$infoIndex]['path'] .= $tokenContent;
269 
270  SCANNER_INCLUDE_CONTINUE:
271 
272  if ($macroTokenAdvance() === false) {
273  goto SCANNER_END;
274  }
275  goto SCANNER_INCLUDE_TOP;
276 
277  SCANNER_INCLUDE_END:
278 
279  $macroInfoAdvance();
280  goto SCANNER_CONTINUE;
281  //goto no break needed
282 
283  case T_FUNCTION:
284  case T_FINAL:
285  case T_ABSTRACT:
286  case T_CLASS:
287  case T_INTERFACE:
288  case T_TRAIT:
289  $infos[$infoIndex] = [
290  'type' => ($this->tokenType === T_FUNCTION) ? 'function' : 'class',
291  'tokenStart' => $macroTokenLogicalStartIndex(),
292  'tokenEnd' => null,
293  'lineStart' => $tokens[$tokenIndex][2],
294  'lineEnd' => null,
295  'namespace' => $namespace,
296  'uses' => $this->getUsesNoScan($namespace),
297  'name' => null,
298  'shortName' => null,
299  ];
300 
301  $classBraceCount = 0;
302 
303  // start processing with current token
304 
305  SCANNER_CLASS_TOP:
306 
307  // process the name
308  if ($infos[$infoIndex]['shortName'] == ''
309  && (($this->tokenType === T_CLASS
310  || $this->tokenType === T_INTERFACE
311  || $this->tokenType === T_TRAIT
312  )
313  && $infos[$infoIndex]['type'] === 'class' && $tokens[$tokenIndex - 1][0] !== T_DOUBLE_COLON
314  || ($this->tokenType === T_FUNCTION && $infos[$infoIndex]['type'] === 'function'))
315  ) {
316  $infos[$infoIndex]['shortName'] = $tokens[$tokenIndex + 2][1];
317  $infos[$infoIndex]['name'] = (($namespace !== null)
318  ? $namespace . '\\'
319  : '') . $infos[$infoIndex]['shortName'];
320  }
321 
322  if ($this->tokenType === null) {
323  if ($tokenContent == '{') {
324  $classBraceCount++;
325  }
326  if ($tokenContent == '}') {
327  $classBraceCount--;
328  if ($classBraceCount === 0) {
329  goto SCANNER_CLASS_END;
330  }
331  }
332  }
333 
334  SCANNER_CLASS_CONTINUE:
335 
336  if ($macroTokenAdvance() === false) {
337  goto SCANNER_END;
338  }
339  goto SCANNER_CLASS_TOP;
340 
341  SCANNER_CLASS_END:
342 
343  $macroInfoAdvance();
344  goto SCANNER_CONTINUE;
345  }
346 
347  SCANNER_CONTINUE:
348 
349  if ($macroTokenAdvance() === false) {
350  goto SCANNER_END;
351  }
352  goto SCANNER_TOP;
353 
354  SCANNER_END:
355 
359  $this->isScanned = true;
360  }
361 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$tokens
Definition: cards_list.phtml:9