Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117140 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44035 invoked from network); 26 Feb 2022 07:49:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2022 07:49:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 14D241804C9 for ; Sat, 26 Feb 2022 01:09:54 -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.6 required=5.0 tests=BAYES_50,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-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 ; Sat, 26 Feb 2022 01:09:53 -0800 (PST) Received: by mail-pg1-f181.google.com with SMTP id c1so6835045pgk.11 for ; Sat, 26 Feb 2022 01:09:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=otWVMcWLUVcdKHp1HH5W2z9kK6Es7Z7xejS9xUn63lo=; b=Py5iVq2fVIiJK7j98BeM+pq96OH/udHTrErYsFjeK8JLsDSAETIEr2J6kU8fO2w6xm wAlJKd9U7HsNfVuyt6bvWffQK9/8gSV7TPT+MLpU3PDHVLkVwXJ1jn1m5FiX4pYgFJpj sAbaegLxJ2Ad6apqtcHMKHHyHZAisRvCoGPOo++Wpxa2Bqf+D7ZC0pdkyTLI2QS+iDlw xFRPLaGqiW2IloG8PbBuMxy256KCX+MrlmbxzjoqZ+TUBzZbLgBALasRx8FIcaW+1vZx OTGJ1PIdaASifXZl/bHVqLO7/LhnQIhm0O0Ifa/moPmWoZ39bemTnT7vZR+aTtfc2xQv 3QJQ== 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=otWVMcWLUVcdKHp1HH5W2z9kK6Es7Z7xejS9xUn63lo=; b=e153AlGTQhcT/Nz8dfuegV+Rj5Qq7ExbuseimzlxoAGH/x9ZbN3ZJ7c4dNdGis6gID JU7Cd+I2kp/Bx9dLgkU1v15e8IV0vqALNMtk5PbpIZ1NfQ8kYja8TJO7qGoz8kut4FtK SL7hHviA3SakfTz6QPRh6DpkKXi8PITJrtA+pGaNty+1sdTz+my8A3eH1HkVlu4VCNJ1 +MSP4DvSKXknhfZ5uA7rBX8p9C1EafmQ7dsEdBrs/cpuNvP2JXMgHFotJgmZ3+KQROB4 K1yV/Do03ru+hdb75EtBRdWNfEPa0cB0OJw4tqwMN26VaD4htego3jk5GDpxyC+BJE05 9yUw== X-Gm-Message-State: AOAM530+SSzUjqq63uJ9l9jGHLzcTsfsb5sSBCftt1m26jT0EHnq1bR3 bdV1qCj5C+9qkgaIWJuQXygODk2JIZXEAd1SBZF27Faf68FXRf2O X-Google-Smtp-Source: ABdhPJzYkJtCboYaFmG8CMWSVsY8VEjqO6Z5PkVOknseSYz4hK2escglhSvVx6sgEm1aGFLnE7PshpW+91NrTVPGVr0= X-Received: by 2002:aa7:8199:0:b0:4f1:2b3f:ff05 with SMTP id g25-20020aa78199000000b004f12b3fff05mr11787451pfi.1.1645866592371; Sat, 26 Feb 2022 01:09:52 -0800 (PST) MIME-Version: 1.0 Date: Sat, 26 Feb 2022 10:09:41 +0100 Message-ID: To: internals Content-Type: multipart/alternative; boundary="00000000000096088d05d8e82d90" Subject: Proposal for RFC to remove undefined array index warning when used in a ternary From: landers.robert@gmail.com (Robert Landers) --00000000000096088d05d8e82d90 Content-Type: text/plain; charset="UTF-8" Hello internals! There is a lot of code in existence that does something like this: $item['id'] ? [$item['id']] : $something_else which to dissolve the current warning must be changed to something like this: !empty($item['id']) ? [$item['id']] : $something_else However, if performance is an issue, we can do this in one less opcode (in fact, the same number of opcodes as the original): empty($item['id']) ? $something_ else : [$item['id']] As far as I can tell, when single array access is the only test in a ternary operation, it is exactly the same semantics as wrapping the array access in a "!empty()". Fixing this warning seems like a lot of "busy work" with little to no tangible benefit. I'd like to propose and implement an RFC that would consider dropping the warning if and only if array access is the only test in ternary and short ternary (?:) operations but first I'd like to start a discussion to see if it is worth pursuing. I'd like to target PHP 8.2 or 8.3. It may also be worth discussing extending it to multiple array accesses because to invert the not empties is counter-intuitive and error-prone: !empty($a['test']) && !empty($a['ok']) ? 'a' : 'b'; becomes: empty($a['test']) || empty($a['ok']) ? 'b' : 'a'; which was before (and IMHO, more readable): $a['test'] && $a['ok'] ? 'a' : 'b'; This is very tricky to get right and to be fair, there will probably be automated tools (if you trust such things on large codebases). There are also performance issues to consider, as some basic benchmarks show ISSET_ISEMPTY_DIM_OBJ is 6x slower than FETCH_DIM_R. Robert Landers Software Engineer Utrecht NL --00000000000096088d05d8e82d90--