Skip to content

Commit

Permalink
ando como dia monday
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 11, 2023
1 parent b0792bb commit 0785c0f
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 214 deletions.
4 changes: 2 additions & 2 deletions .vscode/templates.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"",
"namespace Chevere\\Parameter;",
"",
"use Chevere\\Exceptions\\Core\\Exception;",
"use Exception;",
"",
"/**",
" * Exception thrown when ${1:description}.",
Expand Down Expand Up @@ -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}.",
Expand Down
1 change: 1 addition & 0 deletions src/Attributes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Chevere\Parameter\Interfaces\ArgumentsInterface;
use LogicException;
use ReflectionAttribute;
use ReflectionFunction;
use ReflectionMethod;
use function Chevere\Parameter\parameterAttr;
Expand Down
4 changes: 3 additions & 1 deletion src/FloatParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ final class FloatParameter implements FloatParameterInterface

public function __invoke(float $value): float
{
return assertFloat($this, $value);
$this->assert($value);

Check warning on line 38 in src/FloatParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ private ?float $max = null; public function __invoke(float $value) : float { - $this->assert($value); + return $value; } public function withDefault(float $value) : FloatParameterInterface

Check warning on line 38 in src/FloatParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ private ?float $max = null; public function __invoke(float $value) : float { - $this->assert($value); + return $value; } public function withDefault(float $value) : FloatParameterInterface

Check warning on line 38 in src/FloatParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ private ?float $max = null; public function __invoke(float $value) : float { - $this->assert($value); + return $value; } public function withDefault(float $value) : FloatParameterInterface

return $value;
}

public function withDefault(float $value): FloatParameterInterface
Expand Down
4 changes: 3 additions & 1 deletion src/IntParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/NullParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(

Check warning on line 36 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + new TypeError((string) message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore

Check warning on line 36 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + new TypeError((string) message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore

Check warning on line 36 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + new TypeError((string) message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore
(string) message('Argument value provided is not of type null')

Check warning on line 37 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + throw new TypeError(message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore

Check warning on line 37 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + throw new TypeError(message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore

Check warning on line 37 in src/NullParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($value === null) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type null')); + throw new TypeError(message('Argument value provided is not of type null')); } /** * @codeCoverageIgnore
);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/ObjectParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 40 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {

Check warning on line 40 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {

Check warning on line 40 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {
(string) message(

Check warning on line 41 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + throw new TypeError(message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {

Check warning on line 41 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + throw new TypeError(message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {

Check warning on line 41 in src/ObjectParameter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ if ($this->type->validate($value)) { return $value; } - throw new TypeError((string) message('Argument value provided is not of type `%type%`', type: $this->className())); + throw new TypeError(message('Argument value provided is not of type `%type%`', type: $this->className())); } public function setUp() : void {
'Argument value provided is not of type `%type%`',
type: $this->className()
)
);
}

public function setUp(): void
Expand Down
13 changes: 12 additions & 1 deletion src/StringParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions src/Traits/NumericParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 266 in src/Traits/NumericParameterTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ 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}]")); + throw new InvalidArgumentException(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())));

Check warning on line 266 in src/Traits/NumericParameterTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ 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}]")); + throw new InvalidArgumentException(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())));

Check warning on line 266 in src/Traits/NumericParameterTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ 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}]")); + throw new InvalidArgumentException(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())));
'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;
}
}
14 changes: 0 additions & 14 deletions src/functions-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
96 changes: 10 additions & 86 deletions src/functions-numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

use Chevere\Parameter\Interfaces\FloatParameterInterface;
use Chevere\Parameter\Interfaces\IntParameterInterface;
use InvalidArgumentException;
use function Chevere\Message\message;

/**
* @param float[] $accept
Expand All @@ -31,20 +29,20 @@ 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);
}
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;
Expand All @@ -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;
}
32 changes: 3 additions & 29 deletions src/functions-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
)
);
}
Loading

0 comments on commit 0785c0f

Please sign in to comment.