Skip to content

Commit 24e5967

Browse files
committed
Add __toString() method
1 parent 8b58f41 commit 24e5967

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

ext/tokenizer/tokenizer.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "zend_language_scanner.h"
3030
#include "zend_language_scanner_defs.h"
3131
#include <zend_language_parser.h>
32+
#include "zend_interfaces.h"
3233

3334
#define zendtext LANG_SCNG(yy_text)
3435
#define zendleng LANG_SCNG(yy_leng)
@@ -87,17 +88,17 @@ static zval *php_token_get_id(zval *obj) {
8788
return id;
8889
}
8990

90-
static zval *php_token_get_text(zval *obj) {
91-
zval *text = OBJ_PROP_NUM(Z_OBJ_P(obj), 1);
92-
if (Z_ISUNDEF_P(text)) {
91+
static zend_string *php_token_get_text(zval *obj) {
92+
zval *text_zval = OBJ_PROP_NUM(Z_OBJ_P(obj), 1);
93+
if (Z_ISUNDEF_P(text_zval)) {
9394
zend_throw_error(NULL,
9495
"Typed property PhpToken::$text must not be accessed before initialization");
9596
return NULL;
9697
}
9798

98-
ZVAL_DEREF(text);
99-
ZEND_ASSERT(Z_TYPE_P(text) == IS_STRING);
100-
return text;
99+
ZVAL_DEREF(text_zval);
100+
ZEND_ASSERT(Z_TYPE_P(text_zval) == IS_STRING);
101+
return Z_STR_P(text_zval);
101102
}
102103

103104
static zend_bool tokenize_common(
@@ -170,14 +171,15 @@ PHP_METHOD(PhpToken, is)
170171

171172
RETURN_BOOL(Z_LVAL_P(id_zval) == Z_LVAL_P(kind));
172173
} else if (Z_TYPE_P(kind) == IS_STRING) {
173-
zval *text_zval = php_token_get_text(ZEND_THIS);
174-
if (!text_zval) {
174+
zend_string *text = php_token_get_text(ZEND_THIS);
175+
if (!text) {
175176
RETURN_THROWS();
176177
}
177178

178-
RETURN_BOOL(zend_string_equals(Z_STR_P(text_zval), Z_STR_P(kind)));
179+
RETURN_BOOL(zend_string_equals(text, Z_STR_P(kind)));
179180
} else if (Z_TYPE_P(kind) == IS_ARRAY) {
180-
zval *id_zval = NULL, *text_zval = NULL, *entry;
181+
zval *id_zval = NULL, *entry;
182+
zend_string *text = NULL;
181183
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(kind), entry) {
182184
ZVAL_DEREF(entry);
183185
if (Z_TYPE_P(entry) == IS_LONG) {
@@ -191,13 +193,13 @@ PHP_METHOD(PhpToken, is)
191193
RETURN_TRUE;
192194
}
193195
} else if (Z_TYPE_P(entry) == IS_STRING) {
194-
if (!text_zval) {
195-
text_zval = php_token_get_text(ZEND_THIS);
196-
if (!text_zval) {
196+
if (!text) {
197+
text = php_token_get_text(ZEND_THIS);
198+
if (!text) {
197199
RETURN_THROWS();
198200
}
199201
}
200-
if (zend_string_equals(Z_STR_P(text_zval), Z_STR_P(entry))) {
202+
if (zend_string_equals(text, Z_STR_P(entry))) {
201203
RETURN_TRUE;
202204
}
203205
} else {
@@ -246,12 +248,25 @@ PHP_METHOD(PhpToken, getTokenName)
246248
}
247249
}
248250

251+
PHP_METHOD(PhpToken, __toString)
252+
{
253+
ZEND_PARSE_PARAMETERS_NONE();
254+
255+
zend_string *text = php_token_get_text(ZEND_THIS);
256+
if (!text) {
257+
RETURN_THROWS();
258+
}
259+
260+
RETURN_STR_COPY(text);
261+
}
262+
249263
static const zend_function_entry php_token_methods[] = {
250264
PHP_ME(PhpToken, getAll, arginfo_class_PhpToken_getAll, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
251265
PHP_ME(PhpToken, __construct, arginfo_class_PhpToken___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
252266
PHP_ME(PhpToken, is, arginfo_class_PhpToken_is, ZEND_ACC_PUBLIC)
253267
PHP_ME(PhpToken, isIgnorable, arginfo_class_PhpToken_isIgnorable, ZEND_ACC_PUBLIC)
254268
PHP_ME(PhpToken, getTokenName, arginfo_class_PhpToken_getTokenName, ZEND_ACC_PUBLIC)
269+
PHP_ME(PhpToken, __toString, arginfo_class_PhpToken___toString, ZEND_ACC_PUBLIC)
255270
PHP_FE_END
256271
};
257272

@@ -269,6 +284,7 @@ PHP_MINIT_FUNCTION(tokenizer)
269284

270285
INIT_CLASS_ENTRY(ce, "PhpToken", php_token_methods);
271286
php_token_ce = zend_register_internal_class(&ce);
287+
zend_class_implements(php_token_ce, 1, zend_ce_stringable);
272288

273289
name = zend_string_init("id", sizeof("id") - 1, 1);
274290
zend_declare_typed_property(php_token_ce, name, &default_val, ZEND_ACC_PUBLIC, NULL,

ext/tokenizer/tokenizer.stub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function token_get_all(string $source, int $flags = 0): array {}
44

55
function token_name(int $token): string {}
66

7-
class PhpToken {
7+
class PhpToken implements Stringable {
88
/** @return static[] */
99
public static function getAll(string $code, int $flags = 0): array;
1010

@@ -16,4 +16,6 @@ public function is($kind): bool;
1616
public function isIgnorable(): bool;
1717

1818
public function getTokenName(): ?string;
19+
20+
public function __toString(): string;
1921
}

ext/tokenizer/tokenizer_arginfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ ZEND_END_ARG_INFO()
3030

3131
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken_getTokenName, 0, 0, IS_STRING, 1)
3232
ZEND_END_ARG_INFO()
33+
34+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PhpToken___toString, 0, 0, IS_STRING, 0)
35+
ZEND_END_ARG_INFO()

0 commit comments

Comments
 (0)