On Tue, June 3, 2025 at 04:29 Christoph M. Becker wrote:
> On 02.06.2025 at 18:11, G. P. Banyard wrote:
>
>> The RFC proposes to deprecate implicit type coercions to and from the bool 
>> type
>> for other scalar types. This a "weak" mode change only, as when strict_types 
>> are
>> enabled none of these coercions can happen.
>>
>> Let me know what you think about it.
>>
>> RFC: https://wiki.php.net/rfc/deprecate-function-bool-type-juggling
>
> Let's consider a legacy (PHP 5) function:
> 
>     function foo($bool)
>     {
>         if ($bool) {
>             // something
>         } else {
>             // something else
>         }
>     }
>
> Now the developer wants to add the missing parameter type declaration.
> As is, even without static analysis or a test suite, they can just add
> `bool`, being reasonable sure that the code behaves like before for
> scalar types, and only fails for non-scalars, something they may be fine
> with.  With the suggested change, they can no longer, but would rather
> have to inspect and possibly "fix" all callers.

Isn't this already the case? If e.g. someone is passing a nullable string
to the function, adding a `bool` type would cause this to break, which
necessitates inspecting all the callers of the function to make sure null
isn't being passed anywhere.

The proposed RFC doesn't change anything for null, but only deprecates
passing float, string, or int to a `bool` parameter. In my experience,
such usages almost always indicate bugs, and receiving a deprecation
notice would be valuable for fixing them.

In regards to a unified typing mode, personally I don't see any benefit
from `declare(strict_types=1)` anymore, since with static analysis tools
like Psalm strict typing is enforced before even running the code.

Regards,  
Theodore

Reply via email to