Skip to content

ext/reflection: various cleanups and simplifications (2)#22660

Open
DanielEScherzer wants to merge 11 commits into
php:masterfrom
DanielEScherzer:reflection-cleanup-202607-r2
Open

ext/reflection: various cleanups and simplifications (2)#22660
DanielEScherzer wants to merge 11 commits into
php:masterfrom
DanielEScherzer:reflection-cleanup-202607-r2

Conversation

@DanielEScherzer

Copy link
Copy Markdown
Member

No description provided.

Given that the `Closure` class is declared as `final`, for some class entry
`ce` to represent an instance of a `Closure` it must be exactly
`zend_ce_closure`. Replace the `instanceof_function()` call with a simple
pointer comparison.
Given that the `Closure` class is declared as `final`, for some class entry
`ce` to represent an instance of a `Closure` it must be exactly
`zend_ce_closure`. Replace the `instanceof_function()` call with a simple
pointer comparison.
While different callers provided different levels of indentation to the
function, the indentation was never used. Remove the parameter and update
callers.
…mon`

Internal and userland functions store their documentation comments the same
way, as a `zend_string` pointer. These pointers are part of the common elements
at the start of both `zend_op_array` and `zend_internal_function` that are made
available via `zend_function.common`; use the common access rather than
checking for the different types of functions individually.
…ng()`

For dynamic properties all of the representation details are known immediately,
rather than building up the string in pieces (first the indented `Property [ `,
then the `<dynamic> public` followed by the name, and finally the closing
` ]\n`) use a single `smart_str_append_printf()` call and an early return.
When a variable has a single consistent initialization location, move the
declaration to be combined with the initialization.
Move variable declarations into the loop start macro where possible to have the
types of the variables clear in the loop.
If they cannot be declared at the point of initialization, at least move the
declarations closer to where the variables are used.
Consistently indent with tabs, split up some long lines, combine some nested
conditions, and overall try to improve readability.

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the review commit by commit. And found some orthogonal issues that can be fixed here or in a follow-up PR.

Comment thread ext/reflection/php_reflection.c Outdated
ce = Z_OBJCE_P(reference);

if (instanceof_function(ce, zend_ce_closure)) {
if (ce == zend_ce_closure) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to add a comment just to explain it. As instanceof_function() is always inlined to do this pointer comparison and only call the outlined slow function if not true.

Comment thread ext/reflection/php_reflection.c Outdated
} ZEND_HASH_FOREACH_END();

if (instanceof_function(ce, zend_ce_closure)) {
if (ce == zend_ce_closure) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, probably should be a single commit with all the zend_ce_closure checks rather than multiple.

Comment on lines +980 to 988
if (!prop) {
// Dynamic property, known to have no doc comment, flags, etc.
smart_str_append_printf(str, "%sProperty [ <dynamic> public $%s ]\n", indent, prop_name);
return;
}
if (prop->doc_comment) {
smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(prop->doc_comment));
}
smart_str_append_printf(str, "%sProperty [ ", indent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a smart_str_appends(str, indent); call before either of the smart_str_append_printf(), as I'm not really the biggest fan of the printf variants personally. But no big opinion here.

object_init_ex(object, reflection_attribute_ptr);
intern = Z_REFLECTION_P(object);
reference = (attribute_reference*) emalloc(sizeof(attribute_reference));
reflection_object *intern = Z_REFLECTION_P(object);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: two spaces (or tabs?) after the variable name and the = sign which are not aligned anyway.

Comment on lines +2409 to +2410
zval *object = ZEND_THIS;
reflection_object *intern = Z_REFLECTION_P(object);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the object zval even used somewhere else? Might be better to just do Z_REFLECTION_P(ZEND_THIS);

Comment thread ext/reflection/php_reflection.c Outdated
if (ini_entry->modifiable == ZEND_INI_ALL) {
smart_str_appends(str, "ALL");
} else {
char *comma = "";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be const.

}

zval *params;
int num_args;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be uint32_t

RETURN_THROWS();
}

int argc = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be uint32_t

Comment on lines +7402 to 7403
zend_class_entry *ce;
if (NULL == (ce = zend_lookup_class(attr->data->name))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be rewritten as:

zend_class_entry *ce = zend_lookup_class(attr->data->name);
if (ce == NULL) {

Comment on lines +5340 to +5341
zend_string *mname = zend_string_alloc(ZSTR_LEN(class_name) + ZSTR_LEN(cur_ref->method_name) + 2, false);
snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(cur_ref->method_name));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: This feels this would be safer if it was a simple zend_strpprintf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants