public function testWhitespaceToken() { $sample = $this->getSample('class'); $tokenizer = new PhpTokenizer(); $tokens = $tokenizer->tokenize($sample); $this->assertTrue($tokens->get(1)->type == T_WHITESPACE); }
public static function getUseStatements(\ReflectionClass $class) { $content = ''; $file = file($class->getFileName()); for ($i = 0; $i < $class->getStartLine(); $i++) { $content .= $file[$i]; } $tokenizer = new PhpTokenizer(); $tokens = $tokenizer->tokenize($content); $tokens = $tokens->filter(function ($token) { return $token->type !== T_WHITESPACE && $token->type !== T_COMMENT && $token->type !== T_DOC_COMMENT; }); $statements = []; while ($token = $tokens->next()) { if ($token->type === T_USE) { $explicitAlias = false; $alias = ''; $class = ''; while ($token = $tokens->next()) { $isNameToken = $token->type === T_STRING || $token->type === T_NS_SEPARATOR; if (!$explicitAlias && $isNameToken) { $class .= $token->contents; } else { if ($explicitAlias && $isNameToken) { $alias .= $token->contents; } else { if ($token->type === T_AS) { $explicitAlias = true; $alias = ''; } else { if ($token->contents === ',') { if ($explicitAlias) { $statements[$alias] = $class; } else { $statements[] = $class; } $class = ''; $alias = ''; $explicitAlias = false; } else { if ($token->contents === ';') { if ($explicitAlias) { $statements[$alias] = $class; } else { $statements[] = $class; } break; } else { break; } } } } } } } } return $statements; }