Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113017 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11153 invoked from network); 29 Jan 2021 08:36:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Jan 2021 08:36:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC3C71804CF for ; Fri, 29 Jan 2021 00:18:21 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=BAYES_20,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 29 Jan 2021 00:18:20 -0800 (PST) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 10AE95101324; Fri, 29 Jan 2021 08:18:18 +0000 (UTC) To: Brent Roose , PHP Internals References: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> Message-ID: <95314eec-3926-9d08-001f-bc999ab6248f@processus.org> Date: Fri, 29 Jan 2021 09:18:17 +0100 MIME-Version: 1.0 In-Reply-To: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] [DISCUSSION] ReflectionType::accepts From: pierre-php@processus.org ("Pierre R.") Le 29/01/2021 à 09:14, Brent Roose a écrit : > Hi Internals > > Since the addition of union types, it has become a little more cumbersome to determine whether a given parameter type accepts specific input. Personally I've been reusing this code blob in several places because of it: > > ``` > /** @var \ReflectionNamedType[] $types */ > $types = match ($type::class) { > ReflectionUnionType::class => $type->getTypes(), > ReflectionNamedType::class => [$type], > }; > > foreach ($types as $type) { > if (! is_subclass_of($type->getName(), ShouldBeStored::class)) { > continue; > } > > // … > } > ``` > > I wonder whether we would discuss adding a method on ReflectionType that accepts any given input, and tells you whether that input is valid for that type or not. I was thinking about `ReflectionType::accepts(string|object $input): bool` but we could discuss another name. With it, the above example could be refactored like so: > > ``` > if (! $type->accepts(ShouldBeStored::class)) { > return; > } > > // … > ``` > > I believe this method should accept both class names and objects to be consistent with the existing reflection API. It would also work with intersection types, if they are to be added some day. > > What do you think? > > Kind regards > Brent Hello, I think this would be a good addition. I personally had to write the exact same algorithm to solve this exact same problem yesterday night. Cheers, -- Pierre