Warn when declaring required parameter after optional one

php.internals

Nikita Popov

6 years ago
Hi internals, I've created https://github.com/php/php-src/pull/5067 to make code like function test($foo = null, $bar) {} throw a warning: Warning: Required parameter $bar follows optional parameter Historically, having an "optional" parameter before a required one was useful for poor man's nullable types. That is, one could write function test(FooBar $param = null, $param2) to get an effective function test(?FooBar $param, $param2) signature on old PHP versions that did not have native support for nullable types. Since nullable types have been available since PHP 7.1, having a required parameter after an optional one is increasingly likely a bug rather than an intentional workaround, so I think it would be good to throw a warning for this case. What do you think? Nikita

Sebastian Bergmann

6 years ago
Am 09.01.2020 um 13:26 schrieb Nikita Popov:
> What do you think?
I would prefer erroring out over just emitting a warning.

Mark Randall

6 years ago
On 09/01/2020 12:31, Sebastian Bergmann wrote:
> I would prefer erroring out over just emitting a warning.
I'm in agreement with Sebastian, a compile time error is the way to go. I could understand a warning or deprecation in a minor release, but as there's already so many engine level exceptions landing in 8.0, I think it makes sense to throw this on the pile as well to be swept up in the same pass of migrations that people do in preperation for 8.0. Better to get the pain over and done with, rather than drag it out over multiple versions, as I'd guess that even if it was a warning now, it would end up a compile error a few years from now.
-- Mark Randall

Sebastian Bergmann

6 years ago
Am 09.01.2020 um 13:31 schrieb Sebastian Bergmann:
> I would prefer erroring out over just emitting a warning.
What I meant, of course, was deprecation (or warning) first before erroring out. Sorry.

Bob Weinand

6 years ago
Hey, While I generally agree that it likely is a bug in new code, I would rather deprecate it than warn or even error: the change would make it impossible to retain a type without warning while preserving compatibility with an old PHP version and making incremental migrations harder (would then not be possible to write warning-free code running on 7.0 and 8.0 at the same time while retaining type information). I would like to see it deprecated and then removed in 4+ years. Bob

Nikita Popov

6 years ago
On Thu, Jan 9, 2020 at 2:41 PM Bob Weinand <bobwei9@hotmail.com> wrote:
> Hey, > > While I generally agree that it likely is a bug in new code, I would > rather deprecate it than warn or even error: the change would make it > impossible to retain a type without warning while preserving compatibility > with an old PHP version and making incremental migrations harder (would > then not be possible to write warning-free code running on 7.0 and 8.0 at > the same time while retaining type information). > > I would like to see it deprecated and then removed in 4+ years.
> Bob >
As deprecation and later removal seem to be preferred by multiple people, I've changed the PR to implementation that instead. Nikita

Niklas Keller

6 years ago
Hi Nikita, while this is a rather small change, it has quite some BC impact, as not all old code has been adjusted to run on PHP 7.1+ only using nullable types. I'd like to see an RFC with a vote for this. Regards, Niklas

Olumide Samson

6 years ago
Should it be a warning or an error? --- Sent from Mail for Windows 10 From: Niklas Keller Sent: Saturday, January 11, 2020 2:35 PM To: Nikita Popov Cc: Bob Weinand; PHP internals Subject: Re: [PHP-DEV] Warn when declaring required parameter after optionalone Hi Nikita, while this is a rather small change, it has quite some BC impact, as not all old code has been adjusted to run on PHP 7.1+ only using nullable types. I'd like to see an RFC with a vote for this. Regards, Niklas
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Olumide Samson

6 years ago
Error(in PHP 8) after warning or deprecation in 7.xnything more preferred… --- Sent from Mail for Windows 10 From: Olumide Samson Sent: Saturday, January 11, 2020 2:42 PM To: Niklas Keller; Nikita Popov Cc: Bob Weinand; PHP internals Subject: RE: [PHP-DEV] Warn when declaring required parameter afteroptionalone Should it be a warning or an error? --- Sent from Mail for Windows 10 From: Niklas Keller Sent: Saturday, January 11, 2020 2:35 PM To: Nikita Popov Cc: Bob Weinand; PHP internals Subject: Re: [PHP-DEV] Warn when declaring required parameter after optionalone Hi Nikita, while this is a rather small change, it has quite some BC impact, as not all old code has been adjusted to run on PHP 7.1+ only using nullable types. I'd like to see an RFC with a vote for this. Regards, Niklas
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Nikita Popov

6 years ago
On Sat, Jan 11, 2020 at 2:35 PM Niklas Keller <me@kelunik.com> wrote:
> Hi Nikita, > > while this is a rather small change, it has quite some BC impact, as > not all old code has been adjusted to run on PHP 7.1+ only using > nullable types. > > I'd like to see an RFC with a vote for this. > > Regards, > Niklas >
I was interested in seeing how prevalent this pattern, is, so I ran some analysis on the top 2k composer packages. I found 527 signatures that would throw a deprecation warning with this change. Of these 187 are potentially used as "poor man's nullable types" (the optional argument has both a type and a null default), while the other 340 are definite bugs. Regards, Nikita

Aran Reeks

6 years ago
Hi all, Whilst I totally see the benefits of this, I'm not sure if this is a job for PHP itself, rather a coding standard enforced optionally by something like PHPCS. It's certainly a bad practice, but I'm not sure the benefit will be worth the refactors required. I feel we might be an interesting example, however, it'd cause us a lot of pain for the following reason: We have a portion of our codebase (shared over hundreds of websites) which is symlinked into each of the websites so the core functionality can be easily updated when required on all sites. These sites are staggered at present across multiple PHP versions whilst being migrated (and due to the number of sites to migrate, they will be for some time). If this change were to come into effect, the same code we've safely had symlinked running across multiple PHP versions would suddenly become incompatible on the latest version, with no easy way to be suppressed which would mean we have to fork things and maintain two codebases. I appreciate this isn't likely to be a normal workflow for people, but it is an example of how it could cause unintended pain. Just my thoughts anyway. Cheers, Aran On Fri, 17 Jan 2020 at 18:00, Nikita Popov <nikita.ppv@gmail.com> wrote:

Theodore Brown

6 years ago
On Fri, Jan 17, 2020 at 11:59 AM Nikita Popov <nikita.ppv@gmail.com> wrote: >> I've created https://github.com/php/php-src/pull/5067 to make code like >> function test($foo = null, $bar) {} >> throw a warning > > I was interested in seeing how prevalent this pattern, is, so I ran > some analysis on the top 2k composer packages. I found 527 signatures > that would throw a deprecation warning with this change. Of these 187 > are potentially used as "poor man's nullable types" (the optional > argument has both a type and a null default), while the other 340 are > definite bugs. Given that most of these usages are definite bugs, I'm in favor of deprecating this in PHP 8 and making it a compile error in PHP 9. This should provide plenty of time for codebases to migrate to the simpler nullable types syntax for the minority of usages that aren't bugs. Theodore

Andrew Faulds

6 years ago
Hi Nikita, Nikita Popov wrote:
> > Since nullable types have been available since PHP 7.1, having a required > parameter after an optional one is increasingly likely a bug rather than an > intentional workaround, so I think it would be good to throw a warning for > this case. >
Wouldn't it be trivial to special-case `= NULL` here to not cause a warning, or to cause an E_NOTICE instead of an E_WARNING? That would make this less annoying for old code. I don't know whether I think this worth doing or not, I just want to point out the possibility. Thanks, Andrea