Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.0: handle changed tokenization of namespaced names #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 55 additions & 40 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,46 +118,8 @@ private function tokenize($source)

foreach ($tokens as $token) {
if (is_array($token)) {
switch ($token[0]) {
case T_WHITESPACE:
break;

case T_OPEN_TAG:
case T_OPEN_TAG_WITH_ECHO:
case T_CLOSE_TAG:
case T_STRING:
case T_VARIABLE:

// Constants
case T_DIR:
case T_FILE:
case T_METHOD_C:
case T_DNUMBER:
case T_LNUMBER:
case T_NS_C:
case T_LINE:
case T_CLASS_C:
case T_FUNC_C:
case T_TRAIT_C:
$newType = self::TOKEN_DEFAULT;
break;

case T_COMMENT:
case T_DOC_COMMENT:
$newType = self::TOKEN_COMMENT;
break;

case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$newType = self::TOKEN_STRING;
break;

case T_INLINE_HTML:
$newType = self::TOKEN_HTML;
break;

default:
$newType = self::TOKEN_KEYWORD;
if ($token[0] !== T_WHITESPACE) {
$newType = $this->getTokenType($token);
}
} else {
$newType = $token === '"' ? self::TOKEN_STRING : self::TOKEN_KEYWORD;
Expand All @@ -183,6 +145,59 @@ private function tokenize($source)
return $output;
}

/**
* @param array $arrayToken
* @return string
*/
private function getTokenType($arrayToken)
{
switch ($arrayToken[0]) {
case T_OPEN_TAG:
case T_OPEN_TAG_WITH_ECHO:
case T_CLOSE_TAG:
case T_STRING:
case T_VARIABLE:

// Constants
case T_DIR:
case T_FILE:
case T_METHOD_C:
case T_DNUMBER:
case T_LNUMBER:
case T_NS_C:
case T_LINE:
case T_CLASS_C:
case T_FUNC_C:
case T_TRAIT_C:
return self::TOKEN_DEFAULT;

case T_COMMENT:
case T_DOC_COMMENT:
return self::TOKEN_COMMENT;

case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
return self::TOKEN_STRING;

case T_INLINE_HTML:
return self::TOKEN_HTML;
}

// Handle PHP >= 8.0 namespaced name tokens.
// https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.tokenizer
if (defined('T_NAME_QUALIFIED') && $arrayToken[0] === T_NAME_QUALIFIED) {
return self::TOKEN_DEFAULT;
}
if (defined('T_NAME_FULLY_QUALIFIED') && $arrayToken[0] === T_NAME_FULLY_QUALIFIED) {
return self::TOKEN_DEFAULT;
}
if (defined('T_NAME_RELATIVE') && $arrayToken[0] === T_NAME_RELATIVE) {
return self::TOKEN_DEFAULT;
}

return self::TOKEN_KEYWORD;
}

/**
* @param array $tokens
* @return array
Expand Down
81 changes: 81 additions & 0 deletions tests/HighlighterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,85 @@ public function testWhitespace()
'<token_html> </token_html>'
);
}

public function testFunctionCall()
{
$this->compare(
<<<'EOL'
<?php
echo functionName();
EOL
,
<<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo </token_keyword><token_default>functionName</token_default><token_keyword>();</token_keyword>
EOL
);
}

public function testFQNFunctionCall()
{
$original = <<<'EOL'
<?php
echo \My\Package\functionName();
EOL;

if (PHP_VERSION_ID < 80000) {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo \</token_keyword><token_default>My</token_default><token_keyword>\</token_keyword><token_default>Package</token_default><token_keyword>\</token_keyword><token_default>functionName</token_default><token_keyword>();</token_keyword>
EOL;
} else {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo </token_keyword><token_default>\My\Package\functionName</token_default><token_keyword>();</token_keyword>
EOL;
}

$this->compare($original, $expected);
}

public function testNamespaceRelativeFunctionCall()
{
$original = <<<'EOL'
<?php
echo namespace\functionName();
EOL;

if (PHP_VERSION_ID < 80000) {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo namespace\</token_keyword><token_default>functionName</token_default><token_keyword>();</token_keyword>
EOL;
} else {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo </token_keyword><token_default>namespace\functionName</token_default><token_keyword>();</token_keyword>
EOL;
}

$this->compare($original, $expected);
}

public function testQualifiedFunctionCall()
{
$original = <<<'EOL'
<?php
echo Package\functionName();
EOL;

if (PHP_VERSION_ID < 80000) {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo </token_keyword><token_default>Package</token_default><token_keyword>\</token_keyword><token_default>functionName</token_default><token_keyword>();</token_keyword>
EOL;
} else {
$expected = <<<'EOL'
<token_default><?php</token_default>
<token_keyword>echo </token_keyword><token_default>Package\functionName</token_default><token_keyword>();</token_keyword>
EOL;
}

$this->compare($original, $expected);
}
}