Skip to content

Commit e9a0f67

Browse files
committed
Presenter: added support for typehint 'iterable' [Closes #203]
1 parent 1dbc728 commit e9a0f67

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

src/Application/UI/ComponentReflection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a
170170
$res[$i] = $param->getDefaultValue();
171171
} elseif ($type === 'NULL' || $param->allowsNull()) {
172172
$res[$i] = null;
173-
} elseif ($type === 'array') {
173+
} elseif ($type === 'array' || $type === 'iterable') {
174174
$res[$i] = [];
175175
} else {
176176
throw new Nette\InvalidArgumentException(sprintf(
@@ -198,7 +198,7 @@ public static function convertType(&$val, string $type, bool $isClass = false):
198198
} elseif ($type === 'NULL') { // means 'not array'
199199
return !is_array($val);
200200

201-
} elseif ($type === 'array') {
201+
} elseif ($type === 'array' || $type === 'iterable') {
202202
return is_array($val);
203203

204204
} elseif (!is_scalar($val)) { // array, resource, null, etc.

src/Application/UI/Presenter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ public static function argsToParams(string $class, string $method, array &$args,
959959
}
960960

961961
if (!isset($args[$name])) {
962-
if (!$param->isDefaultValueAvailable() && !$param->allowsNull() && $type !== 'NULL' && $type !== 'array') {
962+
if (!$param->isDefaultValueAvailable() && !$param->allowsNull() && $type !== 'NULL' && $type !== 'array' && $type !== 'iterable') {
963963
$missing[] = $param;
964964
unset($args[$name]);
965965
}

tests/UI/ComponentReflection.combineArgs.phpt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ class MyPresenter
1919
}
2020

2121

22-
public function hints(int $int, bool $bool, string $str, array $arr)
22+
public function hints(int $int, bool $bool, string $str, array $arr, iterable $iter)
2323
{
2424
}
2525

2626

27-
public function hintsNulls(int $int = null, bool $bool = null, string $str = null, array $arr = null)
27+
public function hintsNulls(int $int = null, bool $bool = null, string $str = null, array $arr = null, iterable $iter = null)
2828
{
2929
}
3030

3131

32-
public function hintsNullable(?int $int, ?bool $bool, ?string $str, ?array $arr)
32+
public function hintsNullable(?int $int, ?bool $bool, ?string $str, ?array $arr, ?iterable $iter)
3333
{
3434
}
3535

3636

37-
public function hintsDefaults(int $int = 0, bool $bool = false, string $str = '', array $arr = [])
37+
public function hintsDefaults(int $int = 0, bool $bool = false, string $str = '', array $arr = [], iterable $iter = [])
3838
{
3939
}
4040

@@ -68,8 +68,8 @@ test(function () {
6868
test(function () {
6969
$method = new ReflectionMethod('MyPresenter', 'hints');
7070

71-
Assert::same([1, true, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]]));
72-
Assert::same([0, false, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => ''])); // missing 'arr'
71+
Assert::same([1, true, 'abc', [1], [2]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1], 'iter' => [2]]));
72+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => ''])); // missing 'arr', 'iter'
7373

7474
Assert::exception(function () use ($method) {
7575
Reflection::combineArgs($method, []);
@@ -104,10 +104,10 @@ test(function () {
104104
test(function () {
105105
$method = new ReflectionMethod('MyPresenter', 'hintsNulls');
106106

107-
Assert::same([null, null, null, null], Reflection::combineArgs($method, []));
108-
Assert::same([null, null, null, null], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null]));
109-
Assert::same([1, true, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]]));
110-
Assert::same([0, false, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => []]));
107+
Assert::same([null, null, null, null, null], Reflection::combineArgs($method, []));
108+
Assert::same([null, null, null, null, null], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null, 'iter' => null]));
109+
Assert::same([1, true, 'abc', [1], [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1], 'iter' => [1]]));
110+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => [], 'iter' => []]));
111111

112112
Assert::exception(function () use ($method) {
113113
Reflection::combineArgs($method, ['int' => '']);
@@ -134,10 +134,10 @@ test(function () {
134134
test(function () {
135135
$method = new ReflectionMethod('MyPresenter', 'hintsNullable');
136136

137-
Assert::same([null, null, null, null], Reflection::combineArgs($method, []));
138-
Assert::same([null, null, null, null], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null]));
139-
Assert::same([1, true, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]]));
140-
Assert::same([0, false, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => []]));
137+
Assert::same([null, null, null, null, null], Reflection::combineArgs($method, []));
138+
Assert::same([null, null, null, null, null], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null, 'iter' => null]));
139+
Assert::same([1, true, 'abc', [1], [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1], 'iter' => [1]]));
140+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => [], 'iter' => []]));
141141

142142
Assert::exception(function () use ($method) {
143143
Reflection::combineArgs($method, ['int' => '']);
@@ -164,10 +164,10 @@ test(function () {
164164
test(function () {
165165
$method = new ReflectionMethod('MyPresenter', 'hintsDefaults');
166166

167-
Assert::same([0, false, '', []], Reflection::combineArgs($method, []));
168-
Assert::same([0, false, '', []], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null]));
169-
Assert::same([1, true, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]]));
170-
Assert::same([0, false, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => []]));
167+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, []));
168+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, ['int' => null, 'bool' => null, 'str' => null, 'arr' => null, 'iter' => null]));
169+
Assert::same([1, true, 'abc', [1], [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1], 'iter' => [1]]));
170+
Assert::same([0, false, '', [], []], Reflection::combineArgs($method, ['int' => 0, 'bool' => false, 'str' => '', 'arr' => [], 'iter' => []]));
171171

172172
Assert::exception(function () use ($method) {
173173
Reflection::combineArgs($method, ['int' => '']);

tests/UI/ComponentReflection.convertType.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ testIt('array', 1);
159159
testIt('array', 1.0);
160160
testIt('array', 1.2);
161161

162+
testIt('iterable', null);
163+
testIt('iterable', [], []);
164+
testIt('iterable', $obj);
165+
testIt('iterable', '');
166+
testIt('iterable', 'a');
167+
testIt('iterable', '1');
168+
testIt('iterable', '1.0');
169+
testIt('iterable', '1.1');
170+
testIt('iterable', '1a');
171+
testIt('iterable', true);
172+
testIt('iterable', false);
173+
testIt('iterable', 0);
174+
testIt('iterable', 1);
175+
testIt('iterable', 1.0);
176+
testIt('iterable', 1.2);
177+
162178
testIt('callable', null);
163179
testIt('callable', []);
164180
testIt('callable', $obj);

0 commit comments

Comments
 (0)