What's the use-case for creating a userland `ReflectionType` instance,
besides mocking the reflection API itself?
Consider that the subclassing in userland already provides an easy way to
perform these operations (although ugly):
https://github.com/Roave/BetterReflection/blob/2.0.1/src/Reflection/Adapter/ReflectionType.php

Not trying to drag down your proposal, but as you can see this is already
being done and works very well and is very well tested for BC compliance
(should new methods land into `ReflectionType`).

Also, I'd add that a the proposed API should accept
`InternalType|ReflectionClass|null`, and not `string` (separate named
constructors would be best) which is basically the reason for most
hair-pulling around the PHP lang.

A small `InternalType` VO that throws on `InternalType::fromString() :
self` would be extremely beneficial to avoid the continuously growing list
of edge cases and additions required to accommodate for new internal types
being added to the language.

On 11 Dec 2017 08:25, "Andreas Hennings" <andr...@dqxtech.net> wrote:

Currently there is no (documented) way to directly create a
\ReflectionType object.
The class has no documented constructor.

I propose that static factory methods shall be added for this purpose.

This will be useful for code that wants to dynamically create and pass
around reflection type objects.


/** @var bool $allowsNull */

$t1 = \ReflectionType::fromBuiltinType('string', $allowsNull);
$t2 = \ReflectionType::fromClassName(\stdClass::class, $allowsNull);

assert(true === $t1->isBuiltin());
assert(false === $t2->isBuiltin());

assert('string' === $t1->getName());
assert(\stdClass::class === $t2->getName());


To be discussed:
- method names
- Whether to return ReflectionNamedType or ReflectionType or something else.

(I don't find ReflectionNamedType documented in
http://php.net/manual-lookup.php?pattern=ReflectionNamedType)

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to