Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122973 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 474061A009C for ; Fri, 5 Apr 2024 16:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712333001; bh=Hj90OMSwPOsgksLaPfegngGrf+RWeICs+YpKqFFkiHI=; h=In-Reply-To:References:Date:From:To:Subject:From; b=NpF8nmW1xSM7+KgHBu+oPBjQZbhd5bl66G2Li0+5Z7LPhSoux9KKBoU6+c1WpCT/j 9OGBoHlVSTQCvNgfvrOZhzDPjJCCj7cnFQX1QD8f0lFq9uTRY9sFW67zVQMm6Jgx+V LQLMpoUvmfmOKDEBEiRe9V9Bf+jqkJwR9aPdyZkMkHtXsLCeGtFmERCas2qsF0Xad3 hYTXMesTKfobFfa7wJjbsj9IQe/kT17URBeptVWOi3Td6V9X9hLbAAYMOi7ja/WWjD OBtl4WlBMS1bZmln5k3UKZ9dkC7SsoHvTaESKYlF8RQZcMheaosxMB8LiAXEcTm7tH bS0OUSJJU7Sow== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C5FFD180086 for ; Fri, 5 Apr 2024 16:03:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 5 Apr 2024 16:03:20 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id E56245C005A for ; Fri, 5 Apr 2024 12:02:49 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Fri, 05 Apr 2024 12:02:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1712332969; x= 1712419369; bh=IfRvPn+1mC3X92OMh6gVK0aW4oRGXIJj98BMuoWxLSY=; b=K syFgMQ4noRu7TGQvE8p3/rLB7opbdtMQxWopF0wukrFMhxQ+3R7Q8e0ioaVBs0W0 dVQPtaQROhab+MQho3ImBYDYANZlM9tLDBwXL8+FoteZncjyrdEsi2gMZ3SUBr6l OXC07DmW3Pc4UzZ7jB67xGRjejtQGPrcWcoYQArYFJRPC4CIl61c9tNjONXwUYIb 1TzgTCYhoxxQjXv3oaSpAry/deq9RnMeuNf7HHNOgqJR6uQfAC6AC/+h6d9FWrvi P2XOpr/O/FBl3DI/yiedhxv+oOcfI0WP18KeRQPF4aKe6cN7c3GEw2RU2ZNN8GCL 6lsn/nAGYjYs19y56+fdQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1712332969; x=1712419369; bh=IfRvPn+1mC3X92OMh6gVK0aW4oRG XIJj98BMuoWxLSY=; b=q3/cniKKQf97VNcRcl1tRorBziJzy0ZLMk9JSR+9u7fy gu6TO0qAcxifnH3I9YVsg9oPaLy1RIRgpCO3mp6Dax+noxBUOxIK/rOp8E84ziZI FM+40ILjdcuOiw1GYFMJjtdMb4hFnvs+XELJ6XmQwvrTVF7COR3Pi956m3tvK6iG zwtU/9NVzRf2GKu0ZA8UYWxxhmcstjKCb4pCpc+SpTKnFnd4jijOv5XuYAMYhAu2 rsHtPVo3JTss9GZzKKxGxQpJCy2YXTLfQg4I6PG2shvU/h3dbxDa3mXbjuxahjaf xV1qH8Q/yY3yOlqCaAN7OahLlt8Wtur/rZR1sFNQMA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudegtddgleejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 981431700093; Fri, 5 Apr 2024 12:02:48 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-333-gbfea15422e-fm-20240327.001-gbfea1542 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <1c510d08-eaa8-4821-bf1b-6072fe5cb2ff@app.fastmail.com> In-Reply-To: References: Date: Fri, 05 Apr 2024 16:02:27 +0000 To: "php internals" Subject: Re: [PHP-DEV] Proposal: retrieve line, filename and if user defined for ReflectionAttribute Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Fri, Apr 5, 2024, at 2:20 PM, Joel Wurtz wrote: >> Would it make sense to not only add this for ReflectionAttribute, but also Function and/or others? > > There may be case where this makes sense but not necessarily in the use > case that i explain, and don't want to add more to this proposal, it's > also missing in ReflectionParameter, ReflectionProperty and certainly > many others if we go on this path then there is a lot of changes to > apply and will induce more friction for this proposal, i prefer to add > small parts so we don't deviate from the original use case. > >> Do you have an example of how you expect this to be used? I'm having a hard time understanding how I'd leverage this. (I maintain an attribute enhancement library, Crell/AttributeUtils, so I've hit a lot of edge cases in attribute design.) > > Sure, let's say we have the following class > > #[CustomAttribute(foo: 'foo') > #[CustomAttribute(foo: 'foo') > class Entity {} > > And that later i have something that read those attributes, but i want > to throw an Exception when multiple attribute defined the same value > for the foo property (as it should be unique in my use case), i would > not be able to do that in the constructor since i don't have the > context of others attributes in this case, so i want to throw a custom > exception explaining it's caused by the attribute "CustomAttribute" in > the file "Entity.php" at line 4, as there is the same value in the file > "Entity.php" at line 3 > > If I throw an exception without this message the error will pinpoint to > where I threw the exception and it may be confusing for the end user, > by having this he will know where to look and what to modify to make > its code correct. OK, so the idea is you could do something like: $rClass = new ReflectionClass(Entity::class); $as = $rClass->getAttributes(); $attribs = array_map(fn(ReflectionAttribute $r) => $r->getInstance(), $as); $values = array_map(fn(CustomAttribute $r) => $r->foo, $attribs); if ($values is not unique) { $msg = sprintf('You have too many %s on in file %s on line %d', Entity::class, $as[0]->getFile(), $as[0]->getLine()); throw new Exception($msg); } (Or something like that.) That works, I suppose, but couldn't that also be done with ReflectionClass::getStartLine() and ReflectionClass::getFileName()? --Larry Garfield