On 28 May 2022, at 03:36, Craig Francis <cr...@craigfrancis.co.uk> wrote:
> On 8 Apr 2022, at 18:34, Craig Francis <cr...@craigfrancis.co.uk> wrote:
>> I've written a new draft RFC to address the NULL coercion problems:
>> https://wiki.php.net/rfc/null_coercion_consistency
> 
> 
> I give up.



For everyone affected by this... Rector has a "solution" via 
NullToStrictStringFuncCallArgRector.

It has a list of 275 functions (442 parameters), and every time these functions 
are used, and Rector isn't sure a variable cannot be NULL, it will add a string 
type cast, e.g.

    - <?= htmlspecialchars($var) ?>
    + <?= htmlspecialchars((string) $var) ?>

So yeah, it's very messy, and while I think it makes the code worse, at least 
it's one way to avoid the fatal errors (coming in 9.0).

Oh, and developers can easily re-introduce more "problems" (because they might 
not be aware which variables can be NULL), so Rector will need to re-run.

I'm also tempted to create a composer library to provide a second solution, by 
using a namespace to re-define these functions, e.g.

    <?php
    namespace null_coercion_fixer;
    function strlen(?string $string): int {
        return \strlen(\strval($string));
    }
    ?>

Have fun,
Craig



https://github.com/rectorphp/rector-src/blob/main/rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php#L37

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to