Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127082 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 299CA1A00BC for ; Wed, 9 Apr 2025 01:41:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1744162725; bh=DqsnhRVwPtMCFBIyDfJ1uKrJK1r8wZN3aQrFYQtur1w=; h=References:In-Reply-To:From:Date:Subject:To:From; b=FJXAuqmN6tBA2opzoLnAEb/f07UjRj+2FJqrRpJwf0ZwgNkEwgcJ0AnCUx8yUHriD 2GVN0wgL4ElPO81L7J7Nr2G71BucUaTlGDDeXUTqZm1bYB7Sin91xY+eDzEZKUYR9c NOGONmW1LoWTvYy+hAL6nL/0ykmODZQNZ2ah21UcwWQuVoGGwlZy23Q+jVrmqFkRsS 40CXtP0SEu04yH4Z8gLrUf88wocKK8BJhTiYff8uXW2UZP3I4/GutLRYf1enCMm0U3 o0zgzaWVnMYG4ZuWGW0yyL9xM5jVKDW5TpiEaKJmg7QIntsNz/ahVb/TmVOdyx84vO FAAaCzC3hbw1Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C062618006F for ; Wed, 9 Apr 2025 01:38:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f178.google.com (mail-vk1-f178.google.com [209.85.221.178]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 9 Apr 2025 01:38:44 +0000 (UTC) Received: by mail-vk1-f178.google.com with SMTP id 71dfb90a1353d-527a2b89a11so516160e0c.2 for ; Tue, 08 Apr 2025 18:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744162868; x=1744767668; darn=lists.php.net; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=DqsnhRVwPtMCFBIyDfJ1uKrJK1r8wZN3aQrFYQtur1w=; b=aji6h5qUHprBPd/1JyLc+8zLKqbPti5xaLHcUtLw/6WxN4TlWLHsYpeW8uCweyC1mO AMzEe+RlnETqABE46nRtzfqozIN5CZEv2GxJQZ+ZQirMd1bSyF/NMwUrJqKenUchnLbz TX63YLWrcMdxy6dl5nAftUMkRExOtdcp10g/ykAN9mAqGI1+FDyvy7JV2iRG4wDWKiBk kh7NmiA0xWiZeVHVziCkPkjO1g3BlW7+thycgggKw9GRZnTWQtg2eTEnG+qZTKLppQlI /+n8zLFZg8xBz0IOO32+RBu9j/yceVXJmD1b3iJMbyzM0+IgTA3kDQwOmjOFNZklSWuy E2DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744162868; x=1744767668; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DqsnhRVwPtMCFBIyDfJ1uKrJK1r8wZN3aQrFYQtur1w=; b=uO91Zqh1p9hZvZjM/6iRKt+ksn82CCc8gYEombasr/uUKYSkXiyLfITTc0ZPx0S7sk +/P/63wKEdC0TcoejsP6YzWIU1xEALY2GgmSQoaflPW6QNE1ebIlipenwe0D1ptKF9o1 Y4I0uqIln9tRH8Cr5s7SyMudhmMILrr9RiImXB+4yZGpWUTRfZoEWLP8cq4jnu9g8U9U sJtfWIk5jDrmfHsYn19UdjN8p2r51oQwLg22A/7YHCwP0jeACAOotIzRytaQo60yeBzX x/oZ6vUstFJprasuKp3ugaCavg8dLe29kPxG8gkwd4+Zruj0UvSeJQ4nCGBVKG0CT/xR mQBA== X-Gm-Message-State: AOJu0YxKGe94ga6T/bk63x20w/y9sUv8Lo7PWkAwM9barnl9EsIZsfXx EQmUGAOIv9SLkmB81+8KHWv+KRV/T1GLkFYCpRD9XaBeFH3geThlSeMT3Ju6cwn/jYL+/iw7Gqh VEPB7mvw3ersc0qvPM7a3yYRhzW88hEHCOw8= X-Gm-Gg: ASbGnctYEIPCmQRWs9HcCUTfV6RM4SsLfDIH6EGZS4CIcqSPg+TxYj8PaPLZfXPUE5o enmcZSnshv+WC4FXggjtqIl2jqyz5REk/ENb8RjU7CREETqehwj+YPgE+RMlo0AkwSzuSgyPxUC Xbp0vL914Qlt79opKz35fT3aocwtsymUXvmcxGkzpGJVNhUm3+O5yZg0gUpg== X-Google-Smtp-Source: AGHT+IFMPUuTHTLhgOrwCQe8V61hY74VbcFTEnRzfR5penhruRiohzzbiO1kyJxDjTZWHdU8aVXDm1OXfE52ByGZVw0= X-Received: by 2002:a05:6102:3f0c:b0:4bb:eb4a:f9ec with SMTP id ada2fe7eead31-4c9c4456000mr1125326137.16.1744162867880; Tue, 08 Apr 2025 18:41:07 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 8 Apr 2025 18:40:31 -0700 X-Gm-Features: ATxdqUGNkABOAvV67PKGTcb29HYPrUiekOs298L-5-8sHJXiievFGXil0vpkzwA Message-ID: Subject: [PHP-DEV] Re: [RFC] [Discussion] Never parameters To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000002ba95706324e904e" From: daniel.e.scherzer@gmail.com (Daniel Scherzer) --0000000000002ba95706324e904e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 10, 2025 at 12:05=E2=80=AFPM Daniel Scherzer < daniel.e.scherzer@gmail.com> wrote: > Hi internals, > > I'd like to start discussion on a new RFC about allowing `never` for > parameter types when declaring a method. > > * RFC: https://wiki.php.net/rfc/never-parameters-v2 > * Implementation: https://github.com/php/php-src/pull/18016 > > -Daniel > Since a lot of the discussion seems to be around static analysis and whether there is a real use case for this, I wanted to share another use case I just came across: in the `thephpleague/commonmark` package, different renderers are added to render different types (subclasses) of `League\CommonMark\Node\Node`. You can see the interface for renderers at [1]. The overall interface supports being called with any `Node` type, but each underlying renderer expects to be called with a *narrower* type than that. To avoid LSP violations, the renderers * have a `Node` typehint * have a documentation comment with the actual subclass of `Node` that they support * manually throw an exception on invalid values See, e.g., the default renderer for paragraphs[2]. This seems like exactly the place where you would find it useful to have `never` parameters. The current implementation * uses comments and static analysis tools to document the restriction * manually throws an exception when violated Whereas if the base class had a `never` parameter, it could * use language typehints to document the restriction * have the exception enforced automatically I don't think we should be worried about the fact that, under static analysis, we don't know what type of value is accepted for a `never` parameter, because under actual operation, you can always just manually throw an exception for a type you don't want, like commonmark does. -Daniel [1] https://github.com/thephpleague/commonmark/blob/2.6/src/Renderer/NodeRender= erInterface.php [2] https://github.com/thephpleague/commonmark/blob/2.6/src/Renderer/Block/Para= graphRenderer.php --0000000000002ba95706324e904e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Mar 10, 2025 at 12:05=E2=80=AFPM = Daniel Scherzer <daniel.e= .scherzer@gmail.com> wrote:
Hi internals,

I'd like to start discussion on = a new RFC about allowing `never` for parameter types when declaring a metho= d.


-Daniel


Since a lot of the discussion seems to be a= round static analysis and whether there is a real use case for this, I want= ed to share another use case I just came across: in the `thephpleague/commo= nmark` package, different renderers are added to render different types (su= bclasses) of `League\CommonMark\Node\Node`. You can see the interface for r= enderers at [1]. The overall interface supports=C2=A0being called with any = `Node` type, but each underlying renderer expects to be called with a na= rrower=C2=A0type than that. To avoid LSP violations, the renderers

* have a `Node` typehint
* have a documentat= ion comment with the actual subclass of `Node` that they support
= * manually throw an exception on invalid values

Se= e, e.g., the default renderer for paragraphs[2]. This seems like exactly th= e place where you would find it useful to have `never` parameters. The curr= ent implementation
* uses comments and static analysis tools to d= ocument the restriction
* manually throws an exception when viola= ted

Whereas if the base class had a `never` parame= ter, it could
* use language typehints to document the restrictio= n
* have the exception enforced automatically

I don't think we should be worried about the fact that, under sta= tic analysis, we don't know what type of value is accepted for a `never= ` parameter, because under actual operation, you can always just manually t= hrow an exception for a type you don't want, like commonmark does.

-Daniel

--0000000000002ba95706324e904e--