Skip to content

Commit ba31efc

Browse files
Merge pull request #233 from cleverage/232
#232 - Fix edge cases looping forever or failing with a TypeError
2 parents 6ebfb14 + 7744655 commit ba31efc

16 files changed

Lines changed: 114 additions & 12 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Latest
2525
* [#221](https://github.com/cleverage/process-bundle/issues/221) Fix CsvSplitterTask: each produced file contains `max_lines` data lines (instead of `max_lines - 2`), no infinite loop with `max_lines` <= 2 (`max_lines` must now be an integer greater than 0), no header-only file at the end. Update documentation, add tests.
2626
* [#223](https://github.com/cleverage/process-bundle/issues/223) Fix CounterTask: the final count is outputted once, as `flush()` may be called several times. Document that `flush()` implementations must be idempotent. Update documentation, add tests.
2727
* [#228](https://github.com/cleverage/process-bundle/issues/228) Add the missing `setAllowedTypes()` on HashTransformer (`raw_output`), SimpleBatchTask (`batch_count`), ConditionTrait (`empty`, `not_empty`), NormalizerTask / SerializerTask / DeserializerTask (`context`) and ObjectUpdaterTask (`property_path`): a wrong type is now reported when the options are resolved. Add tests.
28+
* [#232](https://github.com/cleverage/process-bundle/issues/232) Fix edge cases looping forever or failing with a `TypeError`: validate FileSplitterTask `max_lines` (greater than 0, also when given as input), catch any `\Throwable` in PropertySetterTask, type SlugifyTransformer options, explicit exceptions in InputFolderBrowserTask (no folder path) and `XmlFile::write()` (`saveXML()` failure). Update documentation, add tests.
2829

2930
## Deprecated
3031
* [#189](https://github.com/cleverage/process-bundle/issues/189) EventDispatcherTask: when `event_name` is set, listening to `CleverAge\ProcessBundle\Event\EventDispatcherTaskEvent` is deprecated (the event is still dispatched under its class name, with an `E_USER_DEPRECATED` error, if it has listeners). Listen to the configured `event_name` instead: the BC layer will be removed in v6.0.

‎docs/reference/tasks/file_splitter_task.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ next `max_lines` lines of the source file (the last file may contain fewer lines
2525
Options
2626
-------
2727

28-
| Code | Type | Required | Default | Description |
29-
|-------------|----------|:--------:|---------|-------------------------------------------|
30-
| `file_path` | `string` | **X** | | Path of the file to split |
31-
| `max_lines` | `int` | | `1000` | Maximum number of lines per produced file |
28+
| Code | Type | Required | Default | Description |
29+
|-------------|----------|:--------:|---------|-------------------------------------------------------------------|
30+
| `file_path` | `string` | **X** | | Path of the file to split |
31+
| `max_lines` | `int` | | `1000` | Maximum number of lines per produced file, must be greater than 0 |
3232

3333
Examples
3434
--------
@@ -48,7 +48,7 @@ read_chunk:
4848
Notes
4949
-----
5050
51-
* Values given as input are merged after option resolution, so they are not validated.
51+
* Values given as input (`file_path`, `max_lines`) are validated like the options; other input keys are ignored.
5252
* Every line of the source file is kept, in order, including empty lines. Line content is preserved, but each line
5353
break (`\n` or `\r\n`) is written as `PHP_EOL`, and a missing line break on the last line is added.
5454
* An empty source file produces no output (the task is skipped).

‎docs/reference/tasks/input_folder_browser_task.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ directory.
1919

2020
The folder path is released once its files have all been iterated, so each input (the same path again or a different
2121
one) is browsed from the start. Receiving a different folder path while an iteration is in progress throws a
22-
`\LogicException`.
22+
`\LogicException`, and an empty input (`null`, `''`) when no folder is being browsed throws an
23+
`\UnexpectedValueException`.
2324

2425
Possible outputs
2526
----------------

‎docs/reference/tasks/property_setter_task.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Possible outputs
2121

2222
The input, with the configured values set.
2323

24-
If a value cannot be set, the exception is set on the state (with `property` and `value` added to the error context)
24+
If a value cannot be set (including a `\TypeError` of the property accessor, e.g. on a scalar input), the exception
25+
is set on the state (with `property` and `value` added to the error context)
2526
and handled according to the task `error_strategy`; the remaining values are not set.
2627

2728
Options

‎docs/reference/tasks/xml_writer_task.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Notes
4343
-----
4444
4545
* The file is opened on each execution: with the default `wb` mode, each input overwrites the file.
46+
* If the XML content cannot be generated (`\DOMDocument::saveXML()` fails) or written, a `\RuntimeException` is thrown.

‎docs/reference/transformers/slugify_transformer.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ slugify:
5555
Notes
5656
-----
5757
58-
An invalid `transliterator` identifier (rejected by `\Transliterator::create()`) raises an
59-
`InvalidOptionsException` when the options are resolved, i.e. when the transformer is configured, not on the first
60-
transformed value.
58+
An invalid `transliterator` identifier (rejected by `\Transliterator::create()`), or an option that is not a string,
59+
raises an `InvalidOptionsException` when the options are resolved, i.e. when the transformer is configured, not on the
60+
first transformed value.

‎src/Filesystem/XmlFile.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function read(): \DOMDocument
6767
public function write(\DOMDocument $dom): void
6868
{
6969
$content = $dom->saveXML();
70+
if (false === $content) {
71+
throw new \RuntimeException('Could not generate the XML content');
72+
}
7073
$result = $this->file->fwrite($content);
7174

7275
if (false === $result) {

‎src/Task/File/FileSplitterTask.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected function configureOptions(OptionsResolver $resolver): void
9898
'max_lines' => 1000,
9999
]);
100100
$resolver->setAllowedTypes('max_lines', ['int']);
101+
$resolver->setAllowedValues('max_lines', static fn (int $value): bool => $value >= 1);
101102
}
102103

103104
/**
@@ -115,7 +116,11 @@ protected function getMergedOptions(ProcessState $state): array
115116
}
116117
// @var array<mixed> $input
117118

118-
return array_merge($options, $input);
119+
// Options given as input are validated like the task options
120+
$resolver = new OptionsResolver();
121+
$this->configureOptions($resolver);
122+
123+
return $resolver->resolve(array_merge($options, array_intersect_key($input, $options)));
119124
}
120125

121126
private function stripLineBreak(string $line): string

‎src/Task/File/InputFolderBrowserTask.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ protected function getOptions(ProcessState $state): array
7373
$this->folderPath = $folderPath;
7474
}
7575

76+
if (null === $this->folderPath) {
77+
throw new \UnexpectedValueException('No folder path given as input');
78+
}
7679
if (!is_dir($this->folderPath)) {
7780
throw new InvalidConfigurationException("Folder path does not exists or is not a folder: '{$this->folderPath}'");
7881
}

‎src/Task/PropertySetterTask.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function execute(ProcessState $state): void
3737
foreach ($options['values'] as $key => $value) {
3838
try {
3939
$this->accessor->setValue($input, $key, $value);
40-
} catch (\Exception $e) {
40+
} catch (\Throwable $e) {
4141
$state->addErrorContextValue('property', $key);
4242
$state->addErrorContextValue('value', $value);
4343
$state->setException($e);

0 commit comments

Comments
 (0)