Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95348 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97305 invoked from network); 21 Aug 2016 15:52:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2016 15:52:29 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wm0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:37610] helo=mail-wm0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 83/A4-50790-B3EC9B75 for ; Sun, 21 Aug 2016 11:52:28 -0400 Received: by mail-wm0-f43.google.com with SMTP id i5so108356904wmg.0 for ; Sun, 21 Aug 2016 08:52:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=ARBHV/BK8r2NfSDGaUgY4pnp7B7xapVg1iW5xsKMuDY=; b=k8izznMxxNFDAuI4mAUiWHvsO7xkf8qt12e4ijER4c7FTXpQCt3AzBVMTIxqe0b+vM +kfUV7HzwYgivSeYqjgzm6YYaJCEmHNR4tcYMn8M/jZ0TFvkbW2dCltjtFoJZRlgSipA dBgVh88/VrY736QHMiU15oFXLNLhcAHzQGek2Uma/QvAU1fsNJVGYcSRQr1R7c2bowl/ GouAhpsvD6hLih6BQcxID8Zj0tbHw8j/xIWRzKr7HjnvBxnm85diQtQzO9wItwLkZa2p KSPSCVt1Hk1r2EUwAzvJF6ljtkZG9hnCgCOH2gsVUdvljJyyJRm0mLF0z0Cxgmmk0Gpm zC+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=ARBHV/BK8r2NfSDGaUgY4pnp7B7xapVg1iW5xsKMuDY=; b=he3Nh/TpoSQOQ7zCGXA74vMkagddx2DXohWBk76DnM6EuzgkU1792sScSNm8bqzDJs y4kDQj9FqV86FUmY5LIQJG+8GMXaAfCCDjL9U/6CZUy0kxNs5INuL3PDoJbe9afY+G5J hyZ/fkWBEslBhMlxRrtaln3cM/0RCEpl4/L9uS5ZjU+MVRM4tvrxLJs0wq/3MdmNsj0I WWxJzjRNbSBCQhY03uynlfyeQyXXufIv7q9iOQEq5cGFYNeDLEI2ExzNgwiPKYGTLvd0 wICzP+rOxOe0obj+xjUvBzrle1eQoBIWu/GnHRHGL2PWxjnFXpUG1AucXgLaTEnZyv6d uW/A== X-Gm-Message-State: AEkoous92rs2d2QoNTv9TSMzr4ussGItUHlaqHQqpqc8Nze1ZUYZaeaKRR3Lxe4eiQAxmRkbL3jIpJ1Bl80s8A== X-Received: by 10.194.189.229 with SMTP id gl5mr13709898wjc.168.1471794744402; Sun, 21 Aug 2016 08:52:24 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.125.242 with HTTP; Sun, 21 Aug 2016 08:52:03 -0700 (PDT) In-Reply-To: References: <0668D584-EE51-4932-85D7-01D8BF70E409@trowski.com> <15E198DD-FF66-475D-ABBC-54ECD2B6BF62@trowski.com> Date: Sun, 21 Aug 2016 17:52:03 +0200 Message-ID: To: Levi Morrison Cc: Aaron Piotrowski , internals , Stanislav Malyshev Content-Type: multipart/alternative; boundary=047d7bb709d4ec29b9053a96ea3c Subject: Re: [PHP-DEV] ReflectionType::__toString() prepending \ to class names From: ocramius@gmail.com (Marco Pivetta) --047d7bb709d4ec29b9053a96ea3c Content-Type: text/plain; charset=UTF-8 On Sun, Aug 21, 2016 at 5:46 PM, Levi Morrison wrote: > > > On Sun, Aug 21, 2016 at 9:42 AM, Marco Pivetta wrote: > >> Including the question mark still breaks code-gen based on simple >> nullable types. PHP 7.0.x-compliant code will do following: >> >> - reflect function foo(Bar $bar = null) {} >> - cast ReflectionType to string >> - crash (unrecognized `?` symbol) >> >> That's part of what I tested on Friday on just one of the many libraries >> involved. >> >> The new API already includes `ReflectionType#allowsNull()`, so that's >> sufficient to build forward-compliant codegen. Breaking BC is a no-go >> though. >> >> Marco Pivetta >> >> http://twitter.com/Ocramius >> >> http://ocramius.github.com/ >> >> On Sun, Aug 21, 2016 at 5:39 PM, Levi Morrison wrote: >> >>> >>> >>> On Sun, Aug 21, 2016 at 9:07 AM, Levi Morrison wrote: >>> >>>> >>>> >>>> On Sun, Aug 21, 2016 at 8:37 AM, Aaron Piotrowski >>>> wrote: >>>> >>>>> Hi Marco, >>>>> >>>>> > On Aug 19, 2016, at 1:31 PM, Marco Pivetta >>>>> wrote: >>>>> > >>>>> > Hi Aaron et all, >>>>> > >>>>> > I tried to implement support for 7.1 in zend-code as a start: >>>>> > >>>>> > https://github.com/zendframework/zend-code/pull/87 >>>>> > >>>>> > A few issues arise: >>>>> > >>>>> > * `ReflectionType#__toString()` is too volatile, especially if we >>>>> want to >>>>> > support multiple versions of PHP, therefore it's a good idea to not >>>>> think >>>>> > too much about it, and instead deprecate it. Most issues I had while >>>>> > working with the feature were related with string formatting, and >>>>> that's >>>>> > simply gotta die: just using a more specific API should cut it >>>>> (getName, >>>>> > getClass, isNullable, etc. As few strings as possible, please!). >>>>> > * A page where we can see the current state of the `ReflectionType` >>>>> API >>>>> > (and its subtypes) would be golden. >>>>> > * `ReflectionType#__toString()` seems to crash in very interesting >>>>> ways >>>>> > when `?string` is reflected (see issue above - couldn't isolate >>>>> precisely) >>>>> > >>>>> >>>>> I've reverted the changes so that `ReflectionType::__toString()` is >>>>> now identical to 7.0, including *not* prepending a ? for nullable types. >>>>> The method is now just an alias of `ReflectionNamedType::getName()`. >>>>> >>>>> `ReflectionType::__toString()` should be discouraged for code >>>>> generation going forward, as it seems there's just not a way to add type >>>>> features in a BC way. My attempt to incorporate nullable types in a way >>>>> that would allow for even more complex types such as `callable(?\Type\Name, >>>>> ?bool)` just caused too many problems. >>>>> >>>>> > I am currently going through the changes, and just figured that 7.1 >>>>> implements https://wiki.php.net/rfc/reflectiontypeimprovements, even >>>>> though the RFC was declined: >>>>> > >>>>> > ./sapi/cli/php -r 'class Foo { public function bar() : ?Foo {} } >>>>> var_dump((new ReflectionMethod("Foo", "bar"))->getReturnType());' >>>>> > object(ReflectionNamedType)#2 (0) { >>>>> > } >>>>> >>>>> Only `ReflectionNamedType` was added so the object returned from >>>>> parameter and return types could have a `getName()` method. The rest of the >>>>> RFC was not implemented. This should be completely BC while allowing future >>>>> types like unions or callables. See some discussion here: >>>>> https://github.com/php/php-src/pull/2068 >>>>> >>>>> Aaron Piotrowski >>>>> >>>>> >>>>> -- >>>>> PHP Internals - PHP Runtime Development Mailing List >>>>> To unsubscribe, visit: http://www.php.net/unsub.php >>>>> >>>> >>>> This is too big of a revert. You must attempt to generate exactly what >>>> was written in user code which requires the prepended question mark. >>>> >>> >>> To clarify we do resolve certain things such as names which are aliased >>> by use and the like. But we can't resolve all names such as self and parent >>> because of traits. The use of toString is to dump out the type information >>> in a string that would be suitable for regenerating that exact type >>> declaration only. It is not meant to be parsed or analyzed. It was designed >>> this way from the beginning, and any deviation of this is a misuse. By not >>> prepending the ? we will break other libs, so it's a BC break either way. >>> Please put the question mark back as discussed in the beginning. Do not >>> include the leading slash. >>> >> >> > If you are on 7.1 and you generate code for an earlier version of PHP > that's not really a break... > If you install a library on 7.1 and it stops working for existing 7.0-compliant code (you just want to run the same exact code, but on 7.1), then this is a break. > > This is the ultimate point: if we don't prepend the question mark it will > break code. > This is the impedence here: `ReflectionType` is a *REFLECTION*, not a code generator. Don't think of `ReflectionType#__toString()` as a way to generate code: it is merely a string representation of the reflectoion. Current code generators don't use that type directly anyway: they read it, pass it through a dozen layers that do whatever they need to do, and then it may come out on the other side in whatever shape fits the generator most. The original string representation is not used in the output representation anyway. > If do prepend the question mark it will break code. It's a BC break > whether it is done or not. > It won't break BC for existing code, but codegen for nullable/void needs adaptation. This was already underway on most code generators I work with, and this happens at every PHP version, and it is *OK*. New features => new codegen/changes, this is normal/understood/accepted. > So what to do? How to pick which is correct? In my opinion we should > preserve the original intention of the code. Thus we should prepend the > question mark. > Take php 7 code, make sure it works on 7.1 as it did before. Take php 7.1 code => requires library adaptation for forward compliance. The confusion here is between BC and FC. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --047d7bb709d4ec29b9053a96ea3c--