From bbc59910da71b38e6d81c2fecc4018c13f32fa87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?= Date: Tue, 14 Nov 2023 15:37:06 +0100 Subject: [PATCH] Fixing PHPStan level 9 issues --- composer.lock | 8 +++--- src/Extractor/ArrayExtractor.php | 2 +- src/Extractor/IteratorExtractor.php | 2 +- src/GeneratorWrapper.php | 4 +-- src/Loader/DebugLoader.php | 2 +- src/Loader/JSONStreamLoader.php | 2 +- src/Loader/LogLoader.php | 17 +++++++++---- src/Loader/StderrLoader.php | 2 +- src/Loader/StdoutLoader.php | 2 +- src/Loader/StreamLoader.php | 17 +++++++++---- src/Pipeline.php | 12 ++++----- src/PipelineRunner.php | 6 ++--- src/Transformer/BatchingTransformer.php | 30 +++++++++++++++-------- src/Transformer/CallableTransformer.php | 23 ++++++++++------- src/Transformer/ColumnTrimTransformer.php | 21 ++++++++++------ src/Transformer/FilterTransformer.php | 13 +++++++--- src/UnexpectedYieldedValueType.php | 11 +++------ 17 files changed, 106 insertions(+), 68 deletions(-) diff --git a/composer.lock b/composer.lock index 4e311f3..d1d20e2 100644 --- a/composer.lock +++ b/composer.lock @@ -124,12 +124,12 @@ "source": { "type": "git", "url": "https://github.com/php-etl/pipeline-contracts.git", - "reference": "224cf14efe07df6ccd5d2b35eb7fba3bad7823c0" + "reference": "03375b74ce813cd6f91c274de60db8523a65f6f7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-etl/pipeline-contracts/zipball/224cf14efe07df6ccd5d2b35eb7fba3bad7823c0", - "reference": "224cf14efe07df6ccd5d2b35eb7fba3bad7823c0", + "url": "https://api.github.com/repos/php-etl/pipeline-contracts/zipball/03375b74ce813cd6f91c274de60db8523a65f6f7", + "reference": "03375b74ce813cd6f91c274de60db8523a65f6f7", "shasum": "" }, "require": { @@ -173,7 +173,7 @@ "issues": "https://github.com/php-etl/pipeline-contracts/issues", "source": "https://github.com/php-etl/pipeline-contracts/tree/main" }, - "time": "2023-11-14T13:28:21+00:00" + "time": "2023-11-14T14:14:40+00:00" }, { "name": "php-etl/satellite-contracts", diff --git a/src/Extractor/ArrayExtractor.php b/src/Extractor/ArrayExtractor.php index e34d088..47442a5 100644 --- a/src/Extractor/ArrayExtractor.php +++ b/src/Extractor/ArrayExtractor.php @@ -7,7 +7,7 @@ use Kiboko\Contract\Pipeline\ExtractorInterface; /** - * @template Type of non-empty-array|object + * @template Type * * @implements ExtractorInterface */ diff --git a/src/Extractor/IteratorExtractor.php b/src/Extractor/IteratorExtractor.php index 332a85c..0d92d72 100644 --- a/src/Extractor/IteratorExtractor.php +++ b/src/Extractor/IteratorExtractor.php @@ -7,7 +7,7 @@ use Kiboko\Contract\Pipeline\ExtractorInterface; /** - * @template Type of non-empty-array|object + * @template Type * * @implements ExtractorInterface */ diff --git a/src/GeneratorWrapper.php b/src/GeneratorWrapper.php index 1508d95..2edfe43 100644 --- a/src/GeneratorWrapper.php +++ b/src/GeneratorWrapper.php @@ -7,7 +7,7 @@ use Kiboko\Contract\Bucket\ResultBucketInterface; /** - * @template Type of non-empty-array|object + * @template Type */ class GeneratorWrapper { @@ -41,7 +41,7 @@ public function valid(\Iterator ...$iterators): bool /** * @param Type $value - * @param \Generator, ResultBucketInterface, Type, void> ...$generators + * @param \Generator, Type, void> ...$generators */ public function send($value, \Generator ...$generators): \Generator { diff --git a/src/Loader/DebugLoader.php b/src/Loader/DebugLoader.php index aa69550..59286f0 100644 --- a/src/Loader/DebugLoader.php +++ b/src/Loader/DebugLoader.php @@ -5,7 +5,7 @@ namespace Kiboko\Component\Pipeline\Loader; /** - * @template Type of non-empty-array|object + * @template Type * * @extends StreamLoader */ diff --git a/src/Loader/JSONStreamLoader.php b/src/Loader/JSONStreamLoader.php index 755614d..76b4c06 100644 --- a/src/Loader/JSONStreamLoader.php +++ b/src/Loader/JSONStreamLoader.php @@ -5,7 +5,7 @@ namespace Kiboko\Component\Pipeline\Loader; /** - * @template Type of non-empty-array|object + * @template Type * * @extends StreamLoader */ diff --git a/src/Loader/LogLoader.php b/src/Loader/LogLoader.php index a5f8700..8f3ecf5 100644 --- a/src/Loader/LogLoader.php +++ b/src/Loader/LogLoader.php @@ -6,12 +6,13 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; +use Kiboko\Contract\Bucket\ResultBucketInterface; use Kiboko\Contract\Pipeline\LoaderInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; /** - * @template Type of non-empty-array|object + * @template Type * * @implements LoaderInterface */ @@ -19,19 +20,25 @@ { public function __construct(private LoggerInterface $logger, private string $logLevel = LogLevel::DEBUG) {} - /** @return \Generator, AcceptanceResultBucket|EmptyResultBucket, Type|null, void> */ + /** @return \Generator, Type|null, void> */ public function load(): \Generator { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } $this->logger->log($this->logLevel, var_export($line, true)); - $line = yield new AcceptanceResultBucket($line); + /** @var AcceptanceResultBucket $bucket */ + $bucket = new AcceptanceResultBucket($line); + $line = yield $bucket; } } } diff --git a/src/Loader/StderrLoader.php b/src/Loader/StderrLoader.php index e11efbb..fa615d7 100644 --- a/src/Loader/StderrLoader.php +++ b/src/Loader/StderrLoader.php @@ -5,7 +5,7 @@ namespace Kiboko\Component\Pipeline\Loader; /** - * @template Type of non-empty-array|object + * @template Type * * @extends StreamLoader */ diff --git a/src/Loader/StdoutLoader.php b/src/Loader/StdoutLoader.php index b5caeb2..0f4b609 100644 --- a/src/Loader/StdoutLoader.php +++ b/src/Loader/StdoutLoader.php @@ -5,7 +5,7 @@ namespace Kiboko\Component\Pipeline\Loader; /** - * @template Type of non-empty-array|object + * @template Type * * @extends StreamLoader */ diff --git a/src/Loader/StreamLoader.php b/src/Loader/StreamLoader.php index 8bdca13..3439efd 100644 --- a/src/Loader/StreamLoader.php +++ b/src/Loader/StreamLoader.php @@ -6,10 +6,11 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; +use Kiboko\Contract\Bucket\ResultBucketInterface; use Kiboko\Contract\Pipeline\LoaderInterface; /** - * @template Type of non-empty-array|object + * @template Type * * @implements LoaderInterface */ @@ -28,19 +29,25 @@ public function __construct($stream) $this->stream = $stream; } - /** @return \Generator, AcceptanceResultBucket|EmptyResultBucket, Type|null, void> */ + /** @return \Generator, Type|null, void> */ public function load(): \Generator { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } fwrite($this->stream, $this->formatLine($line)); - $line = yield new AcceptanceResultBucket($line); + /** @var AcceptanceResultBucket $bucket */ + $bucket = new AcceptanceResultBucket($line); + $line = yield $bucket; } } diff --git a/src/Pipeline.php b/src/Pipeline.php index d823fe7..4359cf5 100644 --- a/src/Pipeline.php +++ b/src/Pipeline.php @@ -39,7 +39,7 @@ public function __construct( } /** - * @template InputType of non-empty-array|object + * @template InputType * * @param InputType ...$data */ @@ -58,7 +58,7 @@ private function passThroughCoroutine(): \Generator } /** - * @template Type of non-empty-array|object + * @template Type * * @param ExtractorInterface $extractor * @param StepRejectionInterface $rejection @@ -105,8 +105,8 @@ public function extract( } /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType + * @template OutputType * * @param TransformerInterface $transformer * @param StepRejectionInterface $rejection @@ -154,8 +154,8 @@ public function transform( } /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType + * @template OutputType * * @param LoaderInterface $loader * @param StepRejectionInterface $rejection diff --git a/src/PipelineRunner.php b/src/PipelineRunner.php index 283ce21..3787f78 100644 --- a/src/PipelineRunner.php +++ b/src/PipelineRunner.php @@ -22,11 +22,11 @@ public function __construct( ) {} /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType + * @template OutputType * * @param \Iterator, InputType|null> $source - * @param \Generator, ResultBucketInterface|AcceptanceResultBucketInterface|RejectionResultBucketInterface|null, InputType, void> $coroutine + * @param \Generator|AcceptanceResultBucketInterface|RejectionResultBucketInterface|null, InputType, void> $coroutine * @param StepRejectionInterface $rejection * * @return \Iterator, ResultBucketInterface> diff --git a/src/Transformer/BatchingTransformer.php b/src/Transformer/BatchingTransformer.php index 7764ddd..eeb010b 100644 --- a/src/Transformer/BatchingTransformer.php +++ b/src/Transformer/BatchingTransformer.php @@ -11,8 +11,8 @@ use Kiboko\Contract\Pipeline\TransformerInterface; /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array + * @template InputType + * @template OutputType of list * * @implements TransformerInterface * @implements FlushableInterface @@ -29,36 +29,46 @@ public function __construct( private readonly int $batchSize ) {} - /** @return \Generator, ResultBucketInterface|EmptyResultBucket, InputType|null, void> */ + /** @return \Generator, InputType|null, void> */ public function transform(): \Generator { $this->batch = []; - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } $this->batch[] = $line; if (\count($this->batch) >= $this->batchSize) { - /** @phpstan-ignore-next-line */ - $line = yield new AcceptanceResultBucket($this->batch); + /** @var EmptyResultBucket $bucket */ + $bucket = new AcceptanceResultBucket($this->batch); + $line = yield $bucket; + $this->batch = []; continue; } - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; } } - /** @return AcceptanceResultBucket|EmptyResultBucket */ + /** @return ResultBucketInterface */ public function flush(): ResultBucketInterface { if (\count($this->batch) <= 0) { - return new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + return $bucket; } /* @phpstan-ignore-next-line */ diff --git a/src/Transformer/CallableTransformer.php b/src/Transformer/CallableTransformer.php index fc78bd6..208da11 100644 --- a/src/Transformer/CallableTransformer.php +++ b/src/Transformer/CallableTransformer.php @@ -6,11 +6,12 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; +use Kiboko\Contract\Bucket\ResultBucketInterface; use Kiboko\Contract\Pipeline\TransformerInterface; /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType + * @template OutputType * * @template-implements TransformerInterface */ @@ -20,7 +21,7 @@ class CallableTransformer implements TransformerInterface private $callback; /** - * @param callable(InputType $item): OutputType $callback + * @param callable(InputType|null $item): OutputType $callback */ public function __construct( callable $callback, @@ -28,22 +29,26 @@ public function __construct( $this->callback = $callback; } - /** - * @return \Generator, AcceptanceResultBucket|EmptyResultBucket, InputType|null, void> - */ + /** @return \Generator, InputType|null, void> */ public function transform(): \Generator { $callback = $this->callback; - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } - $line = yield new AcceptanceResultBucket($callback($line)); + /** @var AcceptanceResultBucket $bucket */ + $bucket = new AcceptanceResultBucket($callback($line)); + $line = yield $bucket; } } } diff --git a/src/Transformer/ColumnTrimTransformer.php b/src/Transformer/ColumnTrimTransformer.php index 1abe117..5266711 100644 --- a/src/Transformer/ColumnTrimTransformer.php +++ b/src/Transformer/ColumnTrimTransformer.php @@ -6,11 +6,12 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; +use Kiboko\Contract\Bucket\ResultBucketInterface; use Kiboko\Contract\Pipeline\TransformerInterface; /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType of array + * @template OutputType of array * * @implements TransformerInterface */ @@ -21,14 +22,18 @@ public function __construct( private readonly array $columnsToTrim ) {} - /** @return \Generator, AcceptanceResultBucket|EmptyResultBucket, InputType|null, void> */ + /** @return \Generator, InputType|null, void> */ public function transform(): \Generator { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } foreach ($this->columnsToTrim as $column) { @@ -38,8 +43,10 @@ public function transform(): \Generator $line[$column] = trim((string) $line[$column]); } - /** @phpstan-ignore-next-line */ - $line = yield new AcceptanceResultBucket($line); + + /** @var AcceptanceResultBucket $bucket */ + $bucket = new AcceptanceResultBucket($line); + $line = yield $bucket; } } } diff --git a/src/Transformer/FilterTransformer.php b/src/Transformer/FilterTransformer.php index c39174c..899c6d3 100644 --- a/src/Transformer/FilterTransformer.php +++ b/src/Transformer/FilterTransformer.php @@ -6,10 +6,11 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; +use Kiboko\Contract\Bucket\ResultBucketInterface; use Kiboko\Contract\Pipeline\TransformerInterface; /** - * @template Type of non-empty-array|object + * @template Type * * @implements TransformerInterface */ @@ -26,17 +27,21 @@ public function __construct( } /** - * @return \Generator, AcceptanceResultBucket|EmptyResultBucket, Type|null, void> + * @return \Generator, Type|null, void> */ public function transform(): \Generator { $callback = $this->callback; - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; /* @phpstan-ignore-next-line */ while (true) { if (null === $line || !$callback($line)) { - $line = yield new EmptyResultBucket(); + /** @var EmptyResultBucket $bucket */ + $bucket = new EmptyResultBucket(); + $line = yield $bucket; continue; } diff --git a/src/UnexpectedYieldedValueType.php b/src/UnexpectedYieldedValueType.php index acb83c8..286b829 100644 --- a/src/UnexpectedYieldedValueType.php +++ b/src/UnexpectedYieldedValueType.php @@ -7,8 +7,8 @@ use Kiboko\Contract\Bucket\ResultBucketInterface; /** - * @template InputType of non-empty-array|object - * @template OutputType of non-empty-array|object + * @template InputType + * @template OutputType */ final class UnexpectedYieldedValueType extends \UnexpectedValueException { @@ -27,12 +27,11 @@ public function getCoroutine(): \Generator } /** - * @param \Generator, ResultBucketInterface, InputType, void> $actual - * @param list $expectedTypes + * @param list $expectedTypes * * @return UnexpectedYieldedValueType */ - public static function expectingTypes(\Generator $coroutine, array $expectedTypes, $actual, int $code = 0, \Throwable $previous = null): self + public static function expectingTypes(\Generator $coroutine, array $expectedTypes, mixed $actual, int $code = 0, \Throwable $previous = null): self { try { $re = new \ReflectionGenerator($coroutine); @@ -47,7 +46,6 @@ public static function expectingTypes(\Generator $coroutine, array $expectedType $executionFile = $re->getExecutingFile(); $executionLine = $re->getExecutingLine(); - /* @phpstan-ignore-next-line */ return new self( $coroutine, strtr( @@ -64,7 +62,6 @@ public static function expectingTypes(\Generator $coroutine, array $expectedType $previous ); } catch (\ReflectionException) { - /* @phpstan-ignore-next-line */ return new self( $coroutine, strtr(