From 0785c0fa63fae5b05ff5472c6e4dd355404868f0 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:28:30 -0300 Subject: [PATCH] ando como dia monday --- .vscode/templates.code-snippets | 4 +- src/Attributes/functions.php | 1 + src/FloatParameter.php | 4 +- src/IntParameter.php | 4 +- src/NullParameter.php | 10 ++- src/ObjectParameter.php | 11 +++- src/StringParameter.php | 13 +++- src/Traits/NumericParameterTrait.php | 56 ++++++++++++++++ src/functions-array.php | 14 ---- src/functions-numeric.php | 96 +++------------------------- src/functions-string.php | 32 +--------- src/functions.php | 35 ---------- tests/FloatParameterTest.php | 15 ++++- tests/FunctionsFloatTest.php | 10 +-- tests/FunctionsIntTest.php | 11 +--- tests/FunctionsStringTest.php | 49 +++++++------- tests/FunctionsTest.php | 2 +- tests/ObjectParameterTest.php | 2 +- 18 files changed, 155 insertions(+), 214 deletions(-) diff --git a/.vscode/templates.code-snippets b/.vscode/templates.code-snippets index 4441001..2453bfe 100644 --- a/.vscode/templates.code-snippets +++ b/.vscode/templates.code-snippets @@ -38,7 +38,7 @@ "", "namespace Chevere\\Parameter;", "", - "use Chevere\\Exceptions\\Core\\Exception;", + "use Exception;", "", "/**", " * Exception thrown when ${1:description}.", @@ -101,7 +101,7 @@ "", "namespace Chevere\\Parameter\\\\${TM_DIRECTORY/.*src\\/(([^\\/]*)(\\/)?)|(\\/)([^\\/]*)/$2${3:+.}${5:+.}$5/g};", "", - "use Chevere\\Exceptions\\Core\\Exception;", + "use Exception;", "", "/**", " * Exception thrown when ${1:description}.", diff --git a/src/Attributes/functions.php b/src/Attributes/functions.php index d4a09ea..0d6e3ba 100644 --- a/src/Attributes/functions.php +++ b/src/Attributes/functions.php @@ -15,6 +15,7 @@ use Chevere\Parameter\Interfaces\ArgumentsInterface; use LogicException; +use ReflectionAttribute; use ReflectionFunction; use ReflectionMethod; use function Chevere\Parameter\parameterAttr; diff --git a/src/FloatParameter.php b/src/FloatParameter.php index 58b7ac1..0f3d3fb 100644 --- a/src/FloatParameter.php +++ b/src/FloatParameter.php @@ -35,7 +35,9 @@ final class FloatParameter implements FloatParameterInterface public function __invoke(float $value): float { - return assertFloat($this, $value); + $this->assert($value); + + return $value; } public function withDefault(float $value): FloatParameterInterface diff --git a/src/IntParameter.php b/src/IntParameter.php index 789c892..eed4ed2 100644 --- a/src/IntParameter.php +++ b/src/IntParameter.php @@ -40,7 +40,9 @@ final class IntParameter implements IntParameterInterface public function __invoke(int $value): int { - return assertInt($this, $value); + $this->assert($value); + + return $value; } public function withDefault(int $value): IntParameterInterface diff --git a/src/NullParameter.php b/src/NullParameter.php index 193dbbb..1bbc8ec 100644 --- a/src/NullParameter.php +++ b/src/NullParameter.php @@ -18,6 +18,8 @@ use Chevere\Parameter\Traits\ParameterDefaultNullTrait; use Chevere\Parameter\Traits\ParameterTrait; use Chevere\Parameter\Traits\SchemaTrait; +use TypeError; +use function Chevere\Message\message; final class NullParameter implements NullParameterInterface { @@ -27,7 +29,13 @@ final class NullParameter implements NullParameterInterface public function __invoke(mixed $value): mixed { - return assertNull($this, $value); + if ($value === null) { + return $value; + } + + throw new TypeError( + (string) message('Argument value provided is not of type null') + ); } /** diff --git a/src/ObjectParameter.php b/src/ObjectParameter.php index 76987e2..da81193 100644 --- a/src/ObjectParameter.php +++ b/src/ObjectParameter.php @@ -33,7 +33,16 @@ final class ObjectParameter implements ObjectParameterInterface public function __invoke(object $value): object { - return assertObject($this, $value); + if ($this->type->validate($value)) { + return $value; + } + + throw new TypeError( + (string) message( + 'Argument value provided is not of type `%type%`', + type: $this->className() + ) + ); } public function setUp(): void diff --git a/src/StringParameter.php b/src/StringParameter.php index 0e31358..b65c12c 100644 --- a/src/StringParameter.php +++ b/src/StringParameter.php @@ -32,7 +32,18 @@ final class StringParameter implements StringParameterInterface public function __invoke(string|Stringable $value): string { - return assertString($this, $value); + $value = strval($value); + if ($this->regex->match($value) !== []) { + return $value; + } + + throw new InvalidArgumentException( + (string) message( + "Argument value provided `%provided%` doesn't match the regex `%regex%`", + provided: $value, + regex: strval($this->regex), + ) + ); } public function setUp(): void diff --git a/src/Traits/NumericParameterTrait.php b/src/Traits/NumericParameterTrait.php index 918b6c6..e4f5b8e 100644 --- a/src/Traits/NumericParameterTrait.php +++ b/src/Traits/NumericParameterTrait.php @@ -235,4 +235,60 @@ private function assertNumericLimit( ); } } + + /** + * @return TValue + */ + private function assert( + int|float $argument + ): int|float { + if ($this->accept !== []) { + if (in_array($argument, $this->accept, true)) { + return $argument; + } + $values = implode(',', $this->accept); + + throw new InvalidArgumentException( + (string) message( + 'Argument value provided `%provided%` is not an accepted value in `%value%`', + provided: strval($argument), + value: "[{$values}]" + ) + ); + } + if ($this->reject !== []) { + if (! in_array($argument, $this->reject, true)) { + return $argument; + } + $values = implode(',', $this->reject); + + throw new InvalidArgumentException( + (string) message( + 'Argument value provided `%provided%` is on rejected list `%value%`', + provided: strval($argument), + value: "[{$values}]" + ) + ); + } + if ($this->min() !== null && $argument < $this->min()) { + throw new InvalidArgumentException( + (string) message( + 'Argument value provided `%provided%` is less than `%min%`', + provided: strval($argument), + min: strval($this->min()) + ) + ); + } + if ($this->max !== null && $argument > $this->max) { + throw new InvalidArgumentException( + (string) message( + 'Argument value provided `%provided%` is greater than `%max%`', + provided: strval($argument), + max: strval($this->max) + ) + ); + } + + return $argument; + } } diff --git a/src/functions-array.php b/src/functions-array.php index 5fad4a8..b08c043 100644 --- a/src/functions-array.php +++ b/src/functions-array.php @@ -106,18 +106,6 @@ function assertArrayString( return assertArray($parameter, $argument); } -function assertNotEmpty(ParameterInterface $expected, mixed $value): void -{ - if ($expected instanceof ArrayTypeParameterInterface - && empty($value) - && count($expected->parameters()->requiredKeys()) > 0 - ) { - throw new InvalidArgumentException( - (string) message('Argument value provided is empty') - ); - } -} - // @phpstan-ignore-next-line function assertGeneric( GenericParameterInterface $parameter, @@ -131,8 +119,6 @@ function assertGeneric( $generic = ' *generic'; $genericKey = '_K' . $generic; $genericValue = '_V' . $generic; - $expected = $parameter->value(); - // assertNotEmpty($expected, $argument); try { foreach ($argument as $key => $value) { diff --git a/src/functions-numeric.php b/src/functions-numeric.php index 0d67248..949591d 100644 --- a/src/functions-numeric.php +++ b/src/functions-numeric.php @@ -15,8 +15,6 @@ use Chevere\Parameter\Interfaces\FloatParameterInterface; use Chevere\Parameter\Interfaces\IntParameterInterface; -use InvalidArgumentException; -use function Chevere\Message\message; /** * @param float[] $accept @@ -31,8 +29,11 @@ function float( array $reject = [], ): FloatParameterInterface { $parameter = new FloatParameter($description); - if ($default !== null) { - $parameter = $parameter->withDefault($default); + if ($accept !== []) { + $parameter = $parameter->withAccept(...$accept); + } + if ($reject !== []) { + $parameter = $parameter->withReject(...$reject); } if ($min !== null) { $parameter = $parameter->withMin($min); @@ -40,11 +41,8 @@ function float( if ($max !== null) { $parameter = $parameter->withMax($max); } - if ($accept !== []) { - $parameter = $parameter->withAccept(...$accept); - } - if ($reject !== []) { - $parameter = $parameter->withReject(...$reject); + if ($default !== null) { + $parameter = $parameter->withDefault($default); } return $parameter; @@ -69,89 +67,15 @@ function int( if ($reject !== []) { $parameter = $parameter->withReject(...$reject); } - if ($default !== null) { - $parameter = $parameter->withDefault($default); - } if ($min !== null) { $parameter = $parameter->withMin($min); } if ($max !== null) { $parameter = $parameter->withMax($max); } - - return $parameter; -} - -function assertNumeric( - IntParameterInterface|FloatParameterInterface $parameter, - int|float $argument, -): int|float { - if ($parameter->accept() !== []) { - if (in_array($argument, $parameter->accept(), true)) { - return $argument; - } - $values = implode(',', $parameter->accept()); - - throw new InvalidArgumentException( - (string) message( - 'Argument value provided `%provided%` is not an accepted value in `%value%`', - provided: strval($argument), - value: "[{$values}]" - ) - ); - } - if ($parameter->reject() !== []) { - if (! in_array($argument, $parameter->reject(), true)) { - return $argument; - } - $values = implode(',', $parameter->reject()); - - throw new InvalidArgumentException( - (string) message( - 'Argument value provided `%provided%` is on rejected list `%value%`', - provided: strval($argument), - value: "[{$values}]" - ) - ); - } - $min = $parameter->min(); - if ($min !== null && $argument < $min) { - throw new InvalidArgumentException( - (string) message( - 'Argument value provided `%provided%` is less than `%min%`', - provided: strval($argument), - min: strval($min) - ) - ); - } - $max = $parameter->max(); - if ($max !== null && $argument > $max) { - throw new InvalidArgumentException( - (string) message( - 'Argument value provided `%provided%` is greater than `%max%`', - provided: strval($argument), - max: strval($max) - ) - ); + if ($default !== null) { + $parameter = $parameter->withDefault($default); } - return $argument; -} - -function assertInt( - IntParameterInterface $parameter, - int $argument, -): int { - assertNumeric($parameter, $argument); - - return $argument; -} - -function assertFloat( - FloatParameterInterface $parameter, - float $argument -): float { - assertNumeric($parameter, $argument); - - return $argument; + return $parameter; } diff --git a/src/functions-string.php b/src/functions-string.php index 0671127..4842b98 100644 --- a/src/functions-string.php +++ b/src/functions-string.php @@ -15,9 +15,6 @@ use Chevere\Parameter\Interfaces\StringParameterInterface; use Chevere\Regex\Regex; -use InvalidArgumentException; -use Stringable; -use function Chevere\Message\message; function string( string $regex = '', @@ -46,14 +43,10 @@ function intString( ); } -function enum(string ...$string): StringParameterInterface +function enum(string $string, string ...$strings): StringParameterInterface { - if ($string === []) { - throw new InvalidArgumentException( - (string) message('At least one string must be provided') - ); - } - $cases = implode('|', $string); + array_unshift($strings, $string); + $cases = implode('|', $strings); $regex = "/\b({$cases})\b/"; return string($regex); @@ -94,22 +87,3 @@ function datetime( return string($regex, $description, $default); } - -function assertString( - StringParameterInterface $parameter, - Stringable|string $argument, -): string { - $regex = $parameter->regex(); - $argument = strval($argument); - if ($regex->match($argument) !== []) { - return $argument; - } - - throw new InvalidArgumentException( - (string) message( - "Argument value provided `%provided%` doesn't match the regex `%regex%`", - provided: $argument, - regex: strval($regex), - ) - ); -} diff --git a/src/functions.php b/src/functions.php index f978c71..74d1801 100644 --- a/src/functions.php +++ b/src/functions.php @@ -17,7 +17,6 @@ use Chevere\Parameter\Attributes\ReturnAttr; use Chevere\Parameter\Interfaces\ArgumentsInterface; use Chevere\Parameter\Interfaces\ArrayParameterInterface; -use Chevere\Parameter\Interfaces\BoolParameterInterface; use Chevere\Parameter\Interfaces\CastInterface; use Chevere\Parameter\Interfaces\NullParameterInterface; use Chevere\Parameter\Interfaces\ObjectParameterInterface; @@ -86,40 +85,6 @@ function arguments( return new Arguments($parameters, $arguments); } -function assertBool( - BoolParameterInterface $parameter, - bool $argument -): bool { - return $argument; -} - -function assertNull(NullParameterInterface $parameter, mixed $argument): mixed -{ - if ($argument === null) { - return $argument; - } - - throw new TypeError( - (string) message('Argument value provided is not of type null') - ); -} - -function assertObject( - ObjectParameterInterface $parameter, - object $argument -): object { - if ($parameter->type()->validate($argument)) { - return $argument; - } - - throw new InvalidArgumentException( - (string) message( - 'Argument value provided is not of type `%type%`', - type: $parameter->className() - ) - ); -} - function assertUnion( UnionParameterInterface $parameter, mixed $argument, diff --git a/tests/FloatParameterTest.php b/tests/FloatParameterTest.php index d20e93e..873df86 100644 --- a/tests/FloatParameterTest.php +++ b/tests/FloatParameterTest.php @@ -49,11 +49,20 @@ public function testConstruct(): void public function testWithAccept(): void { - $accept = [1.1, 2.2, 3.3]; + $values = [1.1, 2.2, 3.3]; $parameter = new FloatParameter(); - $withValue = $parameter->withAccept(...$accept); + $withValue = $parameter->withAccept(...$values); $this->assertNotSame($parameter, $withValue); - $this->assertSame($accept, $withValue->accept()); + $this->assertSame($values, $withValue->accept()); + } + + public function testWithReject(): void + { + $values = [1.1, 2.2, 3.3]; + $parameter = new FloatParameter(); + $withValue = $parameter->withReject(...$values); + $this->assertNotSame($parameter, $withValue); + $this->assertSame($values, $withValue->reject()); } public function testWithMin(): void diff --git a/tests/FunctionsFloatTest.php b/tests/FunctionsFloatTest.php index d0d26f1..21e308f 100644 --- a/tests/FunctionsFloatTest.php +++ b/tests/FunctionsFloatTest.php @@ -14,7 +14,6 @@ namespace Chevere\Tests; use PHPUnit\Framework\TestCase; -use function Chevere\Parameter\assertFloat; use function Chevere\Parameter\float; final class FunctionsFloatTest extends TestCase @@ -27,6 +26,7 @@ public function testFloat(): void $this->assertSame(null, $parameter->min()); $this->assertSame(null, $parameter->max()); $this->assertSame([], $parameter->accept()); + $this->assertSame([], $parameter->reject()); } public function testFloatOptions(): void @@ -45,14 +45,14 @@ public function testFloatOptions(): void $this->assertSame(100.0, $parameter->max()); $parameter = float(accept: [0, 1]); $this->assertSame([0.0, 1.0], $parameter->accept()); + $parameter = float(reject: [0, 1]); + $this->assertSame([0.0, 1.0], $parameter->reject()); } public function testAssertFloat(): void { $parameter = float(); - $this->assertSame(0.0, assertFloat($parameter, 0)); - $this->assertSame(0.0, assertFloat($parameter, 0.0)); - $parameter(0); - $parameter(0.0); + $this->assertSame(0.0, $parameter(0)); + $this->assertSame(0.0, $parameter(0.0)); } } diff --git a/tests/FunctionsIntTest.php b/tests/FunctionsIntTest.php index 1b0ae93..0aa7cf8 100644 --- a/tests/FunctionsIntTest.php +++ b/tests/FunctionsIntTest.php @@ -15,7 +15,6 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use function Chevere\Parameter\assertInt; use function Chevere\Parameter\boolInt; use function Chevere\Parameter\int; @@ -29,6 +28,7 @@ public function testInt(): void $this->assertSame(null, $parameter->min()); $this->assertSame(null, $parameter->max()); $this->assertSame([], $parameter->accept()); + $this->assertSame([], $parameter->reject()); } public function testIntOptions(): void @@ -47,13 +47,8 @@ public function testIntOptions(): void $this->assertSame(100, $parameter->max()); $parameter = int(accept: [0, 1]); $this->assertSame([0, 1], $parameter->accept()); - } - - public function testAssertInt(): void - { - $parameter = int(); - $this->assertSame(0, assertInt($parameter, 0)); - $parameter(0); + $parameter = int(reject: [0, 1]); + $this->assertSame([0, 1], $parameter->reject()); } public function testBoolInt(): void diff --git a/tests/FunctionsStringTest.php b/tests/FunctionsStringTest.php index cb3c237..d5ec2b4 100644 --- a/tests/FunctionsStringTest.php +++ b/tests/FunctionsStringTest.php @@ -16,7 +16,6 @@ use Chevere\Parameter\Interfaces\ParameterInterface; use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use function Chevere\Parameter\assertString; use function Chevere\Parameter\boolString; use function Chevere\Parameter\date; use function Chevere\Parameter\datetime; @@ -27,18 +26,18 @@ final class FunctionsStringTest extends TestCase { - public function testStringp(): void + public function testString(): void { $parameter = string(); $this->assertSame('', $parameter->description()); $this->assertSame(null, $parameter->default()); - $this->assertSame('', assertString($parameter, '')); + $this->assertSame('', $parameter('')); } public function testAssertString(): void { $parameter = string(); - $this->assertSame('test', assertString($parameter, 'test')); + $this->assertSame('test', $parameter('test')); $parameter('0'); } @@ -47,19 +46,19 @@ public function testEnum(): void $parameter = enum('test'); $this->assertSame('', $parameter->description()); $this->assertSame(null, $parameter->default()); - $this->assertSame('test', assertString($parameter, 'test')); + $this->assertSame('test', $parameter('test')); } public function testAssertEnum(): void { $parameter = enum('foo', 'bar'); - $this->assertSame('foo', assertString($parameter, 'foo')); - $this->assertSame('bar', assertString($parameter, 'bar')); + $this->assertSame('foo', $parameter('foo')); + $this->assertSame('bar', $parameter('bar')); $this->expectException(InvalidArgumentException::class); - assertString($parameter, 'barr'); + $parameter('barr'); } - public function testDatepDefault(): void + public function testDateDefault(): void { $parameter = date(default: '2023-04-10'); $this->assertSame('2023-04-10', $parameter->default()); @@ -67,16 +66,16 @@ public function testDatepDefault(): void date(default: 'fail'); } - public function testAssertDatep(): void + public function testAssertDate(): void { $parameter = date(); - $this->assertSame('1000-01-01', assertString($parameter, '1000-01-01')); - $this->assertSame('9999-12-31', assertString($parameter, '9999-12-31')); + $this->assertSame('1000-01-01', $parameter('1000-01-01')); + $this->assertSame('9999-12-31', $parameter('9999-12-31')); $this->expectException(InvalidArgumentException::class); - assertString($parameter, '9999-99-99'); + $parameter('9999-99-99'); } - public function testTimepDefault(): void + public function testTimeDefault(): void { $parameter = time(default: '23:59:59'); $this->assertSame('23:59:59', $parameter->default()); @@ -84,16 +83,16 @@ public function testTimepDefault(): void time(default: '999:99:99'); } - public function testAssertTimep(): void + public function testAssertTime(): void { $parameter = time(); - $this->assertSame('00:00:00', assertString($parameter, '00:00:00')); - $this->assertSame('999:59:59', assertString($parameter, '999:59:59')); + $this->assertSame('00:00:00', $parameter('00:00:00')); + $this->assertSame('999:59:59', $parameter('999:59:59')); $this->expectException(InvalidArgumentException::class); - assertString($parameter, '9999:99:99'); + $parameter('9999:99:99'); } - public function testDatetimepDefault(): void + public function testDatetimeDefault(): void { $parameter = datetime(default: '1000-01-01 23:59:59'); $this->assertSame('1000-01-01 23:59:59', $parameter->default()); @@ -101,12 +100,12 @@ public function testDatetimepDefault(): void datetime(default: '9999-99-99 999:99:99'); } - public function testAssertDatetimep(): void + public function testAssertDatetime(): void { $parameter = datetime(); - $this->assertSame('1000-01-01 23:59:59', assertString($parameter, '1000-01-01 23:59:59')); + $this->assertSame('1000-01-01 23:59:59', $parameter('1000-01-01 23:59:59')); $this->expectException(InvalidArgumentException::class); - assertString($parameter, '9999-99-99 999:99:99'); + $parameter('9999-99-99 999:99:99'); } public function testBoolString(): void @@ -176,9 +175,9 @@ public function testIntString(): void $parameter = intString(); $this->assertSame('', $parameter->description()); $this->assertSame(null, $parameter->default()); - assertString($parameter, '0'); - assertString($parameter, '1'); + $this->assertSame('0', $parameter('0')); + $this->assertSame('1', $parameter('1')); $this->expectException(InvalidArgumentException::class); - assertString($parameter, '1abc'); + $parameter('1abc'); } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index ea7c275..cd8c8ac 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -120,7 +120,7 @@ public function testFunctionObjectParameter(): void $this->assertSame(stdClass::class, $parameter->className()); $parameter = object(stdClass::class, 'foo'); $this->assertSame('foo', $parameter->description()); - $this->expectException(InvalidArgumentException::class); + $this->expectException(TypeError::class); $parameter(parameters()); } diff --git a/tests/ObjectParameterTest.php b/tests/ObjectParameterTest.php index 879f1d8..ff5580e 100644 --- a/tests/ObjectParameterTest.php +++ b/tests/ObjectParameterTest.php @@ -87,7 +87,7 @@ public function testInvoke(): void { $parameter = object(__CLASS__); $parameter($this); - $this->expectException(InvalidArgumentException::class); + $this->expectException(TypeError::class); $parameter(new stdClass()); } }