Skip to content

Commit 8b58f41

Browse files
committed
Return null from getTokenName() if token unknown
1 parent a852dbd commit 8b58f41

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

ext/tokenizer/tests/PhpToken_methods.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ try {
7070
echo $e->getMessage(), "\n";
7171
}
7272

73+
echo "\nName of unknown token:\n";
74+
$token = new PhpToken(100000, "foo");
75+
var_dump($token->getTokenName());
76+
7377
?>
7478
--EXPECT--
7579
[ 0] T_OPEN_TAG ignorable
@@ -110,3 +114,6 @@ Typed property PhpToken::$id must not be accessed before initialization
110114
Typed property PhpToken::$text must not be accessed before initialization
111115
Typed property PhpToken::$id must not be accessed before initialization
112116
Typed property PhpToken::$text must not be accessed before initialization
117+
118+
Name of unknown token:
119+
NULL

ext/tokenizer/tokenizer.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ PHP_METHOD(PhpToken, getTokenName)
237237
if (Z_LVAL_P(id_zval) < 256) {
238238
RETURN_INTERNED_STR(ZSTR_CHAR(Z_LVAL_P(id_zval)));
239239
} else {
240-
RETURN_STRING(get_token_type_name(Z_LVAL_P(id_zval)));
240+
const char *token_name = get_token_type_name(Z_LVAL_P(id_zval));
241+
if (!token_name) {
242+
RETURN_NULL();
243+
}
244+
245+
RETURN_STRING(token_name);
241246
}
242247
}
243248

@@ -531,6 +536,10 @@ PHP_FUNCTION(token_name)
531536
Z_PARAM_LONG(type)
532537
ZEND_PARSE_PARAMETERS_END();
533538

534-
RETVAL_STRING(get_token_type_name(type));
539+
const char *token_name = get_token_type_name(type);
540+
if (!token_name) {
541+
token_name = "UNKNOWN";
542+
}
543+
RETURN_STRING(token_name);
535544
}
536545
/* }}} */

ext/tokenizer/tokenizer.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public function is($kind): bool;
1515

1616
public function isIgnorable(): bool;
1717

18-
public function getTokenName(): string;
18+
public function getTokenName(): ?string;
1919
}

ext/tokenizer/tokenizer_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ ZEND_END_ARG_INFO()
2828
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_isIgnorable, 0, 0, _IS_BOOL, 0)
2929
ZEND_END_ARG_INFO()
3030

31-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_getTokenName, 0, 0, IS_STRING, 0)
31+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_getTokenName, 0, 0, IS_STRING, 1)
3232
ZEND_END_ARG_INFO()

ext/tokenizer/tokenizer_data.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,6 @@ char *get_token_type_name(int token_type)
306306
case T_BAD_CHARACTER: return "T_BAD_CHARACTER";
307307

308308
}
309-
return "UNKNOWN";
309+
return NULL;
310310
}
311311

ext/tokenizer/tokenizer_data_gen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ awk '
7171

7272
echo '
7373
}
74-
return "UNKNOWN";
74+
return NULL;
7575
}
7676
' >> $outfile
7777

0 commit comments

Comments
 (0)