Skip to content

Commit 580197a

Browse files
authored
[11.x] Handle allows null parameter instead of requiring default value (#52866)
* [11.x] Handle allows null parameter instead of requiring default value Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 4c78508 commit 580197a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Illuminate/Container/Container.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,10 @@ protected function resolvePrimitive(ReflectionParameter $parameter)
10891089
return [];
10901090
}
10911091

1092+
if ($parameter->hasType() && $parameter->allowsNull()) {
1093+
return null;
1094+
}
1095+
10921096
$this->unresolvablePrimitive($parameter);
10931097
}
10941098

tests/Container/ContextualAttributeBindingTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public function testAttributeOnAppCall()
228228
$container->singleton('config', fn () => new Repository([
229229
'app' => [
230230
'timezone' => 'Europe/Paris',
231+
'locale' => null,
231232
],
232233
]));
233234

@@ -236,6 +237,35 @@ public function testAttributeOnAppCall()
236237
});
237238

238239
$this->assertEquals('Europe/Paris', $value);
240+
241+
$value = $container->call(function (#[Config('app.locale')] ?string $value) {
242+
return $value;
243+
});
244+
245+
$this->assertNull($value);
246+
}
247+
248+
public function testNestedAttributeOnAppCall()
249+
{
250+
$container = new Container;
251+
$container->singleton('config', fn () => new Repository([
252+
'app' => [
253+
'timezone' => 'Europe/Paris',
254+
'locale' => null,
255+
],
256+
]));
257+
258+
$value = $container->call(function (TimezoneObject $object) {
259+
return $object;
260+
});
261+
262+
$this->assertEquals('Europe/Paris', $value->timezone);
263+
264+
$value = $container->call(function (LocaleObject $object) {
265+
return $object;
266+
});
267+
268+
$this->assertNull($value->locale);
239269
}
240270

241271
public function testTagAttribute()
@@ -404,3 +434,21 @@ public function __construct(#[Storage('foo')] Filesystem $foo, #[Storage('bar')]
404434
{
405435
}
406436
}
437+
438+
final class TimezoneObject
439+
{
440+
public function __construct(
441+
#[Config('app.timezone')] public readonly ?string $timezone
442+
) {
443+
//
444+
}
445+
}
446+
447+
final class LocaleObject
448+
{
449+
public function __construct(
450+
#[Config('app.locale')] public readonly ?string $locale
451+
) {
452+
//
453+
}
454+
}

0 commit comments

Comments
 (0)