Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113016 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 9684 invoked from network); 29 Jan 2021 08:32:29 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Jan 2021 08:32:29 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EE3931804A7 for ; Fri, 29 Jan 2021 00:14:34 -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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from srv015.mail.ichtushosting.com (srv015.mail.ichtushosting.com [159.69.182.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 29 Jan 2021 00:14:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=stitcher.io ; s=default; h=To:Date:Message-Id:Subject:Mime-Version: Content-Transfer-Encoding:Content-Type:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=hJ8bBHSN2BIWumoekP4rVp7OyJZClH4u9RNby11bWhY=; b=OQCh9HUGESoCuCNq8oH8XSLPKw 21kLIBlvW5lJTHwyCZtNOKMBh8NtodOPOR2JQ/kG2a7dKYESD6EbtVbvFS1Q5EfUsAqcTVNmfMQdW BqdSAQk45F7nOiGkP/toXND9TlZdmQiPEnx8aDFs0ZquGpsDk054O+rHKNFDwf6w2Jle19YvdLBQZ RBWPyj2E40egMeYnKwsgbiHH1Wo4Jq8AYQ6+BE+GBLans3MkoszGKSgaeV0rKETs5vGEnVpk82SWY Qz69iCbI/3Adcauyi6uhIaoc0mfRNwREvaueWRr1YgrKqfYE6s40B1P3T1AZiQYakoAeScjRq4LLP kaJFottA==; Authentication-Results: srv015.mail.ichtushosting.com; iprev=pass (srv021.web.ichtushosting.com) smtp.remote-ip=78.47.76.72; spf=softfail smtp.mailfrom=stitcher.io; dmarc=none header.from=stitcher.io Received: from srv021.web.ichtushosting.com ([78.47.76.72]) by srv015.mail.ichtushosting.com stage1 with esmtp (Exim MailCleaner) id 1l5OvE-0001DS-MR for from ; Fri, 29 Jan 2021 09:14:30 +0100 Received: from ptr-fq9pjpheo126foeya85.18120a2.ip6.access.telenet.be (ptr-fq9pjpheo126foeya85.18120a2.ip6.access.telenet.be [IPv6:2a02:1812:c3c:3a00:5846:e370:750c:645]) (Authenticated sender: brendt@stitcher.io) by srv021.web.ichtushosting.com (Postfix) with ESMTPSA id 9EB7723C92 for ; Fri, 29 Jan 2021 09:14:27 +0100 (CET) X-MailCleaner-return_path: brendt@stitcher.io X-MailCleaner-sender_address: brendt@stitcher.io X-MailCleaner-recipients: internals@lists.php.net Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Message-ID: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> Date: Fri, 29 Jan 2021 09:14:26 +0100 To: PHP Internals X-Mailer: Apple Mail (2.3608.120.23.2.4) X-MailCleaner-TrustedIPs: Ok Subject: [DISCUSSION] ReflectionType::accepts From: brendt@stitcher.io (Brent Roose) 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 =3D match ($type::class) { ReflectionUnionType::class =3D> $type->getTypes(), ReflectionNamedType::class =3D> [$type], }; foreach ($types as $type) { if (! is_subclass_of($type->getName(), ShouldBeStored::class)) { continue; } // =E2=80=A6 } ``` 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; } // =E2=80=A6 ``` 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