-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Reflection support for type hints and return types #1190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Please do not propagate the misnomer "type hint". There's nothing of "hint" in type definition. We've made this mistake once, but we should not make it in perpetuity. We should phase out broken terminology instead. |
So how would you call it then? The thing it accomplishes isn't actually done by presence of an annotation in code (or do I understand it wrong?). |
@smalyshev Sure. However, I thought we settled for "hint" though, as that's the jargon that's been used extensively during the recent Scalar Type Hints civil war ;) |
Looks okay with TypeHint changed back to TypeAnnotation. @morrisonlevi Thoughts? |
@nikic isn't TypeAnnotation a bit too close to Annotations, which aren't supported yet, but something very different? |
@mbeccati I don't really think that there's much potential for confusion in that direction. But, maybe just call it |
@nikic sure. But maybe we should move the discussion to internals and/or resurrect the RFC? |
@mbeccati Yeah, feel free to bring it up on internals again. If there is no disagreement over the APIs I hope we can land this without dragging through the whole RFC cycle. |
@nikic to be honest I tried a couple of times already without gathering any attention. Maybe if it comes from you we have better chances to land it ;) |
If you look at the RFC history I've probably edited it more than Sara has :) Personally, I don't think we should include I honestly think our best solution is to ship without reflection support. Having a bad Reflection API is worse than not having one at all in my opinion; I mean this constructively. Our current API is not very good and this PR basically continues down the same path and just adds more methods. |
Some concrete feedback on this PR:
|
@morrisonlevi I don't think shipping without reflection is an option. I've already hit an issue with a mocking framework generating invalid code because it does not respect scalar and return typehints (due to lack of reflection support for them). Reflection is not just cosmetics. Furthermore, I do not see why this would be incompatible with union types. For them isInstance(), isArray(), etc return false and isUnion() returns true - and getUnionTypes() or whatever returns an array of ReflectionTypes representing the individual members in the union. Similar handling is possible for generics. |
|
@mbeccati Regarding the second point, you are of course right. However this begs the question why there are class ReflectionType {
function __toString() : string;
function isNullable() : bool;
function isInstance() : bool;
} |
After further discussion, the As such the suggestion was to call it |
So, to summarize the current room 11 consensus: class ReflectionType {
function __toString(): string;
function isClassOrInterface(): bool;
function allowsNull(): bool;
} Another topic that came up is what |
case IS_DOUBLE: RETURN_STRINGL("float", sizeof("float") - 1); | ||
default: | ||
php_error_docref(NULL, E_ERROR, "Unknown type hint: %d", (int)param->arg_info->type_hint); | ||
RETURN_EMPTY_STRING(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use EMPTY_SWITCH_DEFAULT_CASE()
here instead of the E_ERROR
to assert that the switch enumerates all options.
322f877
to
a403608
Compare
@nikic I've fixed internal class support and EMPTY_SWITCH_DEFAULT_CASE(). Re: room11 consensus, is that a thing? Internals or it didn't happen ;) Seriously though that seems quite a big deviation from the original RFC, but I'll be happy to make the required changes if there's consensus also outside room11. About |
To clarify the Room 11 consensus: that includes Nikita Popov, Anthony Ferrara, myself and possibly Bob Weinand (can't remember if he agreed it was good or not; sorry if I'm putting words in your mouth, Bob). Github was not allowing me to post comments at the time, anyway. The main intention of what Nikita has shared here is that it provides the baseline functionality without getting in the way of future changes or repeating past mistakes. Keep in mind this minimalism is probably required to get this included in PHP 7.0, given that we are after the feature freeze. |
@nikic @morrisonlevi I've updated the PR. For now I've added a specific test for self/parent to ensure they are returned as-is. Maybe we could add getReflectionClass() that returns the resolved class/interface. |
Just do I only skimmed it, but the patch looks good. |
@morrisonlevi so you don't think there is a case for keeping self/parent. Well, in that case: |
I don't think we currently preserve Edit: It seems we do; casting a ReflectionParameter results in something like this:
I will try talking to a few others tomorrow and see what everyone thinks. Shooting from the hip I think I'd prefer to not disclose |
@morrisonlevi TBH I wasn't even aware that self/parent were legal parameter type "hints". In any case I've added some tests to cover them as well (to both branches). |
@morrisonlevi any further feedback? |
And I concur with that proposal ;) For the record, the idea behind this is as follows: By adding something like The choice of |
ed6a683
to
ed498f4
Compare
@nikic @morrisonlevi Updated and rebased, even though it looks like github master is out-of-sync atm. In any case, please do not merge this one, I can manually merge https://github.com/mbeccati/php-src/tree/reflection-typehints-sth-v6 which has @sgolemon 's original implementation plus a squashed commit of everything I did on top of her work. |
You can squash these and force-push to this branch; github will handle it properly. I doubt that anyone who has this branched checked out will care (or should be able to figure out how to get the latest). |
I would also recommend renaming |
ed498f4
to
a710695
Compare
@morrisonlevi sure, I didn't do it right away to make your review easier. It's squashed now and the test is called ReflectionType_001.phpt, to match the existing ones. |
|
||
RETVAL_BOOL((param->fptr->type == ZEND_INTERNAL_FUNCTION ? | ||
((zend_internal_arg_info*)param->arg_info)->type_hint : | ||
param->arg_info->type_hint) != 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to differentiate between internal/userland here (and in getType), as the structures are compatible (by design). So it's okay to directly check param->arg_info->type_hint
.
587f440
to
0e7821c
Compare
@nikic fixed leak and all tests are now passing. Tbh I haven't digged into the trampoline stuff too much, but mbeccati@0e7821c#diff-39e3ad3b3135c65cc314ca09cc835530L2989 was required to properly pick the class names. However the same check is missing from most of the similar snippets in other parts of the file, so I'm wondering if I did something wrong. |
Hm, I hadn't thought of the problem with the class name for via-handler functions. The check you added will not generally work, because the function |
@dstogov What do you think about this problem? Basically the comment in http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_closures.c#210 is not true as far as Reflection is concerned. Do we need an extra flag to distinguish whether it's user or internal arginfo or do you see some other way to solve this? |
As far as I'm concerned you can merge this PR -- the via-handler issue is a tangential problem we can resolve later. |
I think there should be a method to get the name of the type, without explicit string cast, like Also |
Just a reminder: this needs to be a minimal set of functionality for 7.0 On Sun, Jun 7, 2015, 3:58 PM Matteo Beccati notifications@github.com
|
Conflicts: ext/reflection/php_reflection.c ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
0e7821c
to
ec281fe
Compare
Merged in 11ece75 |
* mbeccati/reflection-typehints-sth-v5: Reflection support for type hints and return types Merge remote-tracking branch 'pollita/reflection.typehint' As discussed on #1190
A work based off of Sara's https://wiki.php.net/rfc/reflectionparameter.typehint with a twist.
I've changed ReturnTypeAnnotation back to ReturnTypeHint, as this is still what we're calling them.
The new methods are:
References:
http://markmail.org/message/meyx6t52opv7xfbo
Btw, not trying to bypass the RFC process, but atm I don't have time to propose one.