Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117044 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68432 invoked from network); 15 Feb 2022 15:24:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Feb 2022 15:24:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1C7BB180541 for ; Tue, 15 Feb 2022 08:42:10 -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=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (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 ; Tue, 15 Feb 2022 08:42:09 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id E803F5C01D9 for ; Tue, 15 Feb 2022 11:42:08 -0500 (EST) Received: from imap43 ([10.202.2.93]) by compute5.internal (MEProxy); Tue, 15 Feb 2022 11:42:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=Z9yUD6FWMLHgu0C934w5cfOkm9ZtlJjFJ0teWfYnvoc=; b=lWF4WdAz YwZSDMn5G1fOBZ24vyiP3cVA3bjYTsTiICTDRPE/VEfWQKAlwzKr1zO2UKKdgt96 SPXgtswfgyfyT8Ko85I92TsZ7jCPQl3VKcjhQYjGvC3rVtNreOt8cCM6F/UjKl82 5OMW2s7LpdrSP7ZQzdk8UULPM9gIrxe2NzG3JqbHdnTyyQEN2/ZxfPaVsY2MNX8H Eic0GQAhFtQGs8fmxRwrC8TuL98shjlxo2DCz/CIE2rkZyxSyMxj3SO9TVDq3OrS y1hgsx3DIDB6OPCKlyY4RUaTrLQypH/vtpjkn7xsh3p6grP+RJpA8H8FugXmIyvN mVevpMSKxkDNTg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrjeeggdeltdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeggeehgfetjeehgefggefhleeugefgtdejieevvdet hfevgeeuudefleehvdetieenucffohhmrghinhepphhhphdrnhgvthenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhi vghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 9EBB8AC0E99; Tue, 15 Feb 2022 11:42:08 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-4773-gd41406ac52-fm-20220208.001-gd41406ac Mime-Version: 1.0 Message-ID: <153fc3b5-5409-44ad-b618-84297aa49a3d@www.fastmail.com> In-Reply-To: <67207814-3052-b116-e856-33e8440049d3@gmx.net> References: <67207814-3052-b116-e856-33e8440049d3@gmx.net> Date: Tue, 15 Feb 2022 10:41:44 -0600 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Setting to disable the "Undefined array index" warning From: larry@garfieldtech.com ("Larry Garfield") On Tue, Feb 15, 2022, at 6:54 AM, Andreas Leathley wrote: > On 15.02.22 13:31, Nicolas BADIA wrote: >> As it is explained in this ticket https://bugs.php.net/bug.php?id=3D8= 1417 we use to check if a property exists by accessing it directly like = we do in JavaScript. >> >> Personally, I subscribe to this coding style and we use it all over o= ur codebase (more than 130 000 lines of PHP code). When it became a noti= ce, we disabled them, but now that it is a warning, it is a lot more pro= blematic for us=E2=80=A6 We can=E2=80=99t even use the last version of P= HP Debug for VSCode. > > The problem with your way of writing code is that it is ambiguous in > meaning, which is why this is a warning. You are not checking if a key > in an array exists, you are checking if an array value is "false-y". If > the value is an empty string, it is also interpreted as false, if it is > null, it is also interpreted as false, if it is an integer of value ze= ro > it is also interpreted as false, if it is the string "0" it is also > interpreted as false. > > When you write code as you do, it is easy to introduce subtle errors > because of these implicit casts. If you want to check if an array key = is > defined, you should do it explicitly - this is not about a "coding > style", this is about writing the code in a way that actually does what > you intend it to do without it meaning multiple things of which some a= re > probably not expected. If you just want a quick fix without solving the > underlying problem, you can just add "?? null" to each array key acces= s. > > If you want to upgrade/improve your code base, which is a worthwhile > task, I would suggest the use of a static analyzer (like Psalm or > PHPStan) to find out where your code is ambigious. I have found many > undetected errors that way, where implicit casts have lead to unexpect= ed > outcomes, which is why I am very much in favor of these warnings for > undefined array keys. As a data point, TYPO3 is a roughly 500,000 line code base. (Non-commen= t lines of code as reported by phploc.) It also relied very heavily on = the old "silently ignore missing values and pretend it's a null" behavio= r, which broke badly in PHP 8. It took one person (me) about 3 weeks I think of running unit tests, add= ing ?? in various places (or array_key_exists, or whatever made sense in= context), running tests again, etc. to fix nearly all of them. There's= still a few that pop up now and again that didn't have test coverage, b= ut they're rare. And that's me doing it all 100% manually, no Rector or= similar automation tools. Yes, there is work required for this change. However, with a code base = 1/4 the size, and using better automation tools than I did you should be= able to address all the upgrade issues in less than one person-week. And that's without even getting into the question of array-centric code = with properties being maybe-undefined is already a code smell, and has b= een since PHP 4. (There's been a lot of very smelly code from the PHP 4= era, but it was smelly even then.) Even without any (probably good) ch= anges to the architecture or business logic of the application, this wou= ld improve the quality of the application. I'd wager it's less work in terms of raw time to just fix up the code ba= se than it is to write, implement, debate, pass, and merge an RFC to mak= e you not have to do so. --Larry Garfield