Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the writer Management + ran php-cs-fixer #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CSV/FingersCrossed/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

readonly class Extractor implements ExtractorInterface

Check failure on line 15 in src/CSV/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 15 in src/CSV/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 15 in src/CSV/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType
{
public function __construct(
private ReaderInterface $reader,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

readonly class Loader implements LoaderInterface, FlushableInterface

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType
{
public function __construct(
private WriterInterface $writer,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function load(): \Generator
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/Safe/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

readonly class Extractor implements ExtractorInterface

Check failure on line 15 in src/CSV/Safe/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 15 in src/CSV/Safe/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 15 in src/CSV/Safe/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType
{
public function __construct(
private ReaderInterface $reader,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

readonly class Loader implements LoaderInterface, FlushableInterface

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\FlushableInterface but does not specify its types: Type

Check failure on line 21 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader implements generic interface Kiboko\Contract\Pipeline\LoaderInterface but does not specify its types: InputType, OutputType
{
public function __construct(
private WriterInterface $writer,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function load(): \Generator
{
Expand Down Expand Up @@ -60,7 +61,7 @@
}
}

private function orderColumns(array $headers, array $line): Row

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $headers with no value type specified in iterable type array.

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $line with no value type specified in iterable type array.

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $headers with no value type specified in iterable type array.

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $line with no value type specified in iterable type array.

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $headers with no value type specified in iterable type array.

Check failure on line 64 in src/CSV/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Component\Flow\Spreadsheet\CSV\Safe\Loader::orderColumns() has parameter $line with no value type specified in iterable type array.
{
$result = [];
foreach ($headers as $cell) {
Expand Down
3 changes: 2 additions & 1 deletion src/Sheet/FingersCrossed/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

readonly class Extractor implements ExtractorInterface

Check failure on line 17 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Class Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 17 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Class Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType

Check failure on line 17 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Class Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor implements generic interface Kiboko\Contract\Pipeline\ExtractorInterface but does not specify its types: OutputType
{
public function __construct(
private ReaderInterface $reader,
private string $sheetName,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down Expand Up @@ -59,7 +60,7 @@
}
}

public function flush(): ResultBucketInterface

Check failure on line 63 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-7

Method Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor::flush() return type with generic interface Kiboko\Contract\Bucket\ResultBucketInterface does not specify its types: Type

Check failure on line 63 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-6

Method Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor::flush() return type with generic interface Kiboko\Contract\Bucket\ResultBucketInterface does not specify its types: Type

Check failure on line 63 in src/Sheet/FingersCrossed/Extractor.php

View workflow job for this annotation

GitHub Actions / phpstan-8

Method Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed\Extractor::flush() return type with generic interface Kiboko\Contract\Bucket\ResultBucketInterface does not specify its types: Type
{
$this->reader->close();

Expand Down
14 changes: 1 addition & 13 deletions src/Sheet/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@
use Kiboko\Component\Bucket\AcceptanceResultBucket;
use Kiboko\Component\Bucket\EmptyResultBucket;
use Kiboko\Component\Bucket\RejectionResultBucket;
use Kiboko\Contract\Bucket\ResultBucketInterface;
use Kiboko\Contract\Pipeline\FlushableInterface;
use Kiboko\Contract\Pipeline\LoaderInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final readonly class Loader implements LoaderInterface, FlushableInterface
final readonly class Loader implements LoaderInterface
{
public function __construct(
private WriterInterface $writer,
private string $sheetName,
private LoggerInterface $logger = new NullLogger()
) {
/* @phpstan-ignore-next-line */
$this->writer->getCurrentSheet()->setName($this->sheetName);
}

public function load(): \Generator
Expand Down Expand Up @@ -65,11 +60,4 @@ public function load(): \Generator
$line = yield new AcceptanceResultBucket($line);
}
}

public function flush(): ResultBucketInterface
{
$this->writer->close();

return new EmptyResultBucket();
}
}
3 changes: 2 additions & 1 deletion src/Sheet/Safe/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function __construct(
private string $sheetName,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
36 changes: 6 additions & 30 deletions src/Sheet/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,37 @@
use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\WriterInterface;
use Kiboko\Component\Bucket\AcceptanceResultBucket;
use Kiboko\Component\Bucket\EmptyResultBucket;
use Kiboko\Component\Bucket\RejectionResultBucket;
use Kiboko\Contract\Bucket\ResultBucketInterface;
use Kiboko\Contract\Pipeline\FlushableInterface;
use Kiboko\Contract\Pipeline\LoaderInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final readonly class Loader implements LoaderInterface, FlushableInterface
final readonly class Loader implements LoaderInterface
{
public function __construct(
private WriterInterface $writer,
private string $sheetName,
private LoggerInterface $logger = new NullLogger()
) {
/* @phpstan-ignore-next-line */
$this->writer->getCurrentSheet()->setName($this->sheetName);
}

public function load(): \Generator
{
$line = yield new EmptyResultBucket();
$line = yield;

Check failure on line 27 in src/Sheet/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.
$headers = array_keys($line);
try {
$this->writer->addRow(
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
);
} catch (IOException|WriterNotOpenedException $exception) {
} catch (WriterNotOpenedException|IOException $exception) {
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
$line = yield new RejectionResultBucket(
'Impossible to load data to the given CSV file.',
$exception,
$line
);

return;
}

/* @phpstan-ignore-next-line */
while (true) {

Check failure on line 39 in src/Sheet/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.
try {
$this->writer->addRow($this->orderColumns($headers, $line));
} catch (IOException|WriterNotOpenedException $exception) {
} catch (WriterNotOpenedException|IOException $exception) {
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
$line = yield new RejectionResultBucket(
'Impossible to load data to the given CSV file.',
$exception,
$line
);
continue;
}

$line = yield new AcceptanceResultBucket($line);
Expand All @@ -73,11 +56,4 @@

return new Row($result, null);
}

public function flush(): ResultBucketInterface
{
$this->writer->close();

return new EmptyResultBucket();
}
}
Loading