Skip to content

Switched to non-null defaults in exception constructors #40271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2021

Conversation

derrabus
Copy link
Member

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets N/A
License MIT
Doc PR N/A

PHP 8.1 will trigger a deprecation warning if we pass null as $message or $code to the constructor of \Exception. However, many of our own exception accept null for those parameters and even use them as default.

This is unfortunate because code like the following snippet would trigger that deprecation although the code itself is perfectly fine:

throw new NotFoundHttpException();

With this PR, I'd like to change our defaults to '' and 0 while still allowing to pass null for BC. In a follow-up PR for the 5.x branch, I'd like to deprecate passing null, matching the future behavior of PHP.

This PR also adjust various PHPDoc blocks with inaccurate types.

@carsonbot carsonbot added this to the 4.4 milestone Feb 21, 2021
@derrabus derrabus force-pushed the bugfix/exception-constructors branch from 5b1becb to b4b3ebe Compare February 21, 2021 20:19
@@ -18,7 +18,7 @@
*/
class FileLoaderImportCircularReferenceException extends LoaderLoadException
{
public function __construct(array $resources, int $code = null, \Throwable $previous = null)
public function __construct(array $resources, ?int $code = 0, \Throwable $previous = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we pass $code ?? 0 to the parent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a decision we have to make. PHP 8.1 deprecates passing null as $code. Do we want to hide that deprecation from the caller? Then we should map null to zero. Or do we want to pass that deprecation down to the caller instead?

Note that all of our exception classes that don't override the constructor will trigger the new deprecation on null arguments, so not mapping null arguments would be more or less consistent behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I see, let's keep as is then, we can figure out later anyway

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derrabus we hit this in 4.4 now. I fix it on our side which has to be done anyway, but yet seems unhandled and undecided in this codebase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you have to do it anyway. See my explanation above.

@nicolas-grekas nicolas-grekas force-pushed the bugfix/exception-constructors branch from b4b3ebe to f8e1009 Compare February 22, 2021 15:37
@nicolas-grekas
Copy link
Member

Thank you @derrabus.

@nicolas-grekas nicolas-grekas merged commit f3529fd into symfony:4.4 Feb 22, 2021
@derrabus derrabus deleted the bugfix/exception-constructors branch February 22, 2021 15:54
@nicolas-grekas
Copy link
Member

Merged in 5.2, in case there are remaining ones there.

@derrabus
Copy link
Member Author

I'll check.

chalasr added a commit that referenced this pull request Feb 24, 2021
…tructors (derrabus)

This PR was merged into the 5.2 branch.

Discussion
----------

[Config] Switched to non-null defaults in exception constructors

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

Follow-up to #40271 on the 5.2 branch.

Commits
-------

2e865ac Switched to non-null defaults in exception constructors
fabpot added a commit that referenced this pull request Feb 25, 2021
…ons (derrabus)

This PR was merged into the 5.3-dev branch.

Discussion
----------

Deprecate passing null as $message or $code to exceptions

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

Follow-up to #40271.

Following the example of the PHP core, this PR introduces deprecation warnings that are triggered if a developer attempts to pass null as `$code` or `$message` to an exception constructor.

Commits
-------

8e3058d Deprecate passing null as $message or $code to exceptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants