Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113916 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75812 invoked from network); 1 Apr 2021 15:53:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Apr 2021 15:53:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0C87C1804D0 for ; Thu, 1 Apr 2021 08:51:13 -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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (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 ; Thu, 1 Apr 2021 08:51:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1617292270; bh=gGzyN2B8A5m7SIVuHGDOW9uN4IO147M6NIkd/GDYDo0=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=GACtR0MbT0PgaxRjQUrR9ccXbq5H+33WbA3Od8P8UBkVSTPnnriUyXEQf2wnTo5Tj WEfGMvj+uXOZL8BXLy+MAOKhBumz8+PdyGJHesKFgqTnHg5VYkmThF1q0qnlO5HWWn YMSYa1l4JqM2lDDPL7n4nqNHd6Qmh/4PFNwPXDgw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.120] ([24.134.51.41]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MRTRH-1lDMzr2QOj-00NV0m for ; Thu, 01 Apr 2021 17:51:10 +0200 To: internals@lists.php.net References: <7399011c-a638-5363-5303-c01ac402bb92@gmx.net> Message-ID: <50cb9463-dbc4-386a-9d2a-e0213c18a560@gmx.net> Date: Thu, 1 Apr 2021 17:51:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Provags-ID: V03:K1:WH6Pa2jKzNtdqS7AUjpWYvb4d5cXS2DMOxLpMDnUTgm6t1Wwa4x 3Ud+sc3h4A2ImKKWUU9a6vKn5RuzhnCYux/odfXZ0pxBh8+zUDoWOSkptZwrbv0CMEoIroF G7ifvF1GiVA8NYec5UzPT3Xf1psVsURdznXAzF1TKuOpH0qTRuh1s+8ixKEPKsbgcyMMxUv gDv1Qs987MXLzDxwjiPrg== X-UI-Out-Filterresults: notjunk:1;V03:K0:awa+9Y2MCps=:QfMVEEd9RRK0z7QcwJTMxc ZpxCcGGWPCq6i8k3p12XoedoBUFfnlS38XK3+AJh4FlLvqt+7jqrw3tEd6hIeXM7lIeJ5JL9K iHissOX/5hyTUqgsKBspXV4dV0XvjnbFbmFFWBZ7IGwK/feo+KR12VfijgqT83ky8EL7HDdE6 pOEgq34qVmDXAxdoOg1EML4sfHA1dmF8ovk1jF2nZZoYGnVZb6lp4k7KVe8xnZdm1Qj+wfdiU l58lPmVhVRc4fO6HkV/4it+6w7CwB1fPiX7XmCxDxiX5AmXXMBFKyfECIM6iUvUyXViTugghV A8N4y2G0FoGd/XX/AWBETbPQR7iVJtETxyyLBUJQVLWKBHW5DvlQUsaYScj+GKZZycqV/lFiK aoQtKI/r/P+7xlz6dgVl0UnLPiAsOQiLWYhHeHSfQjZL6OQOnNwnpp5B9rjh3aAfgg4iFA1i0 4ylzbAG9mAp9J6RJjfh0xjd3RfeaQ0J6JtpIwrKsQAFaEX4GRDSUb/ytDfMxPNMng3/ZAW87D HVtRORf29zNMaP29VZ0UBgVD6jaAWHTIaDyQqKOlhuftb1b4k4evKU3LfFRbWygwUjnuT4SQp tAtkCm/pk+gbT98zEuovWCIbdzIaD0NlTI0wZ/zNaELWFs2/uvCw09jGoIvJS5cxRhLM6Md5Q gk9E5vZ7jA9NAA3uEiGaUS2S1a31d/gtTO5EDw54mXlTnZNcmf3ugF8leszKzChCLoSBUxnbK pu5kosdTbLhyEjVplWB6ESwH5HBlavWH320m/vLlfOW9RDclcLlg9MZA8QStb5xXV8JdDgRAO JT2/12qZ00c2dsE1HGMUg4OzYAo054vaBO0my5/lSY++Z1AoOGXh611v+wrILdLeIswetHSsG YYR6L+1y6iDJyGeNuRSM6gKL47DMCO5s+ISR5jUepIN1z6UC41bvMZYL3BmnOkOAS6BPTW+2e wIUm+d224Ca7C29PCzj9H8ruc1b8TBCqcGkWILZpq0CfNb61iqe92GgqYhqvkpJlukIDSbVv8 b5V7ap6Bs9mm1jMck4VLeGjYGfdVuCyNa8OfTIgYmTpdLoC3y9z8/bg2EznJEqe3F+ejjRJtM U9myEbaYKIQB2NruxIRAIQjBb7f+4FjdwSDT2z7gEse8aqC00ygfhalm7I+f1AMmAV/rLbn5N O31tyKVramKkgX/1dWnHqmDOT5nUwccoq9e3S8QxMRUHKgtQj5CfdxOiH3o8oYC6kmces= Subject: Re: [PHP-DEV] [VOTE] noreturn type From: a.leathley@gmx.net (Andreas Leathley) On 01.04.21 17:07, Benjamin Eberlei wrote: > I don't know, you are arguing that you forgot a return, but you also did > not forget to add a never return type. You > could easily fix this by inlining the exception instead. > > ```php > if (!isset($user)) { > throw new NotFoundException(); > } > ``` > Even when you insist on the function, it is a one liner that throws > immediately. Adding the never return type there isn't going to catch a > great many bugs. Throwing an exception immediately is an alternative, but this only works if that is the absolute only thing the method does, and even then it leads to a lot of repetition (if you have many controllers) with very specific code shared in each controller. Making the identical behavior between controllers explicit by a shared "notFound" method seems better coding to me, and it has the advantage of being very readable and something you can easily recognize between different controllers - you look it up once and know what it does. If you throw exceptions in each controller, you might wonder if this is the exact same exception as another one in another controller. Often you might also want to change the behavior at some point - for example log the event before throwing the exception, or handling notFound entirely different. noreturn/never gives you the flexibility to refactor "throw NotFoundException();" into a method while keeping the promise that the code flow will not continue. Abstracting away something like a redirect, an authorization problem or a 404 also seems an improvement to me (compared to "throw NotFoundException();") because you write and test that logic once, and then reuse it in different places. Like with other parts of the type system in PHP noreturn/never just gives you stronger guarantees and more information about what a function/method is ought to do and how it behaves= .