Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 4, 2024
1 parent f3c4bdf commit e9748cd
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 35 deletions.
13 changes: 9 additions & 4 deletions src/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Chevere\Parameter\Interfaces\ArgumentsInterface;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Parameter\Traits\ExceptionErrorMessageTrait;
use InvalidArgumentException;
use OutOfBoundsException;
use ReflectionClass;
Expand All @@ -27,6 +28,8 @@

final class Arguments implements ArgumentsInterface
{
use ExceptionErrorMessageTrait;

private ParametersInterface $iterable;

/**
Expand Down Expand Up @@ -244,18 +247,20 @@ private function assertArgument(string $name, mixed $argument): void
$this->arguments[$name] = $parameter->__invoke($argument);
} catch (TypeError $e) {
throw new TypeError(
$this->getExceptionMessage($name, $e)
$this->getExceptionPropertyMessage($name, $e)
);
} catch (Throwable $e) {
throw new InvalidArgumentException(
$this->getExceptionMessage($name, $e)
$this->getExceptionPropertyMessage($name, $e)
);
}
}

private function getExceptionMessage(string $property, Throwable $e): string
private function getExceptionPropertyMessage(string $property, Throwable $e): string
{
return "[{$property}]: {$e->getMessage()}";
$message = $this->getExceptionMessage($e);

return "[{$property}]: {$message}";
}

private function handleParameters(): void
Expand Down
18 changes: 5 additions & 13 deletions src/IterableParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\TypeInterface;
use Chevere\Parameter\Traits\ArrayParameterTrait;
use Chevere\Parameter\Traits\ExceptionErrorMessageTrait;
use Chevere\Parameter\Traits\ParameterTrait;
use InvalidArgumentException;
use Throwable;
Expand All @@ -26,6 +27,7 @@ final class IterableParameter implements IterableParameterInterface
{
use ParameterTrait;
use ArrayParameterTrait;
use ExceptionErrorMessageTrait;

/**
* @var iterable<mixed, mixed>
Expand Down Expand Up @@ -56,26 +58,16 @@ public function __invoke(iterable $value): iterable
);
}
$iterable = ' *iterable';
$iterableKey = '_K' . $iterable;
$iterableValue = '_V' . $iterable;
$iterableKey = 'K' . $iterable;
$iterableValue = 'V' . $iterable;

try {
foreach ($value as $k => $v) {
assertNamedArgument($iterableKey, $this->key, $k);
assertNamedArgument($iterableValue, $this->value, $v);
}
} catch (Throwable $e) {
$message = $e->getMessage();
$strstr = strstr($message, ':', false);
if (! is_string($strstr)) {
$strstr = $message; // @codeCoverageIgnore
} else {
$strstr = substr($strstr, 2);
}
$calledIn = strpos($strstr, ', called in');
$message = $calledIn
? substr($strstr, 0, $calledIn)
: $strstr;
$message = $this->getExceptionMessage($e, ': ');

throw new InvalidArgumentException($message);
}
Expand Down
1 change: 1 addition & 0 deletions src/ReflectionParameterTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
$attribute = reflectedParameterAttribute($reflection);
$parameter = $attribute->parameter();
} catch (Throwable) {
// Do nothing
}
if ($this->reflection->isDefaultValueAvailable()
&& method_exists($parameter, 'withDefault')
Expand Down
37 changes: 37 additions & 0 deletions src/Traits/ExceptionErrorMessageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Parameter\Traits;

use Throwable;

trait ExceptionErrorMessageTrait
{
private function getExceptionMessage(
Throwable $e,
string $needle = '::__invoke(): ',
): string {
$message = $e->getMessage();
$strstr = strstr($message, $needle, false);
if (! is_string($strstr)) {
$strstr = $message; // @codeCoverageIgnore
} else {
$strstr = substr($strstr, strlen($needle));
}
$calledIn = strpos($strstr, ', called in');

return $calledIn
? substr($strstr, 0, $calledIn)
: $strstr;
}
}
16 changes: 3 additions & 13 deletions src/UnionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Chevere\Parameter\Interfaces\TypeInterface;
use Chevere\Parameter\Interfaces\UnionParameterInterface;
use Chevere\Parameter\Traits\ArrayParameterTrait;
use Chevere\Parameter\Traits\ExceptionErrorMessageTrait;
use Chevere\Parameter\Traits\ParameterAssertArrayTypeTrait;
use Chevere\Parameter\Traits\ParameterErrorMessageTrait;
use Chevere\Parameter\Traits\ParameterTrait;
use InvalidArgumentException;
use LogicException;
Expand All @@ -31,7 +31,7 @@ final class UnionParameter implements UnionParameterInterface
use ParameterTrait;
use ArrayParameterTrait;
use ParameterAssertArrayTypeTrait;
use ParameterErrorMessageTrait;
use ExceptionErrorMessageTrait;

private mixed $default = null;

Expand Down Expand Up @@ -113,17 +113,7 @@ private function getParameterError(
Throwable $e
): string {
$type = $parameter::class;
$message = $e->getMessage();
$strstr = strstr($message, '::__invoke():', false);
if (! is_string($strstr)) {
$message = $message; // @codeCoverageIgnore
} else {
$message = substr($strstr, 14);
}
$calledIn = strpos($message, ', called in');
$message = $calledIn
? substr($message, 0, $calledIn)
: $message;
$message = $this->getExceptionMessage($e);

return <<<PLAIN
Parameter `{$name}` <{$type}>: {$message}
Expand Down
2 changes: 1 addition & 1 deletion tests/ArgumentsIterableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function testIterableArrayConflict(array $args): void
K: string()
);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^\[_V \*iterable\]\:.*/');
$this->expectExceptionMessageMatches('/^\[V \*iterable\]\:.*/');
$parameter($args);
}
}
12 changes: 9 additions & 3 deletions tests/ArrayParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Chevere\Tests;

use ArgumentCountError;
use Chevere\Parameter\ArrayParameter;
use Chevere\Parameter\Interfaces\IntParameterInterface;
use Chevere\Parameter\Interfaces\StringParameterInterface;
Expand Down Expand Up @@ -52,12 +53,14 @@ public function testWithDefault(): void
$default = [
'test' => 1,
];
$withDefault = $parameter->withDefault($default);
$with = $parameter->withDefault($default);
$this->assertNotSame($parameter, $with);
$this->assertSame($default, $with->default());
(new ParameterHelper())->testWithParameterDefault(
primitive: 'array',
parameter: $parameter,
default: $default,
parameterWithDefault: $withDefault
parameterWithDefault: $with
);
$this->assertSame([
'type' => 'array#map',
Expand All @@ -68,7 +71,10 @@ public function testWithDefault(): void
'required' => true,
] + $int->schema(),
],
], $withDefault->schema());
], $with->schema());
$this->expectException(ArgumentCountError::class);
$this->expectExceptionMessage('Missing required argument(s): `test`');
$parameter->withDefault([]);
}

public function testWithRequired(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Attribute/FunctionReturnAttrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testUsesAttr(): void
],
];
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('[role]: [tenants]: [_V *iterable]: Argument value provided `6` is greater than `5`');
$this->expectExceptionMessage('[role]: [tenants]: [V *iterable]: Argument value provided `6` is greater than `5`');
usesAttr($value);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/IterableParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ public function testInvoke(): void
$parameter([]);
}

public function testKeyError(): void
{
$parameter = iterable(K: int(), V: string());
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
<<<PLAIN
[K *iterable]: Argument #1 (\$value) must be of type int, string given
PLAIN
);
$parameter([
'key' => 'foo',
]);
}

public function testValueError(): void
{
$parameter = iterable(K: int(), V: string());
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
<<<PLAIN
[V *iterable]: Argument #1 (\$value) must be of type Stringable|string, int given
PLAIN
);
$parameter([
100 => 100,
]);
}

public function testWithDefault(): void
{
$value = [10, '10'];
Expand Down

0 comments on commit e9748cd

Please sign in to comment.