Description
ProcessState::addErrorContextValue() only accepts string|int|array values. When a task passes any other value (null, bool, float, object), a \TypeError is thrown and the real behaviour or error is lost:
- Iterable tasks on an empty iterable (
ConstantIterableOutputTask, InputIteratorTask, YamlReaderTask with {}/[], SplitJoinLineTask with no line to produce): AbstractIterableOutputTask::execute() always adds iterator_key before checking the iterator. On an empty iterator key() is null, so the process fails instead of skipping the task, as documented.
- PropertySetterTask: when a value cannot be set, the task adds the value to the error context. If that value is a
bool, float, null or object, the TypeError replaces the original exception (e.g. NoSuchPropertyException).
Reproduction
clever_age_process:
configurations:
demo.error_context.empty_iterable:
tasks:
iterate:
service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask'
options:
output: []
outputs: [debug]
debug:
service: '@CleverAge\ProcessBundle\Task\Debug\DebugTask'
demo.error_context.property_setter:
tasks:
entry:
service: '@CleverAge\ProcessBundle\Task\ConstantOutputTask'
options:
output:
name: Foo
outputs: [set]
set:
service: '@CleverAge\ProcessBundle\Task\PropertySetterTask'
options:
values:
enabled: true # property path on an array: cannot be written
outputs: [debug]
debug:
service: '@CleverAge\ProcessBundle\Task\Debug\DebugTask'
$ bin/console cleverage:process:execute demo.error_context.empty_iterable
CRITICAL [cleverage_process_task] CleverAge\ProcessBundle\Model\ProcessState::addErrorContextValue(): Argument #2 ($value) must be of type array|string|int, null given, called in /var/www/vendor/cleverage/process-bundle/src/Task/AbstractIterableOutputTask.php on line 33
$ bin/console cleverage:process:execute demo.error_context.property_setter
CRITICAL [cleverage_process_task] CleverAge\ProcessBundle\Model\ProcessState::addErrorContextValue(): Argument #2 ($value) must be of type array|string|int, true given, called in /var/www/vendor/cleverage/process-bundle/src/Task/PropertySetterTask.php on line 42 ["property" => "enabled"]
Expected: the first process succeeds with no output, the second one reports Cannot write property "enabled" to an array. Maybe you should write the property path as "[enabled]" instead?. Tested on main (c1a2804), PHP 8.5, Symfony 7.4.
Cause
Proposed fix
- Widen the
$value parameter of ProcessState::addErrorContextValue() to mixed.
- In
AbstractIterableOutputTask::execute(), add iterator_key only when the iterator is valid: an empty iterable then produces no output and the task is skipped.
- Update the reference documentation (
property_setter_task.md, yaml_reader_task.md, split_join_line_task.md, 03-custom_tasks.md), add unit tests.
Requirements
Breaking changes
None for callers. Widening a parameter type is backward compatible, except for a subclass of ProcessState overriding addErrorContextValue() with the old signature (it must widen $value to mixed).
Description
ProcessState::addErrorContextValue()only acceptsstring|int|arrayvalues. When a task passes any other value (null,bool,float, object), a\TypeErroris thrown and the real behaviour or error is lost:ConstantIterableOutputTask,InputIteratorTask,YamlReaderTaskwith{}/[],SplitJoinLineTaskwith no line to produce):AbstractIterableOutputTask::execute()always addsiterator_keybefore checking the iterator. On an empty iteratorkey()isnull, so the process fails instead of skipping the task, as documented.bool,float,nullor object, theTypeErrorreplaces the original exception (e.g.NoSuchPropertyException).Reproduction
Expected: the first process succeeds with no output, the second one reports
Cannot write property "enabled" to an array. Maybe you should write the property path as "[enabled]" instead?. Tested onmain(c1a2804), PHP 8.5, Symfony 7.4.Cause
ProcessState::addErrorContextValue()types$valueasstring|int|array, whilesetErrorContext(array)accepts any value, and the context is only passed to the PSR-3 logger, which accepts anything.AbstractIterableOutputTask::execute()addsiterator_keybeforevalid()is checked.PropertySetterTask::execute()adds the configured value, which can be of any type.Proposed fix
$valueparameter ofProcessState::addErrorContextValue()tomixed.AbstractIterableOutputTask::execute(), additerator_keyonly when the iterator is valid: an empty iterable then produces no output and the task is skipped.property_setter_task.md,yaml_reader_task.md,split_join_line_task.md,03-custom_tasks.md), add unit tests.Requirements
Breaking changes
None for callers. Widening a parameter type is backward compatible, except for a subclass of
ProcessStateoverridingaddErrorContextValue()with the old signature (it must widen$valuetomixed).