Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117152 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21884 invoked from network); 27 Feb 2022 13:50:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Feb 2022 13:50:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 191461804D9 for ; Sun, 27 Feb 2022 07:11:41 -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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS 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-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 ; Sun, 27 Feb 2022 07:11:40 -0800 (PST) Received: by mail-pg1-f178.google.com with SMTP id c1so9289740pgk.11 for ; Sun, 27 Feb 2022 07:11:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=S+wRpDpqOkPrrDXB0zwpd1xRo94g25vtsqje1oWV7Ug=; b=XvMt2+GQOuAJgjp8TqVOVOHgCVj1Ts/JusfVR8y85yYlR9YKwgDIrW+s9YRg0/uR8x 7IpxTVNgAEb5DAcacn2ntxewaPIH9AxUbuUKAYp6Pqaxzyfd8vT7rKZKRU8t9I9XCAPi FNzp4FLjm7rnIpNSbrxzOff4ALmtTK2ORoDbnfUFjiFjnxpMXsFTTQqyyrgoD4Ldq2bQ BIZDrbjMbLA3hzMEobPEVdVTIJnDYwch0q0ZA240gIXn6Lbpfh3l2Wm+/FZ5wzGdPgxK MHZLd6eI+c7UMoYG+B5Vs2V5yRn0VbKNt35a9Mlhxl0lZqRw6bKL9LlVDSJR+ct4aOVr t7Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=S+wRpDpqOkPrrDXB0zwpd1xRo94g25vtsqje1oWV7Ug=; b=tbJOXAxOSVi8Ezw78I6sOLtkYJfW4mts1Egb5RwIWws2DjuvtPYSyEA8tx2BrNDDJt eFWB/CpLHwYIwpZPzfv/J05EGn+7YZFEN3X9FmI0C0dn6ajsDBIsYvMTYShGlzAyzdnk 6ZJBPc1B8QNcBaCY1k95JcaP4gOG8dq1vLXCUCdLm8ubg+dPUGrwAS11A6vtc/5cm0t9 oLm+TKVJ139Qstqqkuu1tQ0bHAn6mLanJZ/jA8rZt+h0615fqedykte2w3qua/YsjwZD yQUln3zA4Isd73jUsG039NQnn6XlD/07xEti+XjMDSydJjRdNyjOo/XqOI/EQTcgC7KL FIUQ== X-Gm-Message-State: AOAM532EAMuMxIQfapKVqelx/d7IhECQyiwcuCFrdt6IzBG+NKwP+rdl anLtf8NVTwe3Y0S37V2p7IbWbUsgbq4A0jLCN+MKSy5Kj61Q2Q== X-Google-Smtp-Source: ABdhPJwgJuGQ0cYUsGhOZGqFTaiSeSc/Pz5gEa5MEzqvZcFolSMvYrliLvH7PcQhcqhugCfqATi9bmZ3IotQkZmzuuQ= X-Received: by 2002:a63:1b5e:0:b0:36f:e756:c118 with SMTP id b30-20020a631b5e000000b0036fe756c118mr13759188pgm.562.1645974698580; Sun, 27 Feb 2022 07:11:38 -0800 (PST) MIME-Version: 1.0 References: <621a56dd.1c69fb81.67b1.242aSMTPIN_ADDED_MISSING@mx.google.com> <621ab36a.1c69fb81.b1a5.462dSMTPIN_ADDED_MISSING@mx.google.com> <621b6215.1c69fb81.ae9e6.4901SMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <621b6215.1c69fb81.ae9e6.4901SMTPIN_ADDED_MISSING@mx.google.com> Date: Sun, 27 Feb 2022 16:11:27 +0100 Message-ID: To: Mark Randall Cc: internals Content-Type: multipart/alternative; boundary="00000000000037e27005d90159fb" Subject: Re: [PHP-DEV] Re: Proposal for RFC to remove undefined arrayindexwarning when used ina ternary From: landers.robert@gmail.com (Robert Landers) --00000000000037e27005d90159fb Content-Type: text/plain; charset="UTF-8" On Sun, Feb 27, 2022 at 12:35 PM Mark Randall wrote: > On 27/02/2022 09:12, Robert Landers wrote: > > I'd also venture that this warning has caused more harm than good, in > that > > writing "$var['something'] ?? null" is second nature when writing new > > code, even if there is practically no chance for a non-existent key. > > > > Using null coalesce should only be used when you know you have the > possibility of a missing key. > That can't always be known, or requires tracing the stack to see where it comes from and what modifies the array before it is passed to the function you are modifying, it is an array after all, not a formalized object with well-defined properties (which I'd prefer, but we can't always change what someone did 15 years ago just for the sake of changing it). > > For everything else you've probably entered an unexpected state and > should display a warning (that should ideally be picked up by an > exception handler and convert it to an exception). > Not always, and not in cases where people are just coalescing null into null to avoid a warning. > > When handling external data it is usually a good idea to use a > validation step before using it, such as JSON schema. > I don't usually write code dealing with handling external API requests in my day-to-day work, so I can't really speak to JSON handling, specifically. I have had to unserialize objects from JSON files from other internal systems but that was back when there was only the Elvis operator (PHP 5.6ish), so it was used quite liberally. > > An even better idea is to parse it into a specific type, rather than > just validating it, at which point you're going to be using a lot of > isset, null coalesces, array_key_exists, property_exists etc anyway and > sucking in some default other than null is almost always the wrong thing > to do. > I'm only suggesting this apply to ternary tests, not in general, so that code can be easier to read and grok. I believe null is a sensible default in cases when you are only establishing the truthiness of a value, and whether or not the key exists is irrelevant (otherwise there's the null coalesce operator and isset()). There may be times when you need more validation, but those aren't usually handled in ternaries. In other words, I believe this should still cause a warning if it is unset: $arr['unset'] ?: $arr['unset']; because it is likely a bug and used outside of the ternary test (the part before the "?" -- I'm not sure what the technical name is for it). This should not, however: $arr['unset'] ?: 'some default'; because it is only used in the test. I also like the suggestion of creating some null-safe operator for arrays, which might be a more general solution. > > As to your final point: PHP internals voted by a supermajority to raise > this from a notice to a warning in PHP 8, it seems unlikely that 33% of > people are now going to change their mind and vote for the opposite. > > We've had to live with it for a little while now and we have a pretty good idea of what it is doing to codebases and the time it takes to "fix" codebases by null coalescing null to null. I tried searching GitHub for this, but it looks like the search functionality removes the question marks in my query. --00000000000037e27005d90159fb--