Arbit - project tracking

PHP Depend

History

Diff

1036 1037 /tests/PHP/Depend/_code/tokenizer/testTokenizerSubstitutesDollarCurlyOpenWithTwoSeparateTokens.php
2 +<?php
3 +${$foo . $bar};
1036 1037 /tests/PHP/Depend/Code/ASTCompoundVariableTest.php
96 96 $variable = $this->_getFirstVariableInFunction(__METHOD__);
97 97
98 98 $literal = $variable->getChild(0)->getChild(0);
99 - $this->assertType(PHP_Depend_Code_ASTLiteral::CLAZZ, $literal);
100 99 $this->assertSame("'FOO{\$bar}'", $literal->getImage());
101 100 }
102 101
1036 1037 /tests/PHP/Depend/Tokenizer/InternalTest.php
556 556
557 557 $this->assertSame($expected, $actual);
558 558 }
559 +
560 + /**
561 + * testTokenizerSubstitutesDollarCurlyOpenWithTwoSeparateTokens
562 + *
563 + * @return void
564 + * @covers PHP_Depend_Tokenizer_Internal
565 + * @group pdepend
566 + * @group pdepend::tokenizer
567 + * @group unittest
568 + */
569 + public function testTokenizerSubstitutesDollarCurlyOpenWithTwoSeparateTokens()
570 + {
571 + $tokenizer = new PHP_Depend_Tokenizer_Internal();
572 + $tokenizer->setSourceFile(
573 + self::createCodeResourceURI('tokenizer/' . __FUNCTION__ . '.php')
574 + );
575 +
576 + $actual = array();
577 + while (is_object($token = $tokenizer->next())) {
578 + $actual[] = array($token->type, $token->startColumn, $token->endColumn);
579 + }
580 +
581 + $expected = array(
582 + array(PHP_Depend_ConstantsI::T_OPEN_TAG, 1, 5),
583 + array(PHP_Depend_ConstantsI::T_DOLLAR, 1, 1),
584 + array(PHP_Depend_ConstantsI::T_CURLY_BRACE_OPEN, 2, 2),
585 + array(PHP_Depend_ConstantsI::T_VARIABLE, 3, 6),
586 + array(PHP_Depend_ConstantsI::T_CONCAT, 8, 8),
587 + array(PHP_Depend_ConstantsI::T_VARIABLE, 10, 13),
588 + array(PHP_Depend_ConstantsI::T_CURLY_BRACE_CLOSE, 14, 14),
589 + array(PHP_Depend_ConstantsI::T_SEMICOLON, 15, 15),
590 + );
591 +
592 + $this->assertEquals($expected, $actual);
593 + }
559 594 }
1036 1037 /PHP/Depend/Tokenizer/Internal.php
69 69 class PHP_Depend_Tokenizer_Internal
70 70 implements PHP_Depend_TokenizerI
71 71 {
72 - protected static $substitutes = array(
73 - T_DOLLAR_OPEN_CURLY_BRACES => array('$', '{'),
74 - );
75 -
76 72 /**
77 73 * Mapping between php internal tokens and php depend tokens.
78 74 *
245 241 );
246 242
247 243 /**
244 + *
245 + * @var array(mixed=>array)
246 + */
247 + protected static $substituteTokens = array(
248 + T_DOLLAR_OPEN_CURLY_BRACES => array('$', '{'),
249 + );
250 +
251 + /**
248 252 * Context sensitive alternative mappings.
249 253 *
250 254 * @var array(integer=>array) $alternativeMap
422 426 }
423 427
424 428 /**
429 + * This method takes an array of tokens returned by <b>token_get_all()</b>
430 + * and substitutes some of the tokens with those required by PHP_Depend's
431 + * parser implementation.
432 + *
433 + * @param array(array) $tokens Unprepared array of php tokens.
434 + *
435 + * @return array(array)
436 + */
437 + private function _substituteTokens(array $tokens)
438 + {
439 + $result = array();
440 + foreach ($tokens as $token) {
441 + $temp = (array) $token;
442 + $temp = $temp[0];
443 + if (isset(self::$substituteTokens[$temp])) {
444 + foreach (self::$substituteTokens[$temp] as $token) {
445 + $result[] = $token;
446 + }
447 + } else {
448 + $result[] = $token;
449 + }
450 + }
451 + return $result;
452 + }
453 +
454 + /**
425 455 * Tokenizes the content of the source file with {@link token_get_all()} and
426 456 * filters this token stream.
427 457 *
443 473 );
444 474
445 475 if (version_compare(phpversion(), '5.3.0alpha3') < 0) {
446 - $tempTokens = PHP_Depend_Tokenizer_PHP53NamespaceHelper::tokenize($source);
476 + $tokens = PHP_Depend_Tokenizer_PHP53NamespaceHelper::tokenize($source);
447 477 } else {
448 - $tempTokens = token_get_all($source);
478 + $tokens = token_get_all($source);
449 479 }
450 480
451 - $tokens = array();
452 - foreach ($tempTokens as $tempToken) {
453 - $temp = (array) $tempToken;
454 - $temp = $temp[0];
455 - if (isset(self::$substitutes[$temp])) {
456 - foreach (self::$substitutes[$temp] as $token) {
457 - $tokens[] = $token;
458 - }
459 - } else {
460 - $tokens[] = $tempToken;
461 - }
462 - }
481 + $tokens = $this->_substituteTokens($tokens);
463 482
464 -
465 - reset($tokens);
466 -
467 483 // Is the current token between an opening and a closing php tag?
468 484 $inTag = false;
469 485