From 703aad07bf18648eb8f01fcaee79b64d7416f3db Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Fri, 6 Nov 2026 14:14:36 +0100 Subject: [PATCH] #491 - CounterTask: reject flush_every 0, deprecate a negative value Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +------ UPGRADE.md | 7 ++++++ docs/reference/tasks/counter_task.md | 9 +++++--- src/Task/CounterTask.php | 20 ++++++++++++++++ tests/Task/CounterTaskTest.php | 34 ++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c173c57..612f1750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Latest * [#483](https://github.com/cleverage/process-bundle/issues/483) Documentation: an exception thrown by `IterableTaskInterface::next()` makes the process fail right away with this exception, the `error_strategy` and the `error_outputs` of the task do not apply. ## Fixes +* [#491](https://github.com/cleverage/process-bundle/issues/491) `CounterTask`: `flush_every: 0` is rejected when the options are resolved, with a clear message, instead of failing on the first execution with `Modulo by zero`; a negative `flush_every`, which works as its absolute value, is deprecated. Update documentation, add tests. * [#477](https://github.com/cleverage/process-bundle/issues/477) "Stop the process on the first invalid item" (`error_strategy: skip` and an error branch ending on a `StopTask`) ends as stopped when the failing task is finalizable (`csv_reader`, `xml_stream_reader`, `stat_counter`...): the error of the skipped item, kept by its state, was taken for a finalization failure and the process ended as failed. Add tests. * [#473](https://github.com/cleverage/process-bundle/issues/473) A task failing with the `stop` error strategy makes the process fail even when its error branch stops the flow (`StopTask`, `ProcessState::stop()`): the failure was swallowed, the process ended as succeeded or stopped (`cleverage_process.end` event, exit code 0) and a parent process went on with a `null` output. Update documentation, add tests. * [#485](https://github.com/cleverage/process-bundle/issues/485) The process returns the last output of its `end_point` passed on to the next tasks: the output of an execution whose output is ignored (skipped, filtered, failed with the `skip` strategy) was returned, e.g. `""` for a `skip_empty` end point whose last item is empty, or `null` for a filtered last item. Update documentation, add tests. @@ -397,7 +398,6 @@ v3.2.2 * `\CleverAge\ProcessBundle\Exception\InvalidProcessConfigurationException` now displays the failing process code * `\CleverAge\ProcessBundle\Transformer\TransformerTrait` now displays a more explicit message on wrong options type - v3.2.1 ------ @@ -418,8 +418,6 @@ v3.2.0 There is no BC break for this version, but note that `sidus/base-bundle` has been removed from dependencies. If you use it, it should already be inside your own composer.json. - - Release v3.1 ============ @@ -445,7 +443,6 @@ v3.1.5 * [GITHUB-120](https://github.com/cleverage/process-bundle/pull/120): FolderBrowserTask: Accept array type for `name_pattern` option - v3.1.4 ------ @@ -453,7 +450,6 @@ v3.1.4 * (_backport from v3.0.9_) Adding simple task to launch system commands - v3.1.3 ------ @@ -509,7 +505,6 @@ v3.1.0 * [GITHUB-106](https://github.com/cleverage/process-bundle/issues/106): an entry-point cannot have an ancestor anymore. The behaviour was undefined, and now it will throw an exception. - UPGRADE TO 2.0 ============== @@ -533,7 +528,6 @@ options: : ``` - UPGRADE TO 1.1 ============== diff --git a/UPGRADE.md b/UPGRADE.md index 5a5e30de..88ff5d5d 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -11,6 +11,13 @@ as succeeded or stopped. To stop the process on purpose on the first error (not on the failing task, as in the "Stop the process on the first invalid item" example of the [StopTask](docs/reference/tasks/stop_task.md). +### Counter task + +A negative `flush_every` option of the `counter` task works as its absolute value (`-3` as `3`): it is deprecated since +5.10 (`Since cleverage/process-bundle 5.10: A negative "flush_every" option of the counter task (-3) is deprecated...`) +and will be rejected in 6.0: use a positive integer. `flush_every: 0`, which failed on the first execution (`Modulo by +zero`), is now rejected when the options are resolved. + ## v5.8 ### Unknown task references diff --git a/docs/reference/tasks/counter_task.md b/docs/reference/tasks/counter_task.md index 268288d5..0c3485b4 100644 --- a/docs/reference/tasks/counter_task.md +++ b/docs/reference/tasks/counter_task.md @@ -25,9 +25,9 @@ Possible outputs Options ------- -| Code | Type | Required | Default | Description | -|---------------|-------|:--------:|---------|-------------------------------------------------------| -| `flush_every` | `int` | **X** | | Output the count every N executions (N = this option) | +| Code | Type | Required | Default | Description | +|---------------|-------|:--------:|---------|--------------------------------------------------------------------------------------| +| `flush_every` | `int` | **X** | | Output the count every N executions (N = this option, a positive integer, see notes) | Examples -------- @@ -55,3 +55,6 @@ Notes * `flush()` can be called several times during a process (see [Advanced workflow](../../04-advanced_workflow.md)), e.g. once at the end of the upstream iteration and once when the counter itself is resolved. The final count is only outputted by the first call; the next ones are skipped. +* `flush_every: 0` is rejected when the options are resolved (`InvalidOptionsException`) since 5.10: it failed on the + first execution (`Modulo by zero`). A negative value works as its absolute value (`-3` as `3`): it is deprecated since + 5.10 and will be rejected in v6.0. diff --git a/src/Task/CounterTask.php b/src/Task/CounterTask.php index 313ea676..cc6e709c 100644 --- a/src/Task/CounterTask.php +++ b/src/Task/CounterTask.php @@ -16,6 +16,8 @@ use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; use CleverAge\ProcessBundle\Model\FlushableTaskInterface; use CleverAge\ProcessBundle\Model\ProcessState; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; +use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -64,5 +66,23 @@ protected function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired(['flush_every']); $resolver->setAllowedTypes('flush_every', ['int']); + $resolver->setNormalizer('flush_every', static function (Options $options, int $value): int { + // Modulo by zero + if (0 === $value) { + throw new InvalidOptionsException('The option "flush_every" of the counter task must be a positive integer (the count is output every "flush_every" executions), 0 given.'); + } + // Works as its absolute value (sign of the modulo) + if ($value < 0) { + trigger_deprecation( + 'cleverage/process-bundle', + '5.10', + 'A negative "flush_every" option of the counter task (%d) is deprecated, it works as %d: use a positive integer. It will be rejected in v6.0.', + $value, + -$value, + ); + } + + return $value; + }); } } diff --git a/tests/Task/CounterTaskTest.php b/tests/Task/CounterTaskTest.php index 7e71d283..df8d99d6 100644 --- a/tests/Task/CounterTaskTest.php +++ b/tests/Task/CounterTaskTest.php @@ -21,6 +21,7 @@ use CleverAge\ProcessBundle\Model\ProcessState; use CleverAge\ProcessBundle\Task\CounterTask; use PHPUnit\Framework\TestCase; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; #[\PHPUnit\Framework\Attributes\CoversClass(CounterTask::class)] #[\PHPUnit\Framework\Attributes\UsesClass(AbstractConfigurableTask::class)] @@ -74,6 +75,39 @@ public function testFlushOutputsANewCountAfterNewExecutions(): void self::assertNull($this->flush($task, $state)); } + /** + * A modulo by zero failed when executed. + */ + public function testZeroFlushEveryIsRejected(): void + { + $this->expectException(InvalidOptionsException::class); + $this->expectExceptionMessage('The option "flush_every" of the counter task must be a positive integer (the count is output every "flush_every" executions), 0 given.'); + + $this->createTask(0); + } + + /** + * A negative value works as its absolute value (sign of the modulo): deprecated. + */ + public function testNegativeFlushEveryIsDeprecated(): void + { + $deprecations = []; + set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool { + $deprecations[] = $errstr; + + return true; + }, \E_USER_DEPRECATED); + try { + [$task, $state] = $this->createTask(-2); + } finally { + restore_error_handler(); + } + + self::assertSame(['Since cleverage/process-bundle 5.10: A negative "flush_every" option of the counter task (-2) is deprecated, it works as 2: use a positive integer. It will be rejected in v6.0.'], $deprecations); + self::assertSame([null, 2, null, 4, null], $this->executeAll($task, $state, 5)); + self::assertSame(5, $this->flush($task, $state)); + } + /** * @return array{CounterTask, ProcessState} */