Skip to content

Commit bb655e6

Browse files
committed
Introduce PhpToken::getAll() instead of flag
1 parent 01b8117 commit bb655e6

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

ext/tokenizer/tests/token_get_all_TOKEN_AS_OBJECT_001.phpt renamed to ext/tokenizer/tests/PhpToken_getAll.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function foo() {
99
echo "bar";
1010
}
1111
PHP;
12-
var_dump(token_get_all($code, TOKEN_AS_OBJECT));
13-
var_dump(token_get_all($code, TOKEN_AS_OBJECT|TOKEN_PARSE));
12+
var_dump(PhpToken::getAll($code));
13+
var_dump(PhpToken::getAll($code, TOKEN_PARSE));
1414

1515
?>
1616
--EXPECT--

ext/tokenizer/tests/PhpToken_methods.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function foo() {
1313
PHP;
1414

1515
// Token names and ignorability.
16-
$tokens = token_get_all($code, TOKEN_AS_OBJECT);
16+
$tokens = PhpToken::getAll($code);
1717
foreach ($tokens as $i => $token) {
1818
printf("[%2d] %-26s %s\n", $i, $token->getTokenName(),
1919
$token->isIgnorable() ? "ignorable" : "meaningful");

ext/tokenizer/tokenizer.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@
3535
#define zendcursor LANG_SCNG(yy_cursor)
3636
#define zendlimit LANG_SCNG(yy_limit)
3737

38-
#define TOKEN_PARSE (1 << 0)
39-
#define TOKEN_AS_OBJECT (1 << 1)
38+
#define TOKEN_PARSE (1 << 0)
4039

4140
zend_class_entry *php_token_ce;
4241

4342
void tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS) {
4443
REGISTER_LONG_CONSTANT("TOKEN_PARSE", TOKEN_PARSE, CONST_CS|CONST_PERSISTENT);
45-
REGISTER_LONG_CONSTANT("TOKEN_AS_OBJECT", TOKEN_AS_OBJECT, CONST_CS|CONST_PERSISTENT);
4644
}
4745

4846
/* {{{ tokenizer_functions[]
@@ -102,6 +100,25 @@ static zval *php_token_get_text(zval *obj) {
102100
return text;
103101
}
104102

103+
static zend_bool tokenize_common(
104+
zval *return_value, zend_string *source, zend_long flags, zend_bool as_object);
105+
106+
PHP_METHOD(PhpToken, getAll)
107+
{
108+
zend_string *source;
109+
zend_long flags = 0;
110+
111+
ZEND_PARSE_PARAMETERS_START(1, 2)
112+
Z_PARAM_STR(source)
113+
Z_PARAM_OPTIONAL
114+
Z_PARAM_LONG(flags)
115+
ZEND_PARSE_PARAMETERS_END();
116+
117+
if (tokenize_common(return_value, source, flags, /* as_object */ 1) == FAILURE) {
118+
RETURN_THROWS();
119+
}
120+
}
121+
105122
PHP_METHOD(PhpToken, is)
106123
{
107124
zval *kind;
@@ -190,6 +207,7 @@ PHP_METHOD(PhpToken, getTokenName)
190207
}
191208

192209
static const zend_function_entry php_token_methods[] = {
210+
PHP_ME(PhpToken, getAll, arginfo_class_PhpToken_getAll, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
193211
PHP_ME(PhpToken, is, arginfo_class_PhpToken_is, ZEND_ACC_PUBLIC)
194212
PHP_ME(PhpToken, isIgnorable, arginfo_class_PhpToken_isIgnorable, ZEND_ACC_PUBLIC)
195213
PHP_ME(PhpToken, getTokenName, arginfo_class_PhpToken_getTokenName, ZEND_ACC_PUBLIC)
@@ -421,6 +439,19 @@ static zend_bool tokenize_parse(zval *return_value, zend_string *source, zend_bo
421439
return success;
422440
}
423441

442+
static zend_bool tokenize_common(
443+
zval *return_value, zend_string *source, zend_long flags, zend_bool as_object)
444+
{
445+
if (flags & TOKEN_PARSE) {
446+
return tokenize_parse(return_value, source, as_object);
447+
} else {
448+
int success = tokenize(return_value, source, as_object);
449+
/* Normal token_get_all() should not throw. */
450+
zend_clear_exception();
451+
return success;
452+
}
453+
}
454+
424455
/* }}} */
425456

426457
/* {{{ proto array token_get_all(string source [, int flags])
@@ -429,23 +460,14 @@ PHP_FUNCTION(token_get_all)
429460
{
430461
zend_string *source;
431462
zend_long flags = 0;
432-
zend_bool success;
433463

434464
ZEND_PARSE_PARAMETERS_START(1, 2)
435465
Z_PARAM_STR(source)
436466
Z_PARAM_OPTIONAL
437467
Z_PARAM_LONG(flags)
438468
ZEND_PARSE_PARAMETERS_END();
439469

440-
if (flags & TOKEN_PARSE) {
441-
success = tokenize_parse(return_value, source, (flags & TOKEN_AS_OBJECT) != 0);
442-
} else {
443-
success = tokenize(return_value, source, (flags & TOKEN_AS_OBJECT) != 0);
444-
/* Normal token_get_all() should not throw. */
445-
zend_clear_exception();
446-
}
447-
448-
if (!success) {
470+
if (tokenize_common(return_value, source, flags, /* as_object */ 0) == FAILURE) {
449471
RETURN_THROWS();
450472
}
451473
}

ext/tokenizer/tokenizer.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ function token_get_all(string $source, int $flags = 0): array {}
55
function token_name(int $token): string {}
66

77
class PhpToken {
8+
/** @return PhpToken[] */
9+
public static function getAll(string $code, int $flags = 0): array;
10+
811
/** @param int|string|array $kind */
912
public function is($kind): bool;
1013

ext/tokenizer/tokenizer_arginfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_token_name, 0, 1, IS_STRING, 0)
99
ZEND_ARG_TYPE_INFO(0, token, IS_LONG, 0)
1010
ZEND_END_ARG_INFO()
1111

12+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_getAll, 0, 1, IS_ARRAY, 0)
13+
ZEND_ARG_TYPE_INFO(0, code, IS_STRING, 0)
14+
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
15+
ZEND_END_ARG_INFO()
16+
1217
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_is, 0, 1, _IS_BOOL, 0)
1318
ZEND_ARG_INFO(0, kind)
1419
ZEND_END_ARG_INFO()

0 commit comments

Comments
 (0)