lithium\analysis\Parser::token PHP Method

token() public static method

Convenience method to get the token name of a PHP code string. If multiple tokens are present in the string, only the first is returned.
public static token ( string $string, array $options = [] ) : mixed
$string string String of PHP code to get the token name of, i.e. `'=>'` or `'static'`.
$options array
return mixed
    public static function token($string, array $options = array())
    {
        $defaults = array('id' => false);
        $options += $defaults;
        if (empty($string) && $string !== '0') {
            return false;
        }
        list($token) = static::tokenize($string);
        return $token[$options['id'] ? 'id' : 'name'];
    }

Usage Example

Example #1
0
 /**
  * Tests that PHP code snippets properly resolve to their corresponding tokens.
  *
  * @return void
  */
 public function testSingleTokenization()
 {
     $result = Parser::token('static');
     $this->assertEqual('T_STATIC', $result);
     $result = Parser::token('=>');
     $this->assertEqual('T_DOUBLE_ARROW', $result);
     $result = Parser::token(' =>');
     $this->assertEqual('T_WHITESPACE', $result);
     $result = Parser::token('static =>');
     $this->assertEqual('T_STATIC', $result);
     $result = Parser::token("\nstatic =>");
     $this->assertEqual('T_WHITESPACE', $result);
     $this->assertFalse(Parser::token(''));
     $result = Parser::token(';');
     $this->assertEqual(';', $result);
     $result = Parser::token('"string"');
     $this->assertEqual('T_CONSTANT_ENCAPSED_STRING', $result);
     $result = Parser::token('1');
     $this->assertEqual('T_LNUMBER', $result);
     $result = Parser::token('0');
     $this->assertEqual('T_LNUMBER', $result);
     $result = Parser::token('0');
     $this->assertEqual('T_LNUMBER', $result);
 }