Skip to content

Commit 9b8c158

Browse files
committed
Change of argument order in call() scalar method
1 parent 318c3b7 commit 9b8c158

File tree

11 files changed

+68
-20
lines changed

11 files changed

+68
-20
lines changed

documentation/upgrading.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ Please follow the instructions for your specific version to ensure a smooth upgr
55

66
---
77

8+
## Upgrading from 0.27.x to 0.28.x
9+
10+
### 1) Change of argument order in `call()` scalar method, and enforcing string types for argument names
11+
12+
Before:
13+
```php
14+
ref('integers')->call(lit('explode'), ['separator' => ','], refAlias: 'string', returnType: type_list(type_integer()))
15+
ref('integers')->call(lit('explode'), ['separator' => ','])
16+
ref('integers')->call(lit('explode'), [','])
17+
ref('integers')->call(lit('count'))
18+
```
19+
20+
After:
21+
22+
```php
23+
ref('integers')->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','], returnType: type_list(type_integer()))
24+
ref('integers')->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','])
25+
ref('integers')->call(lit('count'), 'value')
26+
```
27+
828
## Upgrading from 0.26.x to 0.27.x
929

1030
### 1) Force `EntryFactory $entryFactory` to be required on `array_to_row` & `array_to_row(s)`

examples/topics/transformations/call/code.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
)
2424
->withEntry(
2525
'integers',
26-
ref('integers')->call(lit('explode'), ['separator' => ','], refAlias: 'string', returnType: type_list(type_integer()))
26+
ref('integers')->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','], returnType: type_list(type_integer()))
2727
)
2828
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
2929
->run();

examples/topics/transformations/match/code.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
match_cases(
3535
[
3636
match_condition(ref('string')->contains('-'), ref('string')->strReplace('-', ' ')),
37-
match_condition(ref('string')->call('is_numeric'), ref('string')->cast(type_integer())),
37+
match_condition(ref('string')->call('is_numeric', 'value'), ref('string')->cast(type_integer())),
3838
match_condition(ref('string')->endsWith('%'), ref('string')->strReplace('%', '')->cast(type_integer())),
3939
match_condition(ref('string')->startsWith('+'), ref('string')->strReplace('+', '')->cast(type_integer())),
4040
],

src/core/etl/src/Flow/ETL/DSL/functions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,8 +1373,7 @@ function count(?EntryReference $function = null) : Count
13731373
/**
13741374
* Calls a user-defined function with the given parameters.
13751375
*
1376-
* @param callable|ScalarFunction $callable
1377-
* @param array<mixed> $parameters
1376+
* @param array<string, mixed> $parameters
13781377
* @param null|Type<mixed> $return_type
13791378
*/
13801379
#[DocumentationDSL(module: Module::CORE, type: DSLType::SCALAR_FUNCTION)]

src/core/etl/src/Flow/ETL/Function/CallUserFunc.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ final class CallUserFunc extends ScalarFunctionChain
1717
private $callable;
1818

1919
/**
20-
* @param callable|ScalarFunction $callable
21-
* @param array<mixed> $parameters
20+
* @param array<string, mixed> $parameters
2221
* @param null|Type<mixed> $returnType
2322
*/
2423
public function __construct(ScalarFunction|callable $callable, private readonly array $parameters, private readonly ?Type $returnType = null)
@@ -37,6 +36,10 @@ public function eval(Row $row, FlowContext $context) : mixed
3736
$parameters = [];
3837

3938
foreach ($this->parameters as $key => $parameter) {
39+
if (!\is_string($key)) {
40+
return $context->functions()->invalidResult(new InvalidArgumentException('CallUserFunc requires a parameter names to be a string'));
41+
}
42+
4043
$parameters[$key] = (new Parameter($parameter))->eval($row, $context);
4144
}
4245

src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ public function binaryLength() : BinaryLength
152152
}
153153

154154
/**
155-
* @param array<array-key, mixed> $arguments
156-
* @param Type<mixed> $returnType
155+
* @param array<string, mixed> $arguments
156+
* @param null|Type<mixed> $returnType
157157
*/
158-
public function call(ScalarFunction|callable $callable, array $arguments = [], string|int $refAlias = 0, ?Type $returnType = null) : CallUserFunc
158+
public function call(ScalarFunction|callable $callable, string $refAlias, array $arguments = [], ?Type $returnType = null) : CallUserFunc
159159
{
160160
return new CallUserFunc($callable, array_merge($arguments, [$refAlias => $this]), $returnType);
161161
}

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/CallUserFuncTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function test_call() : void
2323
)
2424
->withEntry(
2525
'integers',
26-
ref('integers')->call(lit('explode'), ['separator' => ','], refAlias: 'string', returnType: type_list(type_integer()))
26+
ref('integers')->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','], returnType: type_list(type_integer()))
2727
)
2828
->write(to_memory($memory = new ArrayMemory()))
2929
->run();

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/MatchCasesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function test_case_match() : void
2727
match_cases(
2828
[
2929
match_condition(ref('string')->contains('-'), ref('string')->strReplace('-', ' ')),
30-
match_condition(ref('string')->call('is_numeric'), ref('string')->cast(type_integer())),
30+
match_condition(ref('string')->call('is_numeric', 'value'), ref('string')->cast(type_integer())),
3131
match_condition(ref('string')->endsWith('%'), ref('string')->strReplace('%', '')->cast(type_integer())),
3232
match_condition(ref('string')->startsWith('+'), ref('string')->strReplace('+', '')->cast(type_integer())),
3333
],

src/core/etl/tests/Flow/ETL/Tests/Unit/Function/CallUserFuncTest.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use function Flow\ETL\DSL\{call, flow_context, list_entry, lit, ref, row, string_entry};
88
use function Flow\Types\DSL\{type_integer, type_list};
99
use Flow\ETL\Function\ScalarFunction\ScalarResult;
10-
use Flow\ETL\Tests\FlowTestCase;
1110
use Flow\ETL\Tests\Unit\Function\Fixtures\CallUserFunc\StaticCalculator;
11+
use PHPUnit\Framework\TestCase;
1212

13-
final class CallUserFuncTest extends FlowTestCase
13+
final class CallUserFuncTest extends TestCase
1414
{
1515
public function test_call_user_func_as_dsl() : void
1616
{
@@ -26,7 +26,33 @@ public function test_call_user_func_with_native_function() : void
2626
self::assertSame(
2727
3,
2828
ref('list')
29-
->call(lit('count'))
29+
->call(lit('count'), 'value')
30+
->eval($row, flow_context())
31+
);
32+
}
33+
34+
public function test_call_user_func_with_non_callable_function() : void
35+
{
36+
$row = row(
37+
list_entry('list', [1, 2, 3], type_list(type_integer())),
38+
);
39+
40+
self::assertNull(
41+
ref('list')
42+
->call(lit('unknown'), 'whatever')
43+
->eval($row, flow_context())
44+
);
45+
}
46+
47+
public function test_call_user_func_with_non_string_argument_keys() : void
48+
{
49+
$row = row(
50+
list_entry('list', [1, 2, 3], type_list(type_integer())),
51+
);
52+
53+
self::assertNull(
54+
ref('list')
55+
->call(lit('explode'), refAlias: 'string', arguments: [','])
3056
->eval($row, flow_context())
3157
);
3258
}
@@ -42,7 +68,7 @@ public function test_call_user_func_with_object_method() : void
4268
self::assertSame(
4369
3,
4470
ref('list')
45-
->call(lit($calculator->count(...)))
71+
->call(lit($calculator->count(...)), 'array')
4672
->eval($row, flow_context())
4773
);
4874
}
@@ -56,7 +82,7 @@ public function test_call_user_func_with_ref_alias_and_optional_arguments() : vo
5682
self::assertSame(
5783
['1', '2', '3'],
5884
ref('item_ids')
59-
->call(lit('explode'), ['separator' => ','], refAlias: 'string')
85+
->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','])
6086
->eval($row, flow_context())
6187
);
6288
}
@@ -70,7 +96,7 @@ public function test_call_user_func_with_ref_alias_and_optional_arguments_and_re
7096
self::assertEquals(
7197
new ScalarResult([1, 2, 3], type_list(type_integer())),
7298
ref('item_ids')
73-
->call(lit('explode'), ['separator' => ','], refAlias: 'string', returnType: type_list(type_integer()))
99+
->call(lit('explode'), refAlias: 'string', arguments: ['separator' => ','], returnType: type_list(type_integer()))
74100
->eval($row, flow_context())
75101
);
76102
}
@@ -84,7 +110,7 @@ public function test_call_user_func_with_static_method() : void
84110
self::assertSame(
85111
3,
86112
ref('list')
87-
->call(lit(StaticCalculator::class . '::count'))
113+
->call(lit(StaticCalculator::class . '::count'), 'array')
88114
->eval($row, flow_context())
89115
);
90116
}

web/landing/resources/api.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)