Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117835 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50169 invoked from network); 30 May 2022 12:20:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2022 12:20:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 70562180549 for ; Mon, 30 May 2022 07:04:30 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 30 May 2022 07:04:29 -0700 (PDT) Received: by mail-yb1-f179.google.com with SMTP id h75so13210602ybg.4 for ; Mon, 30 May 2022 07:04:29 -0700 (PDT) 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=ausj3p25iD46MHEObYMSLEiNPKLWhxEctxUMVG1kNGM=; b=dQcPkfEN8IILX2wzW/Q0nMKnXceJV0Jtac1JmuTdqOF8vnK7U4tvnPenrEgBhEi4VG yuiosDgK9/3MAw/z57xChrE9EQ/JruDZXmbOUo7pP3vFWmd9rf6FUETzf2JnTp4rC92H GTRsnLiW4A3b/rg9qt9mR5Yfk/Msp0tcWSJv8TETqghu0gVlMlG9GN3nwZPpXbPykVIy 3JLQR4LELUbdLFdeTO5K0GWN+bvzVP4IwFG6+gtrDbXFOFQzAMcGOMV19EWfGAdUWKnZ L823GatVQ81RjIgNmhzwdFYMy5y9EeNX4DSPogkC2ZysZsWr05VsVBXb6TqFUhQ3wL27 fnpA== 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=ausj3p25iD46MHEObYMSLEiNPKLWhxEctxUMVG1kNGM=; b=XAF1xgde/bRhtUiHttowq/pfpkmqkBeIu1XGqRwKA0f9gaJ5+rq+MdH89dEtNlMPVE PAs/bp7JMOuiMGT+JubyVhnPra1XguFV8kBHRuEtCggh12cNSFoSGGqS/NYM8rwrrcJE cjiSf0wqCP8FMfpTZK3/4RKxHXhA70A4dDpaAOiDttfoZ8B9uTCN3TokDTnwyhzqWwVf Rmqe/AYxBXovOnCq+2r7XV5ujxUP1n5rFfTpZ67O8JRwarQ9+u6Eh6UbJ4WtPUZCtKPc mAAkTXSevkWXMeoWWUhCHIFEXwacnsImYwglfa10TILfrxpBiwMouicSpD8pOeioaDDM jVKw== X-Gm-Message-State: AOAM532zcW74nPsylAtoB1ZXgsYIv61u2uO/wefEgSb8bR5jNU7kZgsM RUuhzCOVLDsm+h+iM3rv/WSmNYwZnMZ7MPA8VA== X-Google-Smtp-Source: ABdhPJxkWoGU1We5kjEPOg7UBxIbAiRGsJzZmj2CiuYF2+N/PbPH61V7PnXgPbuNPwqP33t9soDDUPvGX9CJ1H87rbM= X-Received: by 2002:a25:3cc6:0:b0:659:f068:f306 with SMTP id j189-20020a253cc6000000b00659f068f306mr16840907yba.505.1653919469446; Mon, 30 May 2022 07:04:29 -0700 (PDT) MIME-Version: 1.0 References: <1755E8B5-229B-47B2-BBAF-B5E014F5473D@craigfrancis.co.uk> <1180af01-080f-ee0a-3159-74bf7e0a8aea@gmail.com> In-Reply-To: <1180af01-080f-ee0a-3159-74bf7e0a8aea@gmail.com> Date: Mon, 30 May 2022 16:04:18 +0200 Message-ID: To: Rowan Tommins Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Re: NULL Coercion Consistency From: guilliam.xavier@gmail.com (Guilliam Xavier) On Tue, May 24, 2022 at 10:47 AM Rowan Tommins wrote: > > As an anecdote, I was recently working on a bug involving nulls causing > unintended behaviour in an API. As part of the clean-up, I changed a > function signature from getByIdentifer($identifier) to > getByIdentifer(string $identifier), and during testing, got an error > because I'd missed a scenario where null was passed. This was a good > result - it prevented the broken behaviour and alerted me to the case > that needed fixing. If the parameter had instead been silently coerced > to an empty string, I would have got neither an error nor the original > null behaviour, causing a new bug that might have taken much longer to > track down. > > If your RFC as drafted was implemented, I could only have achieved the > desired check by turning on strict_types=1 for whole sections of the > application, which would probably require dozens of other fixes, or > writing an ugly manual check: > > function getByIdentifier(?string $identifier) { > if ( $identifier === null ) { > throw new TypeError("Function doesn't actually accept nulls"); > } > // ... > } For this specific example, shouldn't it rather [already] be like this anyway? function getByIdentifier(string $identifier) { if ( $identifier === '' ) { throw new InvalidArgumentException("Empty identifier"); } // ... } (but I guess you could find actual examples where an empty string is "valid" and null is not, indeed) > As I have said previously, I share your concern about the impact of the > currently planned change, but I don't think loosening the existing type > rules on user-defined functions is a good solution. I agree the "issue" looks like different PoV of benefit VS cost, plus the reticence about going back on the general trend of "more strictness" (even with strict_types=0). I'm just worried to see people rather disabling deprecation notices (via error_reporting, or a custom error handler, or even patching PHP's source and recompiling their own) than "fixing" the code (granted, that's not specific to *this* RFC, but somewhat "highlighted" here) FWIW, to avoid the "risks" of `expr ?? ''` (possibly hiding undefined) and `(string)expr` / `strval(expr)` (potentially casting "too much" without errors), I've already seen custom functions like function null_to_empty_string(?string $string_or_null): string { return $string_or_null === null ? '' : $string_or_null; } (but also its "opposite" empty_string_to_null(string $string): ?string) Regards, -- Guilliam Xavier