Skip to content

feat: add an exception when trying to pass an argument that does not exist #20

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions src/BuildableValidateParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Buildotter\Core;

use InvalidArgumentException;
use ReflectionProperty;

trait BuildableValidateParam
{
/**
* @param array<int|string, mixed> $arguments
* @param array<ReflectionProperty> $properties
* @throws InvalidArgumentException
* @return void
*/
public function validateArguments(array $arguments, array $properties): void
{
$propertiesName = [];
\array_walk(
array: $properties,
callback: function (\ReflectionProperty $property) use (&$propertiesName) {
$propertiesName[$property->getName()] = $property->getName();
},
);
$invalidArgs = \array_diff_key($arguments, $propertiesName);
if (0 !== \count($invalidArgs)) {
throw new InvalidArgumentException(
message: \sprintf(
'Arguments %s dont match with call function',
\implode(separator: ',', array: $invalidArgs),
),
);
}
}
}
9 changes: 9 additions & 0 deletions src/BuildableWithArgUnpacking.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

namespace Buildotter\Core;

use ReflectionException;

trait BuildableWithArgUnpacking
{
use BuildableValidateParam;
/**
* @param mixed ...$values
* @throws ReflectionException
*/
public function with(...$values): static
{
$r = new \ReflectionClass(static::class);

$clone = $r->newInstanceWithoutConstructor();

$this->validateArguments(
arguments: $values,
properties: $r->getProperties(),
);

foreach ($r->getProperties() as $property) {
$field = $property->name;
$clone->$field = match (\array_key_exists($field, $values)) {
Expand Down
6 changes: 6 additions & 0 deletions src/BuildableWithArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

trait BuildableWithArray
{
use BuildableValidateParam;
/**
* @param array<string, mixed> $values
*/
Expand All @@ -15,6 +16,11 @@ public function with(array $values): static

$clone = $r->newInstanceWithoutConstructor();

$this->validateArguments(
arguments: $values,
properties: $r->getProperties(),
);

foreach ($r->getProperties() as $property) {
$field = $property->name;
$clone->$field = match (\array_key_exists($field, $values)) {
Expand Down
31 changes: 31 additions & 0 deletions tests/BuildatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Buildotter\Core\BuildableWithArgUnpacking;
use Buildotter\Core\BuildableWithArray;
use Buildotter\Core\Buildatable;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use function PHPUnit\Framework\assertEquals;

final class BuildatableTest extends TestCase
Expand Down Expand Up @@ -39,6 +41,35 @@ public function test_it_is_buildatable_with_arg_unpacking(): void
$fooBuiltWithArgsUnpacking,
);
}

/**
* @throws ReflectionException
*/
public function test_it_is_not_buildatable_with_bad_arg_unpacking(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Arguments test dont match with call function');
FooBuilderWithArgUnpacking::new()
->with(badargument: 'test', number: random()->randomNumber())
->build();
FooBuilderWithArray::new()
->with(['badargument' => 'test'])
->build();
}

public function test_it_is_not_buildatable_with_bad_arg_array(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Arguments test dont match with call function');
FooBuilderWithArray::new()
->with(
values: [
'badargument' => 'test',
'number' => random()->randomNumber(),
],
)
->build();
}
}

final class Foo
Expand Down