diff --git a/CHANGELOG.md b/CHANGELOG.md index 57ca432a..c9fdf131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Latest ## BC breaks * [#428](https://github.com/cleverage/process-bundle/issues/428) Drop the support of PHP 8.2 and Symfony 6.4: PHP 8.3 or later and Symfony 7.4 or 8.x are required, `symfony/polyfill-php83` is no longer required, `symfony/event-dispatcher-contracts` requires 3.4.2 or later (the previous versions trigger the PHP 8.4 "Implicitly marking parameter as nullable" deprecation). Update documentation. * [#428](https://github.com/cleverage/process-bundle/issues/428) The class constants are typed: a class redeclaring a protected constant of the bundle must use the same type (see `UPGRADE.md`). +* [#143](https://github.com/cleverage/process-bundle/issues/143) Narrow native types that were never `null` in practice: `AbstractConfigurableTask::getOptions()` and `ProcessState::getContextualizedOptions()` return `array` (instead of `?array`), `CsvResource::$lineNumber` is an `int` and `ProcessHistory::$startDate` a `\DateTimeInterface` (instead of nullable types). Overrides must declare the narrowed types (see `UPGRADE.md`). Improve PHPStan level from 7 to 8. ## Changes * [#428](https://github.com/cleverage/process-bundle/issues/428) Tests on PHPUnit 12.5 and 13 (PHPUnit 10 and 11 dropped), with the lowest dependencies on PHP 8.3 and 8.5. diff --git a/UPGRADE.md b/UPGRADE.md index e0abf094..8fcec04b 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -18,6 +18,16 @@ protected constants must declare it with the same type: `ProcessLauncherTask::STOP_TIMEOUT` (`protected const int INDENT_SIZE = 2;`); - `array`: `ArrayMergeTask::MERGE_FUNC`, `Configuration::LOG_LEVELS`. +### Narrowed native types + +Native types that were never `null` in practice no longer accept it. A class extending a class of the bundle and +overriding one of these members must declare the narrowed type (a nullable type is a fatal error): + +- `AbstractConfigurableTask::getOptions()` and `ProcessState::getContextualizedOptions()` return `array` (instead of + `?array`): `protected function getOptions(ProcessState $state): array`; +- `CsvResource::$lineNumber` is an `int` (instead of `?int`), `ProcessHistory::$startDate` a `\DateTimeInterface` + (instead of `?\DateTimeInterface`). + ## v5.10 ### Stop strategy and error branch ending on a stop diff --git a/docs/03-custom_tasks.md b/docs/03-custom_tasks.md index 88e0e0fe..2e537cb4 100644 --- a/docs/03-custom_tasks.md +++ b/docs/03-custom_tasks.md @@ -151,9 +151,10 @@ available in `CleverAge\ProcessBundle\Model\AbstractConfigurableTask`. Based on [Symfony's OptionsResolver Component](https://symfony.com/doc/current/components/options_resolver.html) this abstract class allows you to implement its `configureOptions` method to add your requirements, default values and normalizers. Options are resolved (once) during the task initialization, and can be read with `getOptions($state)` or -`getOption($state, $code)`. If the resolution fails during initialization, the error is logged and the options are -resolved again (failing the process) when the task is first executed. Going on with the process after an -initialization failure is deprecated since 5.1: in v6.0, the process will fail before any task is executed (see +`getOption($state, $code)`. If you override `getOptions()`, it must return an `array` (it was typed `?array` before +v6.0). If the resolution fails during initialization, the error is logged and the options are resolved again (failing +the process) when the task is first executed. Going on with the process after an initialization failure is deprecated +since 5.1: in v6.0, the process will fail before any task is executed (see [initializable tasks](02-task_types.md#initializable-tasks)). ```php diff --git a/phpstan.neon b/phpstan.neon index 4fd6dc99..7ecee695 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 7 + level: 8 paths: - src - tests diff --git a/src/Filesystem/CsvResource.php b/src/Filesystem/CsvResource.php index b945ee49..9d5ccbd5 100644 --- a/src/Filesystem/CsvResource.php +++ b/src/Filesystem/CsvResource.php @@ -38,7 +38,7 @@ class CsvResource implements WritableStructuredFileInterface, SeekableFileInterf protected int $headerCount; - protected ?int $lineNumber = 1; + protected int $lineNumber = 1; protected bool $closed = false; diff --git a/src/Model/AbstractConfigurableTask.php b/src/Model/AbstractConfigurableTask.php index 630bd3ec..ca4e318f 100644 --- a/src/Model/AbstractConfigurableTask.php +++ b/src/Model/AbstractConfigurableTask.php @@ -41,9 +41,9 @@ public function reset(): void } /** - * @return array|null + * @return array */ - protected function getOptions(ProcessState $state): ?array + protected function getOptions(ProcessState $state): array { if (null === $this->options) { $resolver = new OptionsResolver(); diff --git a/src/Model/ProcessHistory.php b/src/Model/ProcessHistory.php index aafdfb50..e7afa12e 100644 --- a/src/Model/ProcessHistory.php +++ b/src/Model/ProcessHistory.php @@ -34,7 +34,7 @@ class ProcessHistory implements \Stringable protected string $processCode; - protected ?\DateTimeInterface $startDate; + protected \DateTimeInterface $startDate; protected ?\DateTimeInterface $endDate = null; diff --git a/src/Model/ProcessState.php b/src/Model/ProcessState.php index d6320651..90af81bb 100644 --- a/src/Model/ProcessState.php +++ b/src/Model/ProcessState.php @@ -335,10 +335,9 @@ public function setContext(array $context): void } /** - * @return array|null + * @return array */ - // @phpstan-ignore return.unusedType (narrowing the return type of a non-final class breaks the child classes, see #279) - public function getContextualizedOptions(): ?array + public function getContextualizedOptions(): array { if (!$this->contextualizedOptions) { $options = $this->getTaskConfiguration() diff --git a/src/Transformer/GenericTransformer.php b/src/Transformer/GenericTransformer.php index d2c5af38..a1bd4314 100644 --- a/src/Transformer/GenericTransformer.php +++ b/src/Transformer/GenericTransformer.php @@ -72,7 +72,7 @@ public function initialize(string $code, array $options = []): void $initialOptions = $resolver->resolve($options); $this->contextualOptions = $initialOptions['contextual_options']; - $this->deprecatedContextualOptions = DeprecatedCodeAliases::create(array_keys($this->contextualOptions)); + $this->deprecatedContextualOptions = DeprecatedCodeAliases::create(array_keys($this->contextualOptions ?? [])); $transformers = $initialOptions['transformers']; if (\is_array($transformers)) { $transformers = $this->replaceFormerTransformerCodes($transformers); diff --git a/tests/Task/TransformerTaskTest.php b/tests/Task/TransformerTaskTest.php index 7e867ec2..9c72c0dd 100644 --- a/tests/Task/TransformerTaskTest.php +++ b/tests/Task/TransformerTaskTest.php @@ -228,6 +228,8 @@ private function execute(array $options, mixed $input): ProcessState 'trim' => static fn (): TrimTransformer => new TrimTransformer(), // The mapping transformer uses the registry itself 'mapping' => static function () use (&$registry): MappingTransformer { + \assert($registry instanceof TransformerRegistry); + return new MappingTransformer($registry, new NullLogger(), PropertyAccess::createPropertyAccessor()); }, ]));