Arbit - project tracking

PHP Depend

Browse source code

File: / PHP/ Depend/ Tokenizer/ Internal.php

Type
text/plain text/plain
Last Author
mapi
Version
1266
Line Rev. Author Source
1 1 mapi <?php
2 7 mapi /**
3 mapi * This file is part of PHP_Depend.
4 525 mapi *
5 7 mapi * PHP Version 5
6 mapi *
7 1030 mapi * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
8 7 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 171 mapi * @category QualityAssurance
40 mapi * @package PHP_Depend
41 584 mapi * @subpackage Tokenizer
42 268 mapi * @author Manuel Pichler <mapi@pdepend.org>
43 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
44 171 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45 mapi * @version SVN: $Id: Internal.php 1266 2010-05-07 15:34:11Z mapi $
46 824 mapi * @link http://pdepend.org/
47 7 mapi */
48 mapi
49 562 mapi require_once 'PHP/Depend/Token.php';
50 mapi require_once 'PHP/Depend/TokenizerI.php';
51 130 mapi require_once 'PHP/Depend/Code/File.php';
52 1266 mapi require_once 'PHP/Depend/Tokenizer/PHP52Helper.php';
53 1 mapi
54 10 mapi /**
55 mapi * This tokenizer uses the internal {@link token_get_all()} function as token stream
56 mapi * generator.
57 mapi *
58 171 mapi * @category QualityAssurance
59 mapi * @package PHP_Depend
60 584 mapi * @subpackage Tokenizer
61 268 mapi * @author Manuel Pichler <mapi@pdepend.org>
62 1030 mapi * @copyright 2008-2010 Manuel Pichler. All rights reserved.
63 171 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
64 mapi * @version Release: @package_version@
65 824 mapi * @link http://pdepend.org/
66 10 mapi *
67 mapi */
68 560 mapi class PHP_Depend_Tokenizer_Internal pdepend-warning
69 558 mapi implements PHP_Depend_TokenizerI
70 1 mapi {
71 10 mapi /**
72 mapi * Mapping between php internal tokens and php depend tokens.
73 mapi *
74 mapi * @var array(integer=>integer) $tokenMap
75 mapi */
76 1 mapi protected static $tokenMap = array(
77 81 mapi T_AS => self::T_AS,
78 mapi T_DO => self::T_DO,
79 mapi T_IF => self::T_IF,
80 mapi T_SL => self::T_SL,
81 mapi T_SR => self::T_SR,
82 mapi T_DEC => self::T_DEC,
83 mapi T_FOR => self::T_FOR,
84 mapi T_INC => self::T_INC,
85 mapi T_NEW => self::T_NEW,
86 mapi T_TRY => self::T_TRY,
87 mapi T_USE => self::T_USE,
88 mapi T_VAR => self::T_VAR,
89 mapi T_CASE => self::T_CASE,
90 mapi T_ECHO => self::T_ECHO,
91 mapi T_ELSE => self::T_ELSE,
92 mapi T_EVAL => self::T_EVAL,
93 mapi T_EXIT => self::T_EXIT,
94 mapi T_FILE => self::T_FILE,
95 545 mapi T_GOTO => self::T_GOTO,
96 81 mapi T_LINE => self::T_LINE,
97 mapi T_LIST => self::T_LIST,
98 mapi T_NS_C => self::T_NS_C,
99 mapi T_ARRAY => self::T_ARRAY,
100 mapi T_BREAK => self::T_BREAK,
101 mapi T_CLASS => self::T_CLASS,
102 mapi T_CATCH => self::T_CATCH,
103 mapi T_CLONE => self::T_CLONE,
104 mapi T_CONST => self::T_CONST,
105 mapi T_EMPTY => self::T_EMPTY,
106 mapi T_FINAL => self::T_FINAL,
107 mapi T_ISSET => self::T_ISSET,
108 mapi T_PRINT => self::T_PRINT,
109 mapi T_THROW => self::T_THROW,
110 mapi T_UNSET => self::T_UNSET,
111 mapi T_WHILE => self::T_WHILE,
112 mapi T_ELSEIF => self::T_ELSEIF,
113 mapi T_FUNC_C => self::T_FUNC_C,
114 mapi T_GLOBAL => self::T_GLOBAL,
115 mapi T_PUBLIC => self::T_PUBLIC,
116 mapi T_RETURN => self::T_RETURN,
117 mapi T_STATIC => self::T_STATIC,
118 mapi T_STRING => self::T_STRING,
119 mapi T_SWITCH => self::T_SWITCH,
120 mapi T_CLASS_C => self::T_CLASS_C,
121 84 mapi T_COMMENT => self::T_COMMENT,
122 81 mapi T_DECLARE => self::T_DECLARE,
123 mapi T_DEFAULT => self::T_DEFAULT,
124 mapi T_DNUMBER => self::T_DNUMBER,
125 mapi T_EXTENDS => self::T_EXTENDS,
126 mapi T_FOREACH => self::T_FOREACH,
127 mapi T_INCLUDE => self::T_INCLUDE,
128 mapi T_LNUMBER => self::T_LNUMBER,
129 mapi T_PRIVATE => self::T_PRIVATE,
130 mapi T_REQUIRE => self::T_REQUIRE,
131 mapi T_FUNCTION => self::T_FUNCTION,
132 mapi T_ABSTRACT => self::T_ABSTRACT,
133 mapi T_INT_CAST => self::T_INT_CAST,
134 mapi T_IS_EQUAL => self::T_IS_EQUAL,
135 mapi T_OR_EQUAL => self::T_OR_EQUAL,
136 mapi T_CONTINUE => self::T_CONTINUE,
137 mapi T_METHOD_C => self::T_METHOD_C,
138 mapi T_OPEN_TAG => self::T_OPEN_TAG,
139 mapi T_SL_EQUAL => self::T_SL_EQUAL,
140 mapi T_SR_EQUAL => self::T_SR_EQUAL,
141 mapi T_VARIABLE => self::T_VARIABLE,
142 mapi T_DIV_EQUAL => self::T_DIV_EQUAL,
143 mapi T_AND_EQUAL => self::T_AND_EQUAL,
144 mapi T_MOD_EQUAL => self::T_MOD_EQUAL,
145 mapi T_MUL_EQUAL => self::T_MUL_EQUAL,
146 85 mapi T_NAMESPACE => self::T_NAMESPACE,
147 81 mapi T_XOR_EQUAL => self::T_XOR_EQUAL,
148 mapi T_INTERFACE => self::T_INTERFACE,
149 mapi T_BOOL_CAST => self::T_BOOL_CAST,
150 106 mapi T_CHARACTER => self::T_CHARACTER,
151 81 mapi T_CLOSE_TAG => self::T_CLOSE_TAG,
152 mapi T_PROTECTED => self::T_PROTECTED,
153 mapi T_CURLY_OPEN => self::T_CURLY_BRACE_OPEN,
154 mapi T_IMPLEMENTS => self::T_IMPLEMENTS,
155 mapi T_NUM_STRING => self::T_NUM_STRING,
156 mapi T_PLUS_EQUAL => self::T_PLUS_EQUAL,
157 mapi T_ARRAY_CAST => self::T_ARRAY_CAST,
158 mapi T_BOOLEAN_OR => self::T_BOOLEAN_OR,
159 mapi T_INSTANCEOF => self::T_INSTANCEOF,
160 mapi T_LOGICAL_OR => self::T_LOGICAL_OR,
161 mapi T_UNSET_CAST => self::T_UNSET_CAST,
162 mapi T_DOC_COMMENT => self::T_DOC_COMMENT,
163 mapi T_END_HEREDOC => self::T_END_HEREDOC,
164 mapi T_MINUS_EQUAL => self::T_MINUS_EQUAL,
165 mapi T_BOOLEAN_AND => self::T_BOOLEAN_AND,
166 mapi T_DOUBLE_CAST => self::T_DOUBLE_CAST,
167 mapi T_INLINE_HTML => self::T_INLINE_HTML,
168 mapi T_LOGICAL_AND => self::T_LOGICAL_AND,
169 mapi T_LOGICAL_XOR => self::T_LOGICAL_XOR,
170 mapi T_OBJECT_CAST => self::T_OBJECT_CAST,
171 mapi T_STRING_CAST => self::T_STRING_CAST,
172 mapi T_DOUBLE_ARROW => self::T_DOUBLE_ARROW,
173 mapi T_INCLUDE_ONCE => self::T_INCLUDE_ONCE,
174 mapi T_IS_IDENTICAL => self::T_IS_IDENTICAL,
175 mapi T_DOUBLE_COLON => self::T_DOUBLE_COLON,
176 mapi T_CONCAT_EQUAL => self::T_CONCAT_EQUAL,
177 mapi T_IS_NOT_EQUAL => self::T_IS_NOT_EQUAL,
178 mapi T_REQUIRE_ONCE => self::T_REQUIRE_ONCE,
179 106 mapi T_BAD_CHARACTER => self::T_BAD_CHARACTER,
180 81 mapi T_HALT_COMPILER => self::T_HALT_COMPILER,
181 mapi T_START_HEREDOC => self::T_START_HEREDOC,
182 mapi T_STRING_VARNAME => self::T_STRING_VARNAME,
183 mapi T_OBJECT_OPERATOR => self::T_OBJECT_OPERATOR,
184 mapi T_IS_NOT_IDENTICAL => self::T_IS_NOT_IDENTICAL,
185 mapi T_OPEN_TAG_WITH_ECHO => self::T_OPEN_TAG_WITH_ECHO,
186 mapi T_IS_GREATER_OR_EQUAL => self::T_IS_GREATER_OR_EQUAL,
187 mapi T_IS_SMALLER_OR_EQUAL => self::T_IS_SMALLER_OR_EQUAL,
188 mapi T_PAAMAYIM_NEKUDOTAYIM => self::T_DOUBLE_COLON,
189 105 mapi T_ENCAPSED_AND_WHITESPACE => self::T_ENCAPSED_AND_WHITESPACE,
190 81 mapi T_CONSTANT_ENCAPSED_STRING => self::T_CONSTANT_ENCAPSED_STRING,
191 1036 mapi //T_DOLLAR_OPEN_CURLY_BRACES => self::T_CURLY_BRACE_OPEN,
192 1 mapi );
193 525 mapi
194 10 mapi /**
195 mapi * Mapping between php internal text tokens an php depend numeric tokens.
196 mapi *
197 mapi * @var array(string=>integer) $literalMap
198 mapi */
199 1 mapi protected static $literalMap = array(
200 81 mapi '@' => self::T_AT,
201 mapi '/' => self::T_DIV,
202 mapi '%' => self::T_MOD,
203 mapi '*' => self::T_MUL,
204 mapi '+' => self::T_PLUS,
205 mapi ':' => self::T_COLON,
206 mapi ',' => self::T_COMMA,
207 mapi '=' => self::T_EQUAL,
208 mapi '-' => self::T_MINUS,
209 mapi '.' => self::T_CONCAT,
210 105 mapi '$' => self::T_DOLLAR,
211 237 mapi '`' => self::T_BACKTICK,
212 239 mapi '\\' => self::T_BACKSLASH,
213 81 mapi ';' => self::T_SEMICOLON,
214 mapi '|' => self::T_BITWISE_OR,
215 mapi '&' => self::T_BITWISE_AND,
216 mapi '~' => self::T_BITWISE_NOT,
217 mapi '^' => self::T_BITWISE_XOR,
218 mapi '"' => self::T_DOUBLE_QUOTE,
219 mapi '?' => self::T_QUESTION_MARK,
220 mapi '!' => self::T_EXCLAMATION_MARK,
221 mapi '{' => self::T_CURLY_BRACE_OPEN,
222 mapi '}' => self::T_CURLY_BRACE_CLOSE,
223 mapi '(' => self::T_PARENTHESIS_OPEN,
224 mapi ')' => self::T_PARENTHESIS_CLOSE,
225 mapi '<' => self::T_ANGLE_BRACKET_OPEN,
226 mapi '>' => self::T_ANGLE_BRACKET_CLOSE,
227 mapi '[' => self::T_SQUARED_BRACKET_OPEN,
228 mapi ']' => self::T_SQUARED_BRACKET_CLOSE,
229 mapi 'use' => self::T_USE,
230 544 mapi 'goto' => self::T_GOTO,
231 81 mapi 'null' => self::T_NULL,
232 171 mapi 'self' => self::T_SELF,
233 81 mapi 'true' => self::T_TRUE,
234 mapi 'array' => self::T_ARRAY,
235 mapi 'false' => self::T_FALSE,
236 mapi 'parent' => self::T_PARENT,
237 774 mapi 'namespace' => self::T_NAMESPACE,
238 557 mapi '__dir__' => self::T_DIR,
239 mapi '__namespace__' => self::T_NS_C,
240 1 mapi );
241 525 mapi
242 10 mapi /**
243 1037 mapi *
244 mapi * @var array(mixed=>array)
245 mapi */
246 mapi protected static $substituteTokens = array(
247 mapi T_DOLLAR_OPEN_CURLY_BRACES => array('$', '{'),
248 mapi );
249 mapi
250 mapi /**
251 793 mapi * Context sensitive alternative mappings.
252 mapi *
253 mapi * @var array(integer=>array) $alternativeMap
254 mapi */
255 mapi protected static $alternativeMap = array(
256 mapi self::T_USE => array(
257 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
258 795 mapi self::T_DOUBLE_COLON => self::T_STRING,
259 797 mapi self::T_CONST => self::T_STRING,
260 871 mapi self::T_FUNCTION => self::T_STRING,
261 793 mapi ),
262 mapi self::T_GOTO => array(
263 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
264 795 mapi self::T_DOUBLE_COLON => self::T_STRING,
265 797 mapi self::T_CONST => self::T_STRING,
266 871 mapi self::T_FUNCTION => self::T_STRING,
267 793 mapi ),
268 mapi self::T_NULL => array(
269 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
270 794 mapi self::T_DOUBLE_COLON => self::T_STRING,
271 797 mapi self::T_CONST => self::T_STRING,
272 871 mapi self::T_FUNCTION => self::T_STRING,
273 793 mapi ),
274 mapi self::T_SELF => array(
275 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
276 794 mapi self::T_DOUBLE_COLON => self::T_STRING,
277 797 mapi self::T_CONST => self::T_STRING,
278 871 mapi self::T_FUNCTION => self::T_STRING,
279 793 mapi ),
280 mapi self::T_TRUE => array(
281 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
282 794 mapi self::T_DOUBLE_COLON => self::T_STRING,
283 797 mapi self::T_CONST => self::T_STRING,
284 871 mapi self::T_FUNCTION => self::T_STRING,
285 793 mapi ),
286 mapi self::T_ARRAY => array(
287 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
288 mapi ),
289 mapi self::T_FALSE => array(
290 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
291 794 mapi self::T_DOUBLE_COLON => self::T_STRING,
292 797 mapi self::T_CONST => self::T_STRING,
293 871 mapi self::T_FUNCTION => self::T_STRING,
294 793 mapi ),
295 mapi self::T_PARENT => array(
296 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
297 794 mapi self::T_DOUBLE_COLON => self::T_STRING,
298 797 mapi self::T_CONST => self::T_STRING,
299 793 mapi ),
300 mapi self::T_NAMESPACE => array(
301 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
302 795 mapi self::T_DOUBLE_COLON => self::T_STRING,
303 797 mapi self::T_CONST => self::T_STRING,
304 871 mapi self::T_FUNCTION => self::T_STRING,
305 793 mapi ),
306 mapi self::T_DIR => array(
307 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
308 795 mapi self::T_DOUBLE_COLON => self::T_STRING,
309 797 mapi self::T_CONST => self::T_STRING,
310 871 mapi self::T_FUNCTION => self::T_STRING,
311 793 mapi ),
312 mapi self::T_NS_C => array(
313 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
314 795 mapi self::T_DOUBLE_COLON => self::T_STRING,
315 797 mapi self::T_CONST => self::T_STRING,
316 871 mapi self::T_FUNCTION => self::T_STRING,
317 793 mapi ),
318 954 mapi self::T_PARENT => array(
319 mapi self::T_OBJECT_OPERATOR => self::T_STRING,
320 mapi self::T_DOUBLE_COLON => self::T_STRING,
321 mapi self::T_CONST => self::T_STRING,
322 mapi self::T_FUNCTION => self::T_STRING,
323 mapi ),
324 793 mapi );
325 mapi
326 mapi /**
327 130 mapi * The source file instance.
328 13 mapi *
329 130 mapi * @var PHP_Depend_Code_File $sourceFile
330 13 mapi */
331 14 mapi protected $sourceFile = '';
332 525 mapi
333 13 mapi /**
334 10 mapi * Count of all tokens.
335 mapi *
336 mapi * @var integer $count
337 mapi */
338 1 mapi protected $count = 0;
339 10 mapi
340 mapi /**
341 mapi * Internal stream pointer index.
342 mapi *
343 mapi * @var integer $index
344 mapi */
345 1 mapi protected $index = 0;
346 525 mapi
347 10 mapi /**
348 mapi * Prepared token list.
349 mapi *
350 562 mapi * @var array(PHP_Depend_Token) $tokens
351 10 mapi */
352 1 mapi protected $tokens = array();
353 525 mapi
354 10 mapi /**
355 248 mapi * The next free identifier for unknown string tokens.
356 mapi *
357 mapi * @var integer $_unknownTokenID
358 mapi */
359 mapi private $_unknownTokenID = 1000;
360 525 mapi
361 248 mapi /**
362 13 mapi * Returns the name of the source file.
363 mapi *
364 150 mapi * @return PHP_Depend_Code_File
365 13 mapi */
366 14 mapi public function getSourceFile()
367 13 mapi {
368 14 mapi return $this->sourceFile;
369 13 mapi }
370 525 mapi
371 13 mapi /**
372 211 mapi * Sets a new php source file.
373 mapi *
374 mapi * @param string $sourceFile A php source file.
375 525 mapi *
376 211 mapi * @return void
377 mapi */
378 mapi public function setSourceFile($sourceFile)
379 mapi {
380 mapi $this->sourceFile = new PHP_Depend_Code_File($sourceFile);
381 561 mapi $this->_tokenize();
382 322 mapi $this->sourceFile->setTokens($this->tokens);
383 211 mapi }
384 525 mapi
385 211 mapi /**
386 558 mapi * Returns the next token or {@link PHP_Depend_TokenizerI::T_EOF} if
387 10 mapi * there is no next token.
388 mapi *
389 562 mapi * @return PHP_Depend_Token|integer
390 10 mapi */
391 1 mapi public function next()
392 mapi {
393 mapi if ($this->index < $this->count) {
394 mapi return $this->tokens[$this->index++];
395 mapi }
396 mapi return self::T_EOF;
397 mapi }
398 525 mapi
399 10 mapi /**
400 558 mapi * Returns the next token type or {@link PHP_Depend_TokenizerI::T_EOF} if
401 10 mapi * there is no next token.
402 mapi *
403 mapi * @return integer
404 mapi */
405 1 mapi public function peek()
406 mapi {
407 557 mapi if (isset($this->tokens[$this->index])) {
408 562 mapi return $this->tokens[$this->index]->type;
409 1 mapi }
410 mapi return self::T_EOF;
411 mapi }
412 525 mapi
413 10 mapi /**
414 1178 mapi * Returns the type of next token, after the current token. This method
415 mapi * ignores all comments between the current and the next token.
416 mapi *
417 mapi * @return integer
418 mapi * @since 0.9.12
419 mapi */
420 mapi public function peekNext()
421 mapi {
422 mapi $offset = 0;
423 mapi do {
424 mapi $type = $this->tokens[$this->index + ++$offset]->type;
425 mapi } while ($type == self::T_COMMENT || $type == self::T_DOC_COMMENT);
426 mapi return $type;
427 mapi }
428 mapi
429 mapi /**
430 558 mapi * Returns the previous token type or {@link PHP_Depend_TokenizerI::T_BOF}
431 154 mapi * if there is no previous token.
432 mapi *
433 mapi * @return integer
434 mapi */
435 mapi public function prev()
436 mapi {
437 156 mapi if ($this->index > 1) {
438 562 mapi return $this->tokens[$this->index - 2]->type;
439 154 mapi }
440 157 mapi return self::T_BOF;
441 154 mapi }
442 525 mapi
443 154 mapi /**
444 1037 mapi * This method takes an array of tokens returned by <b>token_get_all()</b>
445 mapi * and substitutes some of the tokens with those required by PHP_Depend's
446 mapi * parser implementation.
447 mapi *
448 mapi * @param array(array) $tokens Unprepared array of php tokens.
449 mapi *
450 mapi * @return array(array)
451 mapi */
452 mapi private function _substituteTokens(array $tokens)
453 mapi {
454 mapi $result = array();
455 mapi foreach ($tokens as $token) {
456 mapi $temp = (array) $token;
457 mapi $temp = $temp[0];
458 mapi if (isset(self::$substituteTokens[$temp])) {
459 mapi foreach (self::$substituteTokens[$temp] as $token) {
460 mapi $result[] = $token;
461 mapi }
462 mapi } else {
463 mapi $result[] = $token;
464 mapi }
465 mapi }
466 mapi return $result;
467 mapi }
468 mapi
469 mapi /**
470 13 mapi * Tokenizes the content of the source file with {@link token_get_all()} and
471 10 mapi * filters this token stream.
472 525 mapi *
473 10 mapi * @return void
474 mapi */
475 561 mapi private function _tokenize() pdepend-warning pdepend-warning pdepend-error pdepend-error pdepend-error pdepend-error
476 1 mapi {
477 211 mapi $this->tokens = array();
478 mapi $this->index = 0;
479 mapi $this->count = 0;
480 525 mapi
481 641 mapi // Replace short open tags, short open tags will produce invalid results
482 mapi // in all environments with disabled short open tags.
483 285 mapi $source = $this->sourceFile->getSource();
484 810 mapi $source = preg_replace(
485 1266 mapi array('(<\?=)', '(<\?(\s))'),
486 mapi array('<?php echo ', '<?php\1'),
487 810 mapi $source
488 mapi );
489 525 mapi
490 538 mapi if (version_compare(phpversion(), '5.3.0alpha3') < 0) {
491 1266 mapi $tokens = PHP_Depend_Tokenizer_PHP52Helper::tokenize($source);
492 538 mapi } else {
493 1037 mapi $tokens = token_get_all($source);
494 538 mapi }
495 mapi
496 1037 mapi $tokens = $this->_substituteTokens($tokens);
497 1036 mapi
498 641 mapi // Is the current token between an opening and a closing php tag?
499 mapi $inTag = false;
500 mapi
501 285 mapi // The current line number
502 561 mapi $startLine = 1;
503 525 mapi
504 642 mapi $startColumn = 1;
505 mapi $endColumn = 1;
506 285 mapi
507 557 mapi $literalMap = self::$literalMap;
508 mapi $tokenMap = self::$tokenMap;
509 mapi
510 793 mapi // Previous found type
511 mapi $previousType = null;
512 mapi
513 561 mapi while ($token = current($tokens)) {
514 562 mapi $type = null;
515 mapi $image = null;
516 641 mapi
517 1 mapi if (is_string($token)) {
518 641 mapi $token = array(null, $token);
519 mapi }
520 mapi
521 mapi if ($token[0] === T_OPEN_TAG) {
522 mapi $type = $tokenMap[$token[0]];
523 mapi $image = $token[1];
524 mapi $inTag = true;
525 285 mapi } else if ($token[0] === T_CLOSE_TAG) {
526 562 mapi $type = $tokenMap[$token[0]];
527 mapi $image = $token[1];
528 641 mapi $inTag = false;
529 mapi } else if ($inTag === false) {
530 mapi $type = self::T_NO_PHP;
531 mapi $image = $this->_consumeNonePhpTokens($tokens);
532 84 mapi } else if ($token[0] === T_WHITESPACE) {
533 642 mapi // Count newlines in token
534 mapi $lines = substr_count($token[1], "\n");
535 mapi if ($lines === 0) {
536 mapi $startColumn += strlen($token[1]);
537 mapi } else {
538 815 mapi $startColumn = strlen(
539 mapi substr($token[1], strrpos($token[1], "\n") + 1)
540 mapi ) + 1;
541 642 mapi }
542 mapi
543 mapi $startLine += $lines;
544 84 mapi } else {
545 1 mapi $value = strtolower($token[1]);
546 557 mapi if (isset($literalMap[$value])) {
547 793 mapi // Fetch literal type
548 mapi $type = $literalMap[$value];
549 871 mapi
550 793 mapi // Check for a context sensitive alternative
551 mapi if (isset(self::$alternativeMap[$type][$previousType])) {
552 mapi $type = self::$alternativeMap[$type][$previousType];
553 mapi }
554 mapi $image = $token[1];
555 557 mapi } else if (isset($tokenMap[$token[0]])) {
556 797 mapi $type = $tokenMap[$token[0]];
557 mapi // Check for a context sensitive alternative
558 mapi if (isset(self::$alternativeMap[$type][$previousType])) {
559 mapi $type = self::$alternativeMap[$type][$previousType];
560 mapi }
561 mapi
562 562 mapi $image = $token[1];
563 84 mapi } else {
564 88 mapi // This should never happen
565 mapi // @codeCoverageIgnoreStart
566 562 mapi list($type, $image) = $this->_generateUnknownToken($token[1]);
567 88 mapi // @codeCoverageIgnoreEnd
568 1 mapi }
569 mapi }
570 525 mapi
571 562 mapi if ($type) {
572 1026 mapi $rtrim = rtrim($image);
573 mapi $lines = substr_count($rtrim, "\n");
574 642 mapi if ($lines === 0) {
575 1026 mapi $endColumn = $startColumn + strlen($rtrim) - 1;
576 642 mapi } else {
577 815 mapi $endColumn = strlen(
578 1026 mapi substr($rtrim, strrpos($rtrim, "\n") + 1)
579 815 mapi );
580 642 mapi }
581 525 mapi
582 642 mapi $endLine = $startLine + $lines;
583 525 mapi
584 916 mapi $token = new PHP_Depend_Token(
585 mapi $type,
586 1026 mapi $rtrim,
587 916 mapi $startLine,
588 mapi $endLine,
589 mapi $startColumn,
590 mapi $endColumn
591 mapi );
592 642 mapi
593 562 mapi // Store token in internal list
594 mapi $this->tokens[] = $token;
595 645 mapi
596 642 mapi // Count newlines in token
597 mapi $lines = substr_count($image, "\n");
598 mapi if ($lines === 0) {
599 mapi $startColumn += strlen($image);
600 mapi } else {
601 815 mapi $startColumn = strlen(
602 mapi substr($image, strrpos($image, "\n") + 1)
603 mapi ) + 1;
604 642 mapi }
605 562 mapi
606 642 mapi $startLine += $lines;
607 797 mapi
608 mapi // Store current type
609 mapi if ($type !== self::T_COMMENT && $type !== self::T_DOC_COMMENT) {
610 mapi $previousType = $type;
611 mapi }
612 40 mapi }
613 525 mapi
614 285 mapi next($tokens);
615 1 mapi }
616 525 mapi
617 1 mapi $this->count = count($this->tokens);
618 mapi }
619 525 mapi
620 539 mapi /**
621 641 mapi * This method fetches all tokens until an opening php tag was found and it
622 mapi * returns the collected content. The returned value will be null if there
623 mapi * was no none php token.
624 557 mapi *
625 595 mapi * @param array &$tokens Reference to the current token stream.
626 557 mapi *
627 641 mapi * @return string
628 557 mapi */
629 641 mapi private function _consumeNonePhpTokens(array &$tokens) pdepend-warning
630 557 mapi {
631 641 mapi // The collected token content
632 mapi $content = null;
633 557 mapi
634 641 mapi // Fetch current token
635 mapi $token = (array) current($tokens);
636 557 mapi
637 mapi // Skipp all non open tags
638 mapi while ($token[0] !== T_OPEN_TAG_WITH_ECHO &&
639 mapi $token[0] !== T_OPEN_TAG &&
640 mapi $token[0] !== false) {
641 mapi
642 641 mapi $content .= (isset($token[1]) ? $token[1] : $token[0]);
643 557 mapi
644 mapi $token = (array) next($tokens);
645 mapi }
646 mapi
647 641 mapi // Set internal pointer one back when there was at least one none php token
648 mapi if ($token[0] !== false) {
649 mapi prev($tokens);
650 mapi }
651 557 mapi
652 641 mapi return $content;
653 557 mapi }
654 mapi
655 mapi /**
656 248 mapi * Generates a dummy/temp token for unknown string literals.
657 525 mapi *
658 248 mapi * @param string $token The unknown string token.
659 mapi *
660 mapi * @return array(integer => mixed)
661 mapi */
662 mapi private function _generateUnknownToken($token)
663 mapi {
664 mapi return array($this->_unknownTokenID++, $token);
665 mapi }
666 1266 mapi }