Skip to content

Commit 894f204

Browse files
committed
PresenterComponentReflection::combineArgs() throws exception when parameter has scalar type hint, no default value and argument is missing
1 parent 3550941 commit 894f204

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/Application/UI/PresenterComponentReflection.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,30 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
119119
$res = [];
120120
foreach ($method->getParameters() as $i => $param) {
121121
$name = $param->getName();
122-
if (!isset($args[$name]) && $param->isDefaultValueAvailable()) {
123-
$res[$i] = $param->getDefaultValue();
124-
} else {
125-
$res[$i] = $arg = isset($args[$name]) ? $args[$name] : NULL;
126-
list($type, $isClass) = self::getParameterType($param);
122+
list($type, $isClass) = self::getParameterType($param);
123+
if (isset($args[$name])) {
124+
$res[$i] = $args[$name];
127125
if (!self::convertType($res[$i], $type, $isClass)) {
128126
throw new BadRequestException(sprintf(
129127
'Argument $%s passed to %s() must be %s, %s given.',
130128
$name,
131129
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(),
132130
$type === 'NULL' ? 'scalar' : $type,
133-
is_object($arg) ? get_class($arg) : gettype($arg)
131+
is_object($args[$name]) ? get_class($args[$name]) : gettype($args[$name])
134132
));
135133
}
134+
} elseif ($param->isDefaultValueAvailable()) {
135+
$res[$i] = $param->getDefaultValue();
136+
} elseif ($type === 'array') {
137+
$res[$i] = [];
138+
} elseif ($type === 'NULL') {
139+
$res[$i] = NULL;
140+
} else {
141+
throw new BadRequestException(sprintf(
142+
'Missing parameter $%s required by %s()',
143+
$name,
144+
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName()
145+
));
136146
}
137147
}
138148
return $res;

tests/UI/PresenterComponentReflection.combineArgs.php7.phpt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ test(function () {
6060
test(function () {
6161
$method = new ReflectionMethod('MyPresenter', 'hints');
6262

63-
Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, []));
64-
Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => NULL, 'bool' => NULL, 'str' => NULL, 'arr' => NULL]));
6563
Assert::same([1, TRUE, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]]));
66-
Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => '', 'arr' => []]));
64+
Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => ''])); // missing 'arr'
65+
66+
Assert::exception(function () use ($method) {
67+
Reflection::combineArgs($method, []);
68+
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
6769

6870
Assert::exception(function () use ($method) {
6971
Reflection::combineArgs($method, ['int' => '']);
7072
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.');
7173

74+
Assert::exception(function () use ($method) {
75+
Reflection::combineArgs($method, ['int' => NULL]);
76+
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
77+
7278
Assert::exception(function () use ($method) {
7379
Reflection::combineArgs($method, ['int' => new stdClass]);
7480
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.');
@@ -184,11 +190,11 @@ test(function () {
184190

185191
Assert::exception(function () use ($method) {
186192
Reflection::combineArgs($method, []);
187-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, NULL given.');
193+
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
188194

189195
Assert::exception(function () use ($method) {
190196
Reflection::combineArgs($method, ['req' => NULL, 'opt' => NULL]);
191-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, NULL given.');
197+
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
192198

193199
Assert::exception(function () use ($method) {
194200
Reflection::combineArgs($method, ['req' => $method, 'opt' => NULL]);

tests/UI/PresenterComponentReflection.combineArgs.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ test(function () {
7272

7373
Assert::exception(function () use ($method) {
7474
Reflection::combineArgs($method, []);
75-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, NULL given.');
75+
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
7676

7777
Assert::exception(function () use ($method) {
7878
Reflection::combineArgs($method, ['req' => NULL, 'opt' => NULL]);
79-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, NULL given.');
79+
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
8080

8181
Assert::exception(function () use ($method) {
8282
Reflection::combineArgs($method, ['req' => $method, 'opt' => NULL]);

0 commit comments

Comments
 (0)