Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118364 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72973 invoked from network); 7 Aug 2022 09:55:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Aug 2022 09:55:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B60D3180380 for ; Sun, 7 Aug 2022 04:56:44 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 7 Aug 2022 04:56:44 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6C2C45C0084 for ; Sun, 7 Aug 2022 07:56:44 -0400 (EDT) Received: from imap44 ([10.202.2.94]) by compute2.internal (MEProxy); Sun, 07 Aug 2022 07:56:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ollie.codes; h= cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm1; t= 1659873404; x=1659959804; bh=POaq7T5xCMKZ6OE+RRKFAjxmeumzT4siVs5 poJvtxug=; b=jhEutzPcjoIP0004JcvupMj9FUa8FH4bRffrxIKhiU0+S7DRGqC I//b8W6coAK1kj/7QwdwhuAGh290YWPqX95a0GutptnBSkepIeQPYIV1J3hm6uoH mHyfH26chsecYb1OtGuRofJ+pu0r46KzWdFynlNJNWjMyK/7hGU6Fr2X9Q8pOVId 5voLZcU4pcutbq7cIUqxSB1LhtfmbT3r19a5VFaZPGq0PXZOWZUIAInqPnVXyIVL 8nxE7I3szllppi+DiUuzAaM//zg5ftbNKOak3QuCesp7bvkvUn0Kg5UGlEBbOg0/ xjhj+iBDuCzvQeKDfVAtQDSVPgIZDR0MtYg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1659873404; x= 1659959804; bh=POaq7T5xCMKZ6OE+RRKFAjxmeumzT4siVs5poJvtxug=; b=3 8BrgYiyvwmvmkLHvdFqyHcp8uF+U8lpzs/eCbGQ7gHT2Xo4xojMla8XtsuUUpqI+ 3StUsFOAGsDj4zRtJeTa0RGQZrfZ8O1MF1l9szjVYeGoYgN1JzEgDV5AvpCnJru8 eQYcNn8jis47V8ny32qi+/HPJkOZwXvOw7/zKxrjqAdwEQpICXIeHxNOSI4wNcAx k5nr4tKbPZmIgPDFwFrdLmwu4bYBnILBt3jhUqACvwH58YRc11yXkZdtaWCrCGZo ZKagoVnW6DD/DlgmLBl5mUGki82kI9tuhyQsSFWEJFqU08YrhXpHvNvJMzAfFESC JL8KWe85QYqaLjbZX60tw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdefiedggeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedfqfhllhhivgcutfgvrggufdcuoehphhhpseholhhlihgvrdgt ohguvghsqeenucggtffrrghtthgvrhhnpeeghfetkeegheeijedtleeliedvffdutdetue etueffhfefjeevveffuefhudfgleenucffohhmrghinhepghhithhhuhgsrdgtohhmnecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhhphesoh hllhhivgdrtghouggvsh X-ME-Proxy: Feedback-ID: i835c46d5:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 377DC36A0071; Sun, 7 Aug 2022 07:56:44 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-758-ge0d20a54e1-fm-20220729.001-ge0d20a54 Mime-Version: 1.0 x-forwarded-message-id: Message-ID: <14237a5f-0522-48b4-8d3f-8dc346dd0870@www.fastmail.com> Date: Sun, 07 Aug 2022 12:56:23 +0100 To: =?UTF-8?Q?Bj=C3=B6rn_Larsson?= Content-Type: multipart/alternative; boundary=72d895cd081d4ec4b6768618a4d3eb13 Subject: RFC Idea: Introducing a ReflectionWithAttributes interface - Looking for feedback From: php@ollie.codes ("Ollie Read") --72d895cd081d4ec4b6768618a4d3eb13 Content-Type: text/plain Hello all, A while back, I posted on this mailing list with a link to a gist that had a brain dump proposal for improvement to reflection essentially, but I'd like to start again by including more detail here and by breaking it up. The current implementation of attributes with PHP is great, but it feels like it's lacking a bit. What I'd like to do is introduce 3 new methods that fall more in line with the way that other child elements are handled within reflection and then introduce an interface called ReflectionWithAttributes, which can be implemented by all Reflector implementations that have attributes. I would like to put all of this in an RFC and would be more than happy to have a go at doing the work, too, should it be something people are interested in, but I wanted to put it on here first to get some feedback, so here's the breakdown/my thinking. ## The Interface Attributes are unlike most other parts of reflection in that their usage and effect on a codebase or application are entirely arbitrary. Because of this, a lot of applications and packages will likely have handlers and wrappings around the reflection of attributes, and having a "ReflectionWithAttributes" interface would allow developers to handle this without having to create rather annoyingly long union types for all the reflection classes that have attributes. As far as I can tell, the introduction of this interface would not be a disruption in the slightest, so there's no real downside to doing so. ## The Methods There are several methods that developers could benefit from with the above interface. ### getAttributeTarget Of all the methods, this is the one I'm most uncertain about, but the idea is simple. ReflectionWithAttribute::getAttributeTarget returns the corresponding constant in Attribute that is relevant to the current implementation. So ReflectionClass::getAttributeTarget would return Attribute::TARGET_CLASS. The idea behind this is that a developer can accept the type ReflectionWithAttribute, and won't have to do any instanceof checks to figure out what sort of attributes they're dealing with in a situation where that's important before retrieving the actual attributes. I'm relatively certain this would be trivial to implement, as the implementation will know what it is. ### getAttribute There is currently a way to retrieve all of the attributes, but not just one. There are ways to do this in userland, but I'd wager that a good percentage of the time, there's only one attribute there. The idea behind this method is to bring this more inline with other parts of reflection, where the other methods have singular versions to, ie, getMethods, getMethod, getProperties, getProperty, etc, etc. Again, I suspect the implementation of this would be trivial, as it could essentially wrap getAttributes but just return the first item, as it would have an identical signature, except for the singular return type or null. There's also the option of throwing an exception if there's more than one match to avoid unforeseen side effects. ### hasAttribute This method is again an attempt to bring attributes more inline with the rest of reflection, as we have hasMethod, hasProperty, etc. Much like getAttribute, this method would have an identical signature to getAttributes, but would return a bool instead. Again, this could just wrap getAttributes returning that its size is greater than 0. ### getNumberOfAttributes For all intents and purposes, this method is identical to hasAttribute except that it returns the actual size from getAttributes. In fact, hasAttribute could probably wrap this. The idea behind this method is to mirror the ReflectionFunctionAbstract::getNumberOfParameters method. Like the others, its signature would be the same, except that it returns an int. ## Conclusion Would love to hear any thoughts on this, I know that a few developers that I have spoken to outside of this are fond of the idea, and as far as I can tell, the implementation wouldn't be too difficult, and shouldn't cause any disruptions at all. And as I said, I'm more than happy to take on the task of implementing this, rather than expect someone else to do it. If you're interested, the brain dump gist is here: https://gist.github.com/ollieread/34c878bf120ee70f9d2a869cb7a242d1#attributes --- Best Regards, *Ollie Read* --72d895cd081d4ec4b6768618a4d3eb13--