Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113019 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17705 invoked from network); 29 Jan 2021 09:51:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Jan 2021 09:51:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9F8D71804CF for ; Fri, 29 Jan 2021 01:33:35 -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.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 01:33:34 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id q12so11571355lfo.12 for ; Fri, 29 Jan 2021 01:33:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hN4bpV1WqsXln/a1ufp6/OpPVYugLA06uPK7P/TFO48=; b=kt7HFmCz5bEs9ob8PpvopP3JrnOqGlBHm8C6wIz08haY52fGCl0n+pzrdTKiqya9FD n3DdoOuXnCbZuqn6IbpqQPZP/KHgqXQmGig5isfun92ALMtJeqLNJmwMqe677JET5ba0 hq7Xb+dYRiUmmKYKZ+DR7Jx2bEoE/bDE/xX2Cfho/3z14ovaC5q+VpwKcXqv7tZc8H+c Z9uMok0i9tf/33lW75FSFa6goEpEXXTI//hUzZxFrVKQZwMN9Sw2Gb4vDb8rl7/RHKSx ottlMaF0jTL2xXJr0wrKRz4pEjpyWvUe1IyvVK9RdzdwSZLZSrOmLdVxoYYzIDEkyHbr T3wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hN4bpV1WqsXln/a1ufp6/OpPVYugLA06uPK7P/TFO48=; b=UwP7pvBmvzUWl7BHwzVSzkzSorb9SEWVychEvF0NTaUM7AM5LDQSbfeXVI00gRYKYe eyWI8MNw6+93xYzX3LeWFJ0V4THjGHC16dr1Hnaic7adUcsaXJAmagnaCtHMlrp55uhz 8Xe27exvNcBqRtohVpKhTyZbOjMUbgAzYopnV+drLV2ZO99tRpXk5ybXP7k8Maz+PP0y F4VXL0HrXEihthUnC71qn2OJaT5v71a5m+Rqp9M4jzArXnm8x7TkKElpCh5JPLQex6kd ttmqhH59/yqmYFB+0AqEcUOU8g3+IAGqoZwHtAWhTNzwbryBbW9yOiTpkBAGJk1QXmUE LUug== X-Gm-Message-State: AOAM532Qb5J8YEpPiEFd8qzh1GKc/zfBFnBqAsrWunA2dA+KHTT6X1ZN HUerAuRk3j72rmTgpoF6piS06Xw3inktxYD72W4= X-Google-Smtp-Source: ABdhPJx1Jhaova/WdMhVM+jZpBuQBGuhBwjZYj4p1DEcCWOjiLVJusTMksSdlXE1bYDpF9kOPVeL0ZV00aHXhEYebrc= X-Received: by 2002:a19:9141:: with SMTP id y1mr1756061lfj.638.1611912811503; Fri, 29 Jan 2021 01:33:31 -0800 (PST) MIME-Version: 1.0 References: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> In-Reply-To: <88ABCF54-FA19-4273-9F04-FF6386758890@stitcher.io> Date: Fri, 29 Jan 2021 10:33:15 +0100 Message-ID: To: Brent Roose Cc: PHP Internals Content-Type: multipart/alternative; boundary="00000000000089e1de05ba06b286" Subject: Re: [PHP-DEV] [DISCUSSION] ReflectionType::accepts From: nikita.ppv@gmail.com (Nikita Popov) --00000000000089e1de05ba06b286 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jan 29, 2021 at 9:15 AM Brent Roose wrote: > 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 th= at > 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 > There's two possible notions of what "accepts" does: * acceptsValue(), whether a given value satisfies the type. Probably needs a flag to determine whether strict_types semantics should be used or not. * isSubTypeOf(), whether a given type satisfies the type in a subtyping relationship. 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.) Nikita --00000000000089e1de05ba06b286--