Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113020 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21114 invoked from network); 29 Jan 2021 10:34:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Jan 2021 10:34:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C6F6A1804A7 for ; Fri, 29 Jan 2021 02:16:27 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,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 02:16:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=stitcher.io ; s=default; h=References:To:Cc:In-Reply-To:Date:Subject:Mime-Version: Content-Type:Message-Id:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=GHRw7GNssuPlUUuJc76fPSsKr7mgX/99keBSo73wUbk=; b=QCH/S6db0T/bxByzXXF0FY4t7O uqgA+x/oCzRrqjMvqRdIEX2SiRpWfeHNKyPHEbIB0S8MnM99FTBRn6C5E5DCz9omPmah8ItkCIl3o ajZeKLrNkCuOhslkZN5lnWouAQ5USjQV5ytuqGelHyVV1gpJMf1Khb0Yy2V/Ap8kwdz/JWMpJRkVR 5dFen4hgnrwvML5gUXmdymUAaDxD0Zm1F5Xr5aggAW3KENgL8th1Ra4TkuzDW+t2rJdTJJDhiG5nM 1W/XQJMaKm0lYgt8eltjrOm+PgTNdvRduVpwP0wy/HEyiSmR+R+ZPPH0ho6Ob3Ml3LhgPwnXICTYS OX4xumQw==; Authentication-Results: srv015.mail.ichtushosting.com; iprev=pass (srv020.mail.ichtushosting.com) smtp.remote-ip=78.46.213.219; spf=pass smtp.mailfrom=stitcher.io; dmarc=pass header.from=stitcher.io Received: from srv020.mail.ichtushosting.com ([78.46.213.219]) by srv015.mail.ichtushosting.com stage1 with esmtp (Exim MailCleaner) id 1l5QpC-0004Sl-Gn from ; Fri, 29 Jan 2021 11:16:22 +0100 Received: from ptr-fq9pjpip3nw3may1e1l.18120a2.ip6.access.telenet.be (ptr-fq9pjpip3nw3may1e1l.18120a2.ip6.access.telenet.be [IPv6:2a02:1812:c3c:3a00:ad18:384f:3644:e759]) (Authenticated sender: brendt@stitcher.io) by srv020.mail.ichtushosting.com (Postfix) with ESMTPSA id BD12F3EB3D; Fri, 29 Jan 2021 11:16:21 +0100 (CET) X-MailCleaner-return_path: brendt@stitcher.io X-MailCleaner-sender_address: brendt@stitcher.io X-MailCleaner-recipients: nikita.ppv@gmail.com, internals@lists.php.net X-MailCleaner-SPF: pass Message-ID: <3B81DE0B-A9E1-4AEE-AE2C-D820ECD43FE9@stitcher.io> Content-Type: multipart/alternative; boundary="Apple-Mail=_A444E2A6-8588-4308-BB3F-99B772EED102" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Date: Fri, 29 Jan 2021 11:16:20 +0100 In-Reply-To: Cc: PHP Internals To: Nikita Popov References: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> X-Mailer: Apple Mail (2.3608.120.23.2.4) X-MailCleaner-TrustedIPs: Ok Subject: Re: [PHP-DEV] [DISCUSSION] ReflectionType::accepts From: brendt@stitcher.io (Brent Roose) --Apple-Mail=_A444E2A6-8588-4308-BB3F-99B772EED102 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi Nikita I was indeed thinking about the former: acceptsValue; but I would = excpect that if the concrete value is a subtype of the current type, the = fuction would also return true. What I'm basically interested in: will = this method throw a type error when I pass this value? In other words: = "will this method accept this value?" Is that in line with your view on `acceptsValue`?=20 Kind regards Brent > On 29 Jan 2021, at 10:33, Nikita Popov wrote: >=20 > On Fri, Jan 29, 2021 at 9:15 AM Brent Roose > wrote: > Hi Internals >=20 > 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: >=20 > ``` > /** @var \ReflectionNamedType[] $types */ > $types =3D match ($type::class) { > ReflectionUnionType::class =3D> $type->getTypes(), > ReflectionNamedType::class =3D> [$type], > }; >=20 > foreach ($types as $type) { > if (! is_subclass_of($type->getName(), ShouldBeStored::class)) { > continue; > } >=20 > // =E2=80=A6 > } > ``` >=20 > 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: >=20 > ``` > if (! $type->accepts(ShouldBeStored::class)) { > return; > } >=20 > // =E2=80=A6 > ``` >=20 > 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. >=20 > What do you think? >=20 > Kind regards > Brent >=20 > There's two possible notions of what "accepts" does: >=20 > * acceptsValue(), whether a given value satisfies the type. Probably = needs a flag to determine whether strict_types semantics should be used = or not. >=20 > * isSubTypeOf(), whether a given type satisfies the type in a = subtyping relationship. >=20 > You seem to be mixing these two up into one concept, even though they = are quite different. Which is the one you actually need? (I am much more = open to providing the former than the latter.) >=20 > Nikita --Apple-Mail=_A444E2A6-8588-4308-BB3F-99B772EED102--