-
Notifications
You must be signed in to change notification settings - Fork 7.8k
ReflectionType improvements #2068
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
Added ReflectionNamedType and updated ReflectionType::__toString()
@trowski I would like to see this in 7.1 please. It looks like the CI failed due to the rand() changes and I agree 100% that not making these changes is a bigger problem than breaking BC. Please ensure these get merged before beta3. |
There should also be a method to detect whether the type is nullable or not. There is allowsNull now, but it serves different purpose. The only way would be some smelly code like this: |
You can't actually tell at runtime if it's implicit or explicitly null. On Tue, Aug 9, 2016, 6:57 PM Michael Moravec notifications@github.com
|
That is strange. In such case, this patch is broken: class Test {
function foo(int $z = null) {}
}
$rp = new ReflectionParameter(['Test', 'foo'], 'z');
var_dump((string) $rp->getType()); // oops, "?int", but should be "int" |
@Majkl578 In your example, To detect function foo(?int $i) {}
function bar(?int $i = null) {}
$parameter = new ReflectionParameter('foo', 'i');
var_dump($parameter->getType()->allowsNull()); // bool(true)
var_dump($parameter->isOptional()); // bool(false)
$parameter = new ReflectionParameter('bar', 'i');
var_dump($parameter->getType()->allowsNull()); // bool(true)
var_dump($parameter->isOptional()); // bool(true)
var_dump($parameter->getDefaultValue()); // NULL |
Merged via 622d2f4. Thanks! |
@trowski Well, I still disagree, having default value and requiring value are two different things (for caller, not callee). public function returnsNullableObject() : ?\stdClass results in: public function returnsNullableObject(): \?stdClass This unfortunately makes your original claim void:
So libraries still will have to manually resolve this issue manually. And in order to not change method signature in proxied class (because there is no way to differentiate between nullable type and default null value, or both), it'd still require a hack such as Also there is a bug (not sure whether caused by this patch or not, will need to bisect later): namespace Foo;
function x(?\stdClass $y) {}
var_dump((new \ReflectionParameter('Foo\x', 'y'))->getClass()); // Fatal error: Uncaught ReflectionException: Class Foo\stdClass does not exist EDIT: Forgot to reply to NULL detection. How would you propose to solve this issue: function x(?int $a = null) {}
function y(int $a = null) {}
foreach (['x', 'y'] as $fn) {
echo $fn, ":\n";
$rp = new \ReflectionParameter($fn, 'a');
var_dump($rp->getType()->allowsNull());
var_dump($rp->isOptional());
}
|
Looks like I focused on scalar types when I was experimenting with PHPUnit, etc. There does seem to be an issue with @Majkl578 You still seem confused about |
@trowski Interesting question. Usually I'd say that leading backslashes have nothing lost in strings, but this situation is special in that the output of @morrisonlevi Your thoughts on this? |
Implements a portion of @morrisonlevi's ReflectionType Improvements RFC that is necessary due to the Nullable Types RFC.
ReflectionParameter::getType()
andReflectionFunctionAbstract::getReturnType()
now return an instance ofReflectionNamedType
. This class extendsReflectionType
, adding agetName()
method that returns the type name as a string. This method provides a means of retrieving the type name without casting theReflectionType
object to a string.ReflectionType::__toString()
was updated to prepend a?
if the type is nullable, maintaining the string representation ofReflectionType
as a syntax-valid representation of the type.The BC impact of this PR should be minimal. In fact, not applying these changes may have a bigger impact to BC. Without the changes in this PR projects such as PHPUnit, PHPSpec, and Mockery fail to produce code compatible with function signatures including nullable types. These projects depend on the string representation of
ReflectionType
to generate code compatible with the parent class or interface. After the changes in this PR, these projects work as expected with nullable types.@krakjoe @dshafik I'd like to merge this for 7.1. I think these changes to reflection are necessary and should not wait for 7.2.