Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116186 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 12092 invoked from network); 1 Oct 2021 23:10:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Oct 2021 23:10:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8C92C180002 for ; Fri, 1 Oct 2021 16:53:55 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=BAYES_50,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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, 1 Oct 2021 16:53:55 -0700 (PDT) Received: by mail-pf1-f177.google.com with SMTP id h1so5173670pfv.12 for ; Fri, 01 Oct 2021 16:53:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20210112.gappssmtp.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=/wt/QFiz0VqjfDxgEY3Kv69GQQvIfkdX4HgXYewPq1Q=; b=XjRc28XFnyGHNyaHpAQ53pC7yxXhxffRwTc8oaLGJXN0of/KKJwx9IeP6rhbCMgkNR gXhz/QM0az8FD/QPrPcjd9jpgeqokc67LJz4lWudACrQycm0cQKqZ7NTcfnb1Uo9VFVl sX2xed9U98vTmlqR/yQ0A94dPzM3kyBLbZX1nJJCzh/kj3bOP6cZC+PgBDPWi6+73zBK Aas7LFwfTlW/1/DFaACbO6kyDQYwGGeNMd2kBOufXcyeMezQ4ITGRiBr5hz1PGHfBi8R CvaVkrZaCkiGB0NE9WHcTzwi+tIhzP46CVARw0paWckRSuv4tO12BdyqVLSraIs2FH9Q eaEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=/wt/QFiz0VqjfDxgEY3Kv69GQQvIfkdX4HgXYewPq1Q=; b=U57SGidsAS9At0cFcvk5TiYjz8SsFprDb4dtpXpl0LOsqBPROrIE7qhKI2iVBo5hCs Y083y7Mz1FCpqWs/yeC376g5cRdHgYj/1MTuWyWdSvO/AvqIZg/8jNYpemkF3B0lB2Yq 2xXESYU2JkcX5lRGjHLGkAAFlxt8Y2htkXzRYhmitBDxmd5UsWjP/rL+rm0bj3pDfxQ+ NlJbOBWYZ6RbxCgbIQMGxHAljsKE6n/LR4o25V9KhWhZhKbSlFiZjn4wHuhpGvYG0YnZ i8RXwjeWlETE6UtxmqOWFajMbgnZQB+KDNf/6BeKeilgqrfqe7SIplzXcvCuz1GjM8HX mwLg== X-Gm-Message-State: AOAM532grPLrF/ErwkvTkWiWdu9NsYjYKKpqpamP/qrli2F/oGIha7if vS/Wf41yQBoubCRxmlXx4QRyFU6CW+GA5v3XBp6QdBFYQa5jnA== X-Google-Smtp-Source: ABdhPJwZna+Qnlfzsbo8uC1SKpgRpM5JCRLUG2W4t1rUWERNPeyYZoeKf+eKvE4hRA0VCL9f26zh5MXhwmoIv9XXU7Q= X-Received: by 2002:aa7:954a:0:b0:44b:bc53:1e2b with SMTP id w10-20020aa7954a000000b0044bbc531e2bmr12815126pfq.64.1633132432838; Fri, 01 Oct 2021 16:53:52 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 2 Oct 2021 01:53:42 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Unified ReflectionType methods From: andreas@dqxtech.net (Andreas Hennings) Hello list, I would like to propose new methods for ReflectionType, that would allow treating ReflectionNamedType and ReflectionUnionType in a unified way. This would eliminate the need for if (.. instanceof) in many use cases. Some details can still be discussed, e.g. whether 'null' should be included in builtin type names, whether there should be a canonical ordering of type names, whether we should use class names as array keys, etc. abstract class ReflectionType { [..] // existing methods. /** * @return string[] */ public function getClassNames(): array; /** * @return \ReflectionClass[] */ public function getClasses(): array { $classes = []; foreach ($this->getClassNames() as $className) { $classes[] = new ReflectionClass($className); } return $classes; } /** * @return string[] */ public function getBuiltinNames(): array; } class ReflectionNamedType extends ReflectionType { [..] // existing methods. public function getClassNames(): array { return $this->isBuiltin() ? [] : $this->getName(); } public function getBuiltinNames(): array { $names = $this->isBuiltin() ? [] : $this->getName(); if ($this->allowsNull()) { $names[] = 'null'; } return $names; } } class ReflectionUnionType extends ReflectionType { [..] // existing methods. public function getClassNames(): array { $namess = []; foreach ($this->getTypes() as $type) { $namess[] = $type->getClassNames(); } return array_unique(array_merge(...$types)); } public function getBuiltinNames(): array { $namess = []; foreach ($this->getTypes() as $type) { $namess[] = $type->getBuiltinNames(); } return array_unique(array_merge(...$types)); } } What do you think? -- Andreas