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