Skip to content

Commit

Permalink
Use variadic parameter instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 25, 2023
1 parent 87b9ab1 commit 20342d2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
26 changes: 12 additions & 14 deletions README.md
Expand Up @@ -38,22 +38,20 @@ use SebastianBergmann\CsvParser\Type;
use SebastianBergmann\CsvParser\Callback;

$schema = Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
ColumnDefinition::from(6, 'f', Type::callback(
new class implements Callback
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
ColumnDefinition::from(6, 'f', Type::callback(
new class implements Callback
{
public function apply(string $value): DateTimeImmutable
{
public function apply(string $value): DateTimeImmutable
{
return new DateTimeImmutable($value);
}
return new DateTimeImmutable($value);
}
)),
]
}
)),
);

$parser = new Parser;
Expand Down
14 changes: 8 additions & 6 deletions src/schema/Schema.php
Expand Up @@ -9,6 +9,9 @@
*/
namespace SebastianBergmann\CsvParser;

use function array_is_list;
use function assert;

/**
* @psalm-immutable
*
Expand All @@ -17,20 +20,19 @@
final class Schema
{
/**
* @psalm-var array<int, ColumnDefinition>
* @psalm-var list<ColumnDefinition>
*/
private readonly array $columnDefinitions;

/**
* @psalm-param array<int, ColumnDefinition> $columnDefinitions
*/
public static function from(array $columnDefinitions): self
public static function from(ColumnDefinition ...$columnDefinitions): self
{
assert(array_is_list($columnDefinitions));

return new self($columnDefinitions);
}

/**
* @psalm-param array<int, ColumnDefinition> $columnDefinitions
* @psalm-param list<ColumnDefinition> $columnDefinitions
*/
private function __construct(array $columnDefinitions)
{
Expand Down
34 changes: 13 additions & 21 deletions tests/unit/ParserTest.php
Expand Up @@ -41,13 +41,11 @@ public static function provider(): array
],
],
Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
]
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
),
__DIR__ . '/../fixture/fixture_with_header.csv',
true,
Expand All @@ -60,9 +58,7 @@ public static function provider(): array
],
],
Schema::from(
[
ColumnDefinition::from(2, 'b', Type::float()),
]
ColumnDefinition::from(2, 'b', Type::float()),
),
__DIR__ . '/../fixture/fixture_with_header.csv',
true,
Expand All @@ -79,13 +75,11 @@ public static function provider(): array
],
],
Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
]
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
ColumnDefinition::from(5, 'e', Type::boolean()),
),
__DIR__ . '/../fixture/fixture_without_header.csv',
false,
Expand All @@ -98,9 +92,7 @@ public static function provider(): array
],
],
Schema::from(
[
ColumnDefinition::from(2, 'b', Type::float()),
]
ColumnDefinition::from(2, 'b', Type::float()),
),
__DIR__ . '/../fixture/fixture_without_header.csv',
false,
Expand Down Expand Up @@ -129,6 +121,6 @@ public function test_Cannot_read_from_CSV_file_that_does_not_exist(): void
{
$this->expectException(CannotReadCsvFileException::class);

(new Parser)->parse('does_not_exist.csv', Schema::from([]), false);
(new Parser)->parse('does_not_exist.csv', Schema::from(), false);
}
}
10 changes: 4 additions & 6 deletions tests/unit/schema/SchemaTest.php
Expand Up @@ -27,12 +27,10 @@ final class SchemaTest extends TestCase
public function testAppliesColumnDefinitionsToMapInputArrayToOutputArray(): void
{
$schema = Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
]
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
);

$this->assertSame(
Expand Down

0 comments on commit 20342d2

Please sign in to comment.