Skip to content

Commit 6991999

Browse files
minor #40271 Switched to non-null defaults in exception constructors (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- Switched to non-null defaults in exception constructors | 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: ```php 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. Commits ------- f8e10094a4 Switched to non-null defaults in exception constructors
2 parents 3a79e94 + edea70c commit 6991999

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Exception/MethodNotAllowedException.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class MethodNotAllowedException extends \RuntimeException implements ExceptionIn
2222
{
2323
protected $allowedMethods = [];
2424

25-
public function __construct(array $allowedMethods, string $message = null, int $code = 0, \Throwable $previous = null)
25+
/**
26+
* @param string[] $allowedMethods
27+
*/
28+
public function __construct(array $allowedMethods, ?string $message = '', int $code = 0, \Throwable $previous = null)
2629
{
2730
$this->allowedMethods = array_map('strtoupper', $allowedMethods);
2831

@@ -32,7 +35,7 @@ public function __construct(array $allowedMethods, string $message = null, int $
3235
/**
3336
* Gets the allowed HTTP methods.
3437
*
35-
* @return array
38+
* @return string[]
3639
*/
3740
public function getAllowedMethods()
3841
{

RouteCollectionBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,11 @@ private function load($resource, string $type = null): array
362362
}
363363

364364
if (null === $resolver = $this->loader->getResolver()) {
365-
throw new LoaderLoadException($resource, null, null, null, $type);
365+
throw new LoaderLoadException($resource, null, 0, null, $type);
366366
}
367367

368368
if (false === $loader = $resolver->resolve($resource, $type)) {
369-
throw new LoaderLoadException($resource, null, null, null, $type);
369+
throw new LoaderLoadException($resource, null, 0, null, $type);
370370
}
371371

372372
$collections = $loader->load($resource, $type);

0 commit comments

Comments
 (0)