Use fully-qualified names for global constant references

This commit is contained in:
Nikita Popov 2017-11-10 23:33:12 +01:00
parent 7162b36f2d
commit 68e9b91e9c
6 changed files with 42 additions and 42 deletions

View File

@ -138,7 +138,7 @@ class Comment implements \JsonSerializable
*/
private function getShortestWhitespacePrefixLen(string $str) : int {
$lines = explode("\n", $str);
$shortestPrefixLen = INF;
$shortestPrefixLen = \INF;
foreach ($lines as $line) {
preg_match('(^\s*)', $line, $matches);
$prefixLen = strlen($matches[0]);

View File

@ -34,7 +34,7 @@ class Lexer
// map of tokens to drop while lexing (the map is only used for isset lookup,
// that's why the value is simply set to 1; the value is never actually used.)
$this->dropTokens = array_fill_keys(
[T_WHITESPACE, T_OPEN_TAG, T_COMMENT, T_DOC_COMMENT], 1
[\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT], 1
);
// the usedAttributes member is a map of the used attribute names to a dummy
@ -112,7 +112,7 @@ class Lexer
* @return bool
*/
private function isUnterminatedComment($token) : bool {
return ($token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)
return ($token[0] === \T_COMMENT || $token[0] === \T_DOC_COMMENT)
&& substr($token[1], 0, 2) === '/*'
&& substr($token[1], -2) !== '*/';
}
@ -173,7 +173,7 @@ class Lexer
// Emulate the PHP behavior
$isDocComment = isset($comment[3]) && $comment[3] === '*';
$this->tokens[] = [$isDocComment ? T_DOC_COMMENT : T_COMMENT, $comment, $line];
$this->tokens[] = [$isDocComment ? \T_DOC_COMMENT : \T_COMMENT, $comment, $line];
} else {
// Invalid characters at the end of the input
$this->handleInvalidCharacterRange(
@ -253,18 +253,18 @@ class Lexer
} elseif (!isset($this->dropTokens[$token[0]])) {
$value = $token[1];
$id = $this->tokenMap[$token[0]];
if (T_CLOSE_TAG === $token[0]) {
if (\T_CLOSE_TAG === $token[0]) {
$this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
} else if (T_INLINE_HTML === $token[0]) {
} else if (\T_INLINE_HTML === $token[0]) {
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
}
$this->line += substr_count($value, "\n");
$this->filePos += \strlen($value);
} else {
if (T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0]) {
if (\T_COMMENT === $token[0] || \T_DOC_COMMENT === $token[0]) {
if (isset($this->usedAttributes['comments'])) {
$comment = T_DOC_COMMENT === $token[0]
$comment = \T_DOC_COMMENT === $token[0]
? new Comment\Doc($token[1], $this->line, $this->filePos, $this->pos)
: new Comment($token[1], $this->line, $this->filePos, $this->pos);
$startAttributes['comments'][] = $comment;
@ -344,13 +344,13 @@ class Lexer
// 256 is the minimum possible token number, as everything below
// it is an ASCII value
for ($i = 256; $i < 1000; ++$i) {
if (T_DOUBLE_COLON === $i) {
if (\T_DOUBLE_COLON === $i) {
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
$tokenMap[$i] = Tokens::T_PAAMAYIM_NEKUDOTAYIM;
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
} elseif(\T_OPEN_TAG_WITH_ECHO === $i) {
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
$tokenMap[$i] = Tokens::T_ECHO;
} elseif(T_CLOSE_TAG === $i) {
} elseif(\T_CLOSE_TAG === $i) {
// T_CLOSE_TAG is equivalent to ';'
$tokenMap[$i] = ord(';');
} elseif ('UNKNOWN' !== $name = token_name($i)) {
@ -366,11 +366,11 @@ class Lexer
// HHVM uses a special token for numbers that overflow to double
if (defined('T_ONUMBER')) {
$tokenMap[T_ONUMBER] = Tokens::T_DNUMBER;
$tokenMap[\T_ONUMBER] = Tokens::T_DNUMBER;
}
// HHVM also has a separate token for the __COMPILER_HALT_OFFSET__ constant
if (defined('T_COMPILER_HALT_OFFSET')) {
$tokenMap[T_COMPILER_HALT_OFFSET] = Tokens::T_STRING;
$tokenMap[\T_COMPILER_HALT_OFFSET] = Tokens::T_STRING;
}
return $tokenMap;

View File

@ -214,7 +214,7 @@ class NameContext {
// Find shortest name
$shortestName = null;
$shortestLength = INF;
$shortestLength = \INF;
foreach ($possibleNames as $possibleName) {
$length = strlen($possibleName->toCodeString());
if ($length < $shortestLength) {

View File

@ -888,7 +888,7 @@ class Standard extends PrettyPrinterAbstract
$oct = decoct(ord($matches[1]));
if ($matches[2] !== '') {
// If there is a trailing digit, use the full three character form
return '\\' . str_pad($oct, 3, '0', STR_PAD_LEFT);
return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT);
}
return '\\' . $oct;
}, $escaped);

View File

@ -1068,10 +1068,10 @@ abstract class PrettyPrinterAbstract
protected function initializeRemovalMap() {
if ($this->removalMap) return;
$stripBoth = ['left' => T_WHITESPACE, 'right' => T_WHITESPACE];
$stripLeft = ['left' => T_WHITESPACE];
$stripRight = ['right' => T_WHITESPACE];
$stripDoubleArrow = ['right' => T_DOUBLE_ARROW];
$stripBoth = ['left' => \T_WHITESPACE, 'right' => \T_WHITESPACE];
$stripLeft = ['left' => \T_WHITESPACE];
$stripRight = ['right' => \T_WHITESPACE];
$stripDoubleArrow = ['right' => \T_DOUBLE_ARROW];
$stripColon = ['left' => ':'];
$stripEquals = ['left' => '='];
$this->removalMap = [
@ -1086,8 +1086,8 @@ abstract class PrettyPrinterAbstract
'Param->default' => $stripEquals,
'Stmt_Break->num' => $stripBoth,
'Stmt_ClassMethod->returnType' => $stripColon,
'Stmt_Class->extends' => ['left' => T_EXTENDS],
'Expr_PrintableNewAnonClass->extends' => ['left' => T_EXTENDS],
'Stmt_Class->extends' => ['left' => \T_EXTENDS],
'Expr_PrintableNewAnonClass->extends' => ['left' => \T_EXTENDS],
'Stmt_Continue->num' => $stripBoth,
'Stmt_Foreach->keyVar' => $stripDoubleArrow,
'Stmt_Function->returnType' => $stripColon,
@ -1114,21 +1114,21 @@ abstract class PrettyPrinterAbstract
'Expr_ArrayItem->key' => [null, null, ' => '],
'Expr_Closure->returnType' => [')', ' : ', null],
'Expr_Ternary->if' => ['?', ' ', ' '],
'Expr_Yield->key' => [T_YIELD, null, ' => '],
'Expr_Yield->value' => [T_YIELD, ' ', null],
'Expr_Yield->key' => [\T_YIELD, null, ' => '],
'Expr_Yield->value' => [\T_YIELD, ' ', null],
'Param->type' => [null, null, ' '],
'Param->default' => [null, ' = ', null],
'Stmt_Break->num' => [T_BREAK, ' ', null],
'Stmt_Break->num' => [\T_BREAK, ' ', null],
'Stmt_ClassMethod->returnType' => [')', ' : ', null],
'Stmt_Class->extends' => [null, ' extends ', null],
'Expr_PrintableNewAnonClass->extends' => [null, ' extends ', null],
'Stmt_Continue->num' => [T_CONTINUE, ' ', null],
'Stmt_Foreach->keyVar' => [T_AS, null, ' => '],
'Stmt_Continue->num' => [\T_CONTINUE, ' ', null],
'Stmt_Foreach->keyVar' => [\T_AS, null, ' => '],
'Stmt_Function->returnType' => [')', ' : ', null],
'Stmt_If->else' => [null, ' ', null],
'Stmt_Namespace->name' => [T_NAMESPACE, ' ', null],
'Stmt_Namespace->name' => [\T_NAMESPACE, ' ', null],
'Stmt_PropertyProperty->default' => [null, ' = ', null],
'Stmt_Return->expr' => [T_RETURN, ' ', null],
'Stmt_Return->expr' => [\T_RETURN, ' ', null],
'Stmt_StaticVar->default' => [null, ' = ', null],
//'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, ' ', null], // TODO
'Stmt_TryCatch->finally' => [null, ' ', null],
@ -1214,10 +1214,10 @@ abstract class PrettyPrinterAbstract
if ($this->modifierChangeMap) return;
$this->modifierChangeMap = [
'Stmt_ClassConst->flags' => T_CONST,
'Stmt_ClassMethod->flags' => T_FUNCTION,
'Stmt_Class->flags' => T_CLASS,
'Stmt_Property->flags' => T_VARIABLE,
'Stmt_ClassConst->flags' => \T_CONST,
'Stmt_ClassMethod->flags' => \T_FUNCTION,
'Stmt_Class->flags' => \T_CLASS,
'Stmt_Property->flags' => \T_VARIABLE,
//'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
];

View File

@ -67,8 +67,8 @@ class TokenStream {
if ($tokenType === $expectedTokenType) {
return true;
}
if ($tokenType !== T_WHITESPACE
&& $tokenType !== T_COMMENT && $tokenType !== T_DOC_COMMENT) {
if ($tokenType !== \T_WHITESPACE
&& $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
break;
}
}
@ -93,8 +93,8 @@ class TokenStream {
if ($tokenType === $expectedTokenType) {
return true;
}
if ($tokenType !== T_WHITESPACE
&& $tokenType !== T_COMMENT && $tokenType !== T_DOC_COMMENT) {
if ($tokenType !== \T_WHITESPACE
&& $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
break;
}
}
@ -105,7 +105,7 @@ class TokenStream {
$tokens = $this->tokens;
$pos = $this->skipLeftWhitespace($pos);
if ($skipTokenType === T_WHITESPACE) {
if ($skipTokenType === \T_WHITESPACE) {
return $pos;
}
@ -122,7 +122,7 @@ class TokenStream {
$tokens = $this->tokens;
$pos = $this->skipRightWhitespace($pos);
if ($skipTokenType === T_WHITESPACE) {
if ($skipTokenType === \T_WHITESPACE) {
return $pos;
}
@ -145,7 +145,7 @@ class TokenStream {
$tokens = $this->tokens;
for (; $pos >= 0; $pos--) {
$type = $tokens[$pos][0];
if ($type !== T_WHITESPACE && $type !== T_COMMENT && $type !== T_DOC_COMMENT) {
if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
break;
}
}
@ -162,7 +162,7 @@ class TokenStream {
$tokens = $this->tokens;
for ($count = \count($tokens); $pos < $count; $pos++) {
$type = $tokens[$pos][0];
if ($type !== T_WHITESPACE && $type !== T_COMMENT && $type !== T_DOC_COMMENT) {
if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
break;
}
}
@ -208,7 +208,7 @@ class TokenStream {
if (\is_array($token)) {
$type = $token[0];
$content = $token[1];
if ($type === T_CONSTANT_ENCAPSED_STRING || $type === T_ENCAPSED_AND_WHITESPACE) {
if ($type === \T_CONSTANT_ENCAPSED_STRING || $type === \T_ENCAPSED_AND_WHITESPACE) {
$result .= $content;
} else {
// TODO Handle non-space indentation
@ -238,7 +238,7 @@ class TokenStream {
foreach ($this->tokens as $token) {
$indentMap[] = $indent;
if ($token[0] === T_WHITESPACE) {
if ($token[0] === \T_WHITESPACE) {
$content = $token[1];
$newlinePos = \strrpos($content, "\n");
if (false !== $newlinePos) {