Skip to content

Commit 0459dc6

Browse files
committed
#[Parameter]: throw if not set without default value
1 parent 914105f commit 0459dc6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Application/UI/Component.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,14 @@ public function loadState(array $params): void
172172
}
173173

174174
$this->$name = &$params[$name];
175-
} else {
175+
} elseif ($meta['hasDefault']) {
176176
$params[$name] = &$this->$name;
177+
} else {
178+
throw new Nette\InvalidArgumentException(sprintf(
179+
'Missing parameter $%s required by %s.',
180+
$name,
181+
$this instanceof Presenter ? "presenter {$this->getName()}" : "component '{$this->getUniqueId()}'",
182+
));
177183
}
178184
}
179185

src/Application/UI/ComponentReflection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ComponentReflection extends \ReflectionClass
3030

3131
/**
3232
* Returns array of class properties that are public and have attribute #[Persistent] or #[Parameter].
33-
* @return array<string, array{def: mixed, type: string, since: ?string}>
33+
* @return array<string, array{def: mixed, type: string, since?: ?string, hasDefault: boolean}>
3434
*/
3535
public function getParameters(): array
3636
{
@@ -48,16 +48,20 @@ public function getParameters(): array
4848
self::parseAnnotation($prop, 'persistent')
4949
|| $prop->getAttributes(Attributes\Persistent::class)
5050
) {
51-
$params[$prop->getName()] = [
52-
'def' => $prop->getDefaultValue(),
51+
$param = [
5352
'type' => ParameterConverter::getType($prop),
5453
'since' => $isPresenter ? Reflection::getPropertyDeclaringClass($prop)->getName() : null,
5554
];
5655
} elseif ($prop->getAttributes(Attributes\Parameter::class)) {
57-
$params[$prop->getName()] = [
56+
$param = [
5857
'type' => (string) ($prop->getType() ?? 'mixed'),
5958
];
59+
} else {
60+
continue;
6061
}
62+
$param['def'] = $prop->getDefaultValue();
63+
$param['hasDefault'] = $prop->hasDefaultValue();
64+
$params[$prop->getName()] = $param;
6165
}
6266

6367
if ($this->getParentClass()->isSubclassOf(Component::class)) {
@@ -77,7 +81,7 @@ public function getParameters(): array
7781

7882
/**
7983
* Returns array of persistent properties. They are public and have attribute #[Persistent].
80-
* @return array<string, array{def: mixed, type: string, since: string}>
84+
* @return array<string, array{def: mixed, type: string, since: ?string, hasDefault: bool}>
8185
*/
8286
public function getPersistentParams(): array
8387
{

0 commit comments

Comments
 (0)