PhpToken_methods.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --TEST--
  2. PhpToken instance methods
  3. --EXTENSIONS--
  4. tokenizer
  5. --FILE--
  6. <?php
  7. $code = <<<'PHP'
  8. <?php
  9. // comment
  10. /** comment */
  11. function foo() {
  12. echo "bar";
  13. }
  14. PHP;
  15. // Token names and ignorability.
  16. $tokens = PhpToken::tokenize($code);
  17. foreach ($tokens as $i => $token) {
  18. printf("[%2d] %-26s %s\n", $i, $token->getTokenName(),
  19. $token->isIgnorable() ? "ignorable" : "meaningful");
  20. }
  21. // is() variations
  22. $token = $tokens[5];
  23. echo "\nSuccess:\n";
  24. var_dump($token->is(T_FUNCTION));
  25. var_dump($token->is('function'));
  26. var_dump($token->is(['class', T_FUNCTION]));
  27. var_dump($token->is([T_CLASS, 'function']));
  28. echo "\nFailure:\n";
  29. var_dump($token->is(T_CLASS));
  30. var_dump($token->is('class'));
  31. var_dump($token->is(['class', T_TRAIT]));
  32. var_dump($token->is([T_CLASS, 'trait']));
  33. echo "\nError:\n";
  34. try {
  35. $token->is(3.141);
  36. } catch (TypeError $e) {
  37. echo $e->getMessage(), "\n";
  38. }
  39. try {
  40. $token->is([3.141]);
  41. } catch (TypeError $e) {
  42. echo $e->getMessage(), "\n";
  43. }
  44. unset($token->id);
  45. unset($token->text);
  46. try {
  47. $token->is(T_FUNCTION);
  48. } catch (Error $e) {
  49. echo $e->getMessage(), "\n";
  50. }
  51. try {
  52. $token->is('function');
  53. } catch (Error $e) {
  54. echo $e->getMessage(), "\n";
  55. }
  56. try {
  57. $token->is([T_FUNCTION]);
  58. } catch (Error $e) {
  59. echo $e->getMessage(), "\n";
  60. }
  61. try {
  62. $token->is(['function']);
  63. } catch (Error $e) {
  64. echo $e->getMessage(), "\n";
  65. }
  66. echo "\nName of unknown token:\n";
  67. $token = new PhpToken(100000, "foo");
  68. var_dump($token->getTokenName());
  69. ?>
  70. --EXPECT--
  71. [ 0] T_OPEN_TAG ignorable
  72. [ 1] T_COMMENT ignorable
  73. [ 2] T_WHITESPACE ignorable
  74. [ 3] T_DOC_COMMENT ignorable
  75. [ 4] T_WHITESPACE ignorable
  76. [ 5] T_FUNCTION meaningful
  77. [ 6] T_WHITESPACE ignorable
  78. [ 7] T_STRING meaningful
  79. [ 8] ( meaningful
  80. [ 9] ) meaningful
  81. [10] T_WHITESPACE ignorable
  82. [11] { meaningful
  83. [12] T_WHITESPACE ignorable
  84. [13] T_ECHO meaningful
  85. [14] T_WHITESPACE ignorable
  86. [15] T_CONSTANT_ENCAPSED_STRING meaningful
  87. [16] ; meaningful
  88. [17] T_WHITESPACE ignorable
  89. [18] } meaningful
  90. Success:
  91. bool(true)
  92. bool(true)
  93. bool(true)
  94. bool(true)
  95. Failure:
  96. bool(false)
  97. bool(false)
  98. bool(false)
  99. bool(false)
  100. Error:
  101. PhpToken::is(): Argument #1 ($kind) must be of type string|int|array, float given
  102. PhpToken::is(): Argument #1 ($kind) must only have elements of type string|int, float given
  103. Typed property PhpToken::$id must not be accessed before initialization
  104. Typed property PhpToken::$text must not be accessed before initialization
  105. Typed property PhpToken::$id must not be accessed before initialization
  106. Typed property PhpToken::$text must not be accessed before initialization
  107. Name of unknown token:
  108. NULL