Skip to content

Commit

Permalink
[11.x] Handle allows null parameter instead of requiring default value (
Browse files Browse the repository at this point in the history
#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]>
  • Loading branch information
crynobone authored Sep 20, 2024
1 parent 4c78508 commit 580197a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,10 @@ protected function resolvePrimitive(ReflectionParameter $parameter)
return [];
}

if ($parameter->hasType() && $parameter->allowsNull()) {
return null;
}

$this->unresolvablePrimitive($parameter);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function testAttributeOnAppCall()
$container->singleton('config', fn () => new Repository([
'app' => [
'timezone' => 'Europe/Paris',
'locale' => null,
],
]));

Expand All @@ -236,6 +237,35 @@ public function testAttributeOnAppCall()
});

$this->assertEquals('Europe/Paris', $value);

$value = $container->call(function (#[Config('app.locale')] ?string $value) {
return $value;
});

$this->assertNull($value);
}

public function testNestedAttributeOnAppCall()
{
$container = new Container;
$container->singleton('config', fn () => new Repository([
'app' => [
'timezone' => 'Europe/Paris',
'locale' => null,
],
]));

$value = $container->call(function (TimezoneObject $object) {
return $object;
});

$this->assertEquals('Europe/Paris', $value->timezone);

$value = $container->call(function (LocaleObject $object) {
return $object;
});

$this->assertNull($value->locale);
}

public function testTagAttribute()
Expand Down Expand Up @@ -404,3 +434,21 @@ public function __construct(#[Storage('foo')] Filesystem $foo, #[Storage('bar')]
{
}
}

final class TimezoneObject
{
public function __construct(
#[Config('app.timezone')] public readonly ?string $timezone
) {
//
}
}

final class LocaleObject
{
public function __construct(
#[Config('app.locale')] public readonly ?string $locale
) {
//
}
}

0 comments on commit 580197a

Please sign in to comment.