Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 23, 2023
1 parent 5449135 commit ab1d88b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -105,4 +105,4 @@ jobs:
run: "./tools/composer update --no-ansi --no-interaction --no-progress"

- name: "Run mutation tests with Infection"
run: "./tools/infection"
run: "./tools/infection --only-covered"
8 changes: 7 additions & 1 deletion .psalm/baseline.xml
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.8.0@9cf4f60a333f779ad3bc704a555920e81d4fdcda"/>
<files psalm-version="5.8.0@9cf4f60a333f779ad3bc704a555920e81d4fdcda">
<file src="src/Parser.php">
<ArgumentTypeCoercion>
<code>$line</code>
</ArgumentTypeCoercion>
</file>
</files>
40 changes: 24 additions & 16 deletions src/Parser.php
Expand Up @@ -9,10 +9,10 @@
*/
namespace SebastianBergmann\CsvParser;

use function array_shift;
use function file;
use function str_getcsv;
use function is_array;
use Generator;
use RuntimeException;
use SplFileObject;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for this library
Expand All @@ -27,31 +27,39 @@ final class Parser
*/
public function parse(string $filename, Schema $schema, bool $ignoreFirstLine = true): Generator
{
$lines = @file($filename);

if ($lines === false) {
throw CannotReadCsvFileException::from($filename);
try {
$file = new SplFileObject($filename);
} catch (RuntimeException $e) {
throw new CannotReadCsvFileException($e->getMessage());
}

if ($ignoreFirstLine) {
array_shift($lines);
}
$file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

return $this->generator($lines, $schema);
return $this->generator($file, $schema, $ignoreFirstLine);
}

/**
* @psalm-param list<string> $lines
*
* @psalm-return Generator<int, array<string, int|float|string>>
*/
private function generator(array $lines, Schema $schema): Generator
private function generator(SplFileObject $file, Schema $schema, bool $ignoreFirstLine): Generator
{
foreach ($lines as $line) {
$firstLine = true;

foreach ($file as $line) {
if ($ignoreFirstLine && $firstLine) {
$firstLine = false;

continue;
}

if (!is_array($line)) {
continue;

Check warning on line 56 in src/Parser.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ continue; } if (!is_array($line)) { - continue; + break; } $data = []; foreach ($schema->columnDefinitions() as $columnDefinition) {
}

$data = [];

foreach ($schema->columnDefinitions() as $columnDefinition) {
$columnDefinition->parse(str_getcsv($line), $data);
$columnDefinition->parse($line, $data);
}

yield $data;
Expand Down
10 changes: 0 additions & 10 deletions src/exception/CannotReadCsvFileException.php
Expand Up @@ -9,18 +9,8 @@
*/
namespace SebastianBergmann\CsvParser;

use function sprintf;
use RuntimeException;

final class CannotReadCsvFileException extends RuntimeException implements Exception
{
public static function from(string $filename): self
{
return new self(
sprintf(
'Reading from CSV file %s failed',
$filename
)
);
}
}
16 changes: 0 additions & 16 deletions src/exception/InvalidValueException.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/schema/ColumnDefinition.php
Expand Up @@ -50,7 +50,7 @@ private function __construct(int $position, string $name, Type $type)
}

/**
* @psalm-param list<?string> $input
* @psalm-param list<string> $input
* @psalm-param array<string, mixed> $output
*
* @throws OutOfBoundsException
Expand All @@ -65,10 +65,6 @@ public function parse(array $input, array &$output): void

$value = $input[$this->position - 1];

if ($value === null) {
throw new InvalidValueException('Input array has element with invalid value at position ' . $this->position);
}

$output[$this->name] = $this->type->cast($value);
}

Expand Down
2 changes: 0 additions & 2 deletions tests/unit/ParserTest.php
Expand Up @@ -17,7 +17,6 @@
use PHPUnit\Framework\TestCase;

#[CoversClass(Parser::class)]
#[CoversClass(CannotReadCsvFileException::class)]
#[UsesClass(Schema::class)]
#[UsesClass(ColumnDefinition::class)]
#[UsesClass(Type::class)]
Expand Down Expand Up @@ -88,7 +87,6 @@ public function test_Parses_CSV_file_according_to_schema(array $expected, Schema
public function test_Cannot_read_from_CSV_file_that_does_not_exist(): void
{
$this->expectException(CannotReadCsvFileException::class);
$this->expectExceptionMessage('Reading from CSV file does_not_exist.csv failed');

(new Parser)->parse('does_not_exist.csv', Schema::from([]));
}
Expand Down
11 changes: 0 additions & 11 deletions tests/unit/schema/ColumnDefinitionTest.php
Expand Up @@ -56,17 +56,6 @@ public function testCannotParseColumnFromInputArrayThatDoesNotExist(): void
$this->column()->parse($input, $output);
}

public function testCannotParseColumnWithInvalidValue(): void
{
$input = [null];
$output = [];

$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('Input array has element with invalid value at position 1');

$this->column()->parse($input, $output);
}

private function column(): ColumnDefinition
{
return ColumnDefinition::from(1, 'name', Type::integer());
Expand Down

0 comments on commit ab1d88b

Please sign in to comment.