Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101318 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40844 invoked from network); 11 Dec 2017 09:21:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2017 09:21:34 -0000 Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.41 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.41 mail-lf0-f41.google.com Received: from [209.85.215.41] ([209.85.215.41:43460] helo=mail-lf0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 56/0B-53433-E1E4E2A5 for ; Mon, 11 Dec 2017 04:21:34 -0500 Received: by mail-lf0-f41.google.com with SMTP id 94so18323722lfy.10 for ; Mon, 11 Dec 2017 01:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=+LAV+WS0ZCUn27dXKhGQ99HBKR13it2VYadWCshjBds=; b=XOE4T4vAVxRy5NeQyXuZBlWK0Jh5YgV73kyosgHjrocDK9S3bTdyGD9yHVu60YAKmr 5ru2fo4CL8rJrWgK9p3cmqviedIizlsX5AyHivy/oSh8h0H3DpFkqTAjXoPl2fMm+KLI ynPYpiIkyCUkXlyxvLvWwTBr/BHFQFO9JU8hkXPecHMx1b9g3tZldEu8wfTLhWUpD0Ey +mOhkmvwRPNkdcdwBYj61vfGyOza9IpZ+72hkiT8k0ChKnqgnAuopFw34/LAjvxi8ZfD thqmGQl1U02mkubnSuh1Odgwt9ra1csvtts2r/hyfpjamOsQO2+7+rz30gns7UOw9Ba7 aZKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=+LAV+WS0ZCUn27dXKhGQ99HBKR13it2VYadWCshjBds=; b=l6ad+dTH6cKfMCXbDS4GKiOw3S9YVf/z7np4SDqxeaEEspqArK25io1O0ofcDu1iqY QCyrbA1wqQYOsforG1VRDi1ZCB80xtj0v6AGjKWcb+0fIsSnaKfnawMGhkfGA/pW5tI4 i8L7GgqNYcnbde4JD0SYFkHbuJV3+ODqj/VzWrw6+R9IQfF4sKa6DYZ+5XtkF6cIHUl9 iG2HkPe2HyrMhe3+W6M44zfFS9e2AI6lFRI2olI9P8wuXKY0iZH/wxPRvhM9PGjguYf3 f6ofaEGghwjp0Gxqf8AtoHVf1ot3jfbZKwIfcCcK+hSgE5fmhes9LQAXBcHrU0BFFAYF x0jw== X-Gm-Message-State: AJaThX7odSwWHJNjijyEaaMbCDnGyLKn4flC7At2/s146VWTfWuSdXNc BkWkaXpeBQct02hXxvnfNvIE3wXS X-Google-Smtp-Source: AGs4zMYJ8RqkPo6KS1JdOnWy1kxV7jQ8ERPkk0WXM7POMmokcJMI23aro6SDte8xy7T7MT5eQF4OEQ== X-Received: by 10.25.168.205 with SMTP id r196mr15986304lfe.52.1512984090954; Mon, 11 Dec 2017 01:21:30 -0800 (PST) Received: from mail-lf0-f44.google.com (mail-lf0-f44.google.com. [209.85.215.44]) by smtp.googlemail.com with ESMTPSA id y3sm2682538ljd.62.2017.12.11.01.21.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Dec 2017 01:21:30 -0800 (PST) Received: by mail-lf0-f44.google.com with SMTP id x204so18300649lfa.11 for ; Mon, 11 Dec 2017 01:21:29 -0800 (PST) X-Received: by 10.46.23.156 with SMTP id 28mr18061537ljx.3.1512984089661; Mon, 11 Dec 2017 01:21:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.170.16 with HTTP; Mon, 11 Dec 2017 01:21:08 -0800 (PST) In-Reply-To: References: Date: Mon, 11 Dec 2017 10:21:08 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Marco Pivetta Cc: PHP Internals List Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Reflection API: Add static factories on ReflectionType From: andreas@dqxtech.net (Andreas Hennings) On 11 December 2017 at 08:58, Marco Pivetta wrote: > 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 You are right. And it does not even need BetterReflection library, it only needs one single class which can also be defined locally. It only needs $this->name, $this->allowsNull and $this->isBuiltin as private properties. (nothing against the library, just in case someone only needs this one feature) Still, I think having a documented native constructor and/or static factories would make this API more complete. Without this, it seems like someone walked away from a construction site. Also, inheriting from core reflection classes, I imagine always carries some baggage. (as opposed to implementing an interface) > > 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" 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 > >