Skip to content

Commit

Permalink
Remove array_merge() from constructors when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Oct 22, 2023
1 parent 0ad27a7 commit 01bd923
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Flow\ETL\DSL;

use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row;
use Flow\ETL\Row\EntryFactory;
use Flow\ETL\Row\EntryReference;
Expand Down Expand Up @@ -47,13 +46,7 @@ function optional(Expression $expression) : Expression

function struct(string ...$entries) : StructureReference
{
if (!\count($entries)) {
throw new InvalidArgumentException('struct (StructureReference) require at least one entry');
}

$entry = \array_shift($entries);

return new StructureReference($entry, ...$entries);
return new StructureReference(...$entries);
}

function lit(mixed $value) : Expression
Expand Down
9 changes: 7 additions & 2 deletions src/core/etl/src/Flow/ETL/Join/Comparison/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\ETL\Join\Comparison;

use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Join\Comparison;
use Flow\ETL\Row;
use Flow\ETL\Row\EntryReference;
Expand All @@ -18,9 +19,13 @@ final class All implements Comparison
*/
private array $comparisons;

public function __construct(Comparison $comparison, Comparison ...$comparisons)
public function __construct(Comparison ...$comparisons)
{
$this->comparisons = \array_merge([$comparison], $comparisons);
if (!$comparisons) {
throw new InvalidArgumentException('All comparison requires at least one comparison');
}

$this->comparisons = $comparisons;
}

public function __serialize() : array
Expand Down
9 changes: 7 additions & 2 deletions src/core/etl/src/Flow/ETL/Join/Comparison/Any.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\ETL\Join\Comparison;

use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Join\Comparison;
use Flow\ETL\Row;
use Flow\ETL\Row\EntryReference;
Expand All @@ -18,9 +19,13 @@ final class Any implements Comparison
*/
private array $comparisons;

public function __construct(Comparison $comparison, Comparison ...$comparisons)
public function __construct(Comparison ...$comparisons)
{
$this->comparisons = \array_merge([$comparison], $comparisons);
if (!$comparisons) {
throw new InvalidArgumentException('Any comparison requires at least one comparison');
}

$this->comparisons = $comparisons;
}

public function __serialize() : array
Expand Down
10 changes: 8 additions & 2 deletions src/core/etl/src/Flow/ETL/Row/StructureReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Flow\ETL\Row;

use Flow\ETL\Exception\InvalidArgumentException;

/**
* @implements Reference<array{entries: array<string>, alias: ?string}>
*/
Expand All @@ -14,9 +16,13 @@ final class StructureReference implements Reference
/** @var array<string> */
private readonly array $entries;

public function __construct(string $entry, string ...$entries)
public function __construct(string ...$entries)
{
$this->entries = \array_merge([$entry], $entries);
if (!$entries) {
throw new InvalidArgumentException('StructureReference requires at least one entry');
}

$this->entries = $entries;
}

public function __serialize() : array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Join\Comparison;
use Flow\ETL\Join\Comparison\All;
use Flow\ETL\Row;

final class AllTest extends TestCase
{
public function test_empty_comparisons() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('All comparison requires at least one comparison');

new All();
}

public function test_failure() : void
{
$comparison1 = $this->createStub(Comparison::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Join\Comparison;
use Flow\ETL\Join\Comparison\Any;
use Flow\ETL\Row;

final class AnyTest extends TestCase
{
public function test_empty_comparisons() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Any comparison requires at least one comparison');

new Any();
}

public function test_failure() : void
{
$comparison1 = $this->createStub(Comparison::class);
Expand Down

0 comments on commit 01bd923

Please sign in to comment.