Skip to content

Commit 7fa20c4

Browse files
committed
Update Container to not inject null for unset nullable parameters
1 parent 72e8ce8 commit 7fa20c4

File tree

4 files changed

+156
-161
lines changed

4 files changed

+156
-161
lines changed

docs/best-practices/controllers.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ covers most common use cases:
190190
* Each class may or may not have a constructor.
191191
* If the constructor has an optional argument, it will be omitted unless an
192192
explicit [container configuration](#container-configuration) is used.
193-
* If the constructor has a nullable argument, it will be given a `null` value
194-
unless an explicit [container configuration](#container-configuration) is used.
195193
* If the constructor references another class, it will load this class next.
196194

197195
This covers most common use cases where the request handler class uses a
@@ -325,27 +323,6 @@ some manual configuration like this:
325323
]);
326324

327325

328-
$app = new FrameworkX\App($container);
329-
330-
// …
331-
```
332-
333-
=== "Nullable values"
334-
335-
```php title="public/index.php"
336-
<?php
337-
338-
require __DIR__ . '/../vendor/autoload.php';
339-
340-
$container = new FrameworkX\Container([
341-
Acme\Todo\UserController::class => function (?string $name) {
342-
// example UserController class uses $name, defaults to null if not set
343-
return new Acme\Todo\UserController($name ?? 'ACME');
344-
},
345-
'name' => 'Demo'
346-
]);
347-
348-
349326
$app = new FrameworkX\App($container);
350327

351328
// …
@@ -415,7 +392,7 @@ all uppercase in any factory function like this:
415392
// Framework X also uses environment variables internally.
416393
// You may explicitly configure this built-in functionality like this:
417394
// 'X_LISTEN' => '0.0.0.0:8081'
418-
// 'X_LISTEN' => fn(?string $PORT = '8080') => '0.0.0.0:' . $PORT
395+
// 'X_LISTEN' => fn(string $PORT = '8080') => '0.0.0.0:' . $PORT
419396
'X_LISTEN' => '127.0.0.1:8080'
420397
]);
421398

src/Container.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
296296
if ($parameter->isDefaultValueAvailable()) {
297297
return $parameter->getDefaultValue();
298298
}
299-
if ($type->allowsNull()) {
300-
return null;
301-
}
302299

303300
throw new \Error(
304301
self::parameterError($parameter, $for) . ' expects unsupported type ' . $type
@@ -321,14 +318,12 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
321318
);
322319
}
323320

324-
// use default/nullable argument if not loadable as container variable or by type
325-
if (!$type instanceof \ReflectionNamedType || $type->isBuiltin() || !\array_key_exists($type->getName(), $this->container)) {
326-
if ($parameter->isDefaultValueAvailable()) {
327-
return $parameter->getDefaultValue();
328-
}
329-
if ($type !== null && $type->allowsNull() && $type->getName() !== 'mixed') {
330-
return null;
331-
}
321+
// use default argument if not loadable as container variable or by type
322+
if (
323+
$parameter->isDefaultValueAvailable() &&
324+
(!$type instanceof \ReflectionNamedType || $type->isBuiltin() || !\array_key_exists($type->getName(), $this->container))
325+
) {
326+
return $parameter->getDefaultValue();
332327
}
333328

334329
// abort if required container variable is not defined or for any other primitive types (array etc.)

0 commit comments

Comments
 (0)