Skip to content

Commit

Permalink
feat(ThrowInvalidArgumentException): rename ifNegative method to ifZe…
Browse files Browse the repository at this point in the history
…roOrNegative for clarity
  • Loading branch information
SandroMiguel committed Mar 30, 2024
1 parent d0bc09f commit 96725c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
@@ -1 +1 @@
{"version":1,"defects":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":3},"times":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":0.004,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithDefaultMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfPositiveValueDoesNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithoutCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithoutPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgNoPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":0}}
{"version":1,"defects":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":4,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithDefaultMessage":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithCustomMessage":3,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfPositiveValueDoesNotThrowException":4},"times":{"PhpThrow\\Tests\\BaseExceptionTraitTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeThrowsException":0.004,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeDoesNotThrowForPositiveValue":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testCreateMethod":0.001,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithDefaultMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfPositiveValueDoesNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithoutCustomMessage":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithoutPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgNoPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMessageWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueWithCustomMsgWithPlaceholder":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithValueNotThrowException":0,"PhpThrow\\Tests\\ThrowInvalidArgumentExceptionTest::testIfNegativeWithZeroValue":0}}
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -43,7 +43,7 @@ try {
try {
$value = -5;

ThrowInvalidArgumentException::ifNegative($value);
ThrowInvalidArgumentException::ifZeroOrNegative($value);

// ...
} catch (ThrowInvalidArgumentException $e) {
Expand Down Expand Up @@ -74,25 +74,25 @@ try {
}
```

#### ifNegative()
#### ifZeroOrNegative()

`ifNegative($value, $message = null)`: Throws an exception if the numeric value is less than 0.
`ifZeroOrNegative($value, $message = null)`: Throws an exception if the numeric value is less than 0.

```php
try {
ThrowInvalidArgumentException::ifNegative(-1);
ThrowInvalidArgumentException::ifZeroOrNegative(-1);
} catch (\PhpThrow\ThrowInvalidArgumentException $e) {
echo $e->getMessage(); // Output: The value must be greater than or equal to 0.
}
```

#### ifNegativeWithValue()
#### ifZeroOrNegativeWithValue()

`ifNegativeWithValue($value, $message = null)`: Throws an exception if the numeric value is negative, including the value in the error message.
`ifZeroOrNegativeWithValue($value, $message = null)`: Throws an exception if the numeric value is negative, including the value in the error message.

```php
try {
ThrowInvalidArgumentException::ifNegativeWithValue(-1);
ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(-1);
} catch (\PhpThrow\ThrowInvalidArgumentException $e) {
echo $e->getMessage(); // Output: The value -1 must be greater than or equal to 0.
}
Expand All @@ -101,7 +101,7 @@ try {
You can also use a custom message without the placeholder:

```php
ThrowInvalidArgumentException::ifNegativeWithValue(
ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
-1,
'Value must be positive'
); // Output: Value must be positive. Provided -1
Expand Down
6 changes: 3 additions & 3 deletions src/ThrowInvalidArgumentException.php
Expand Up @@ -31,7 +31,7 @@ class ThrowInvalidArgumentException extends \InvalidArgumentException
*
* @throws ThrowInvalidArgumentException If the value is negative.
*/
public static function ifNegative(
public static function ifZeroOrNegative(
float|int $value,
?string $message = null,
): void {
Expand All @@ -52,7 +52,7 @@ public static function ifNegative(
*
* @throws ThrowInvalidArgumentException If the value is negative.
*/
public static function ifNegativeWithValue(
public static function ifZeroOrNegativeWithValue(
float|int $value,
?string $message = null,
): void {
Expand All @@ -65,6 +65,6 @@ public static function ifNegativeWithValue(
$value
);

self::ifNegative($value, $message);
self::ifZeroOrNegative($value, $message);
}
}
20 changes: 12 additions & 8 deletions src/tests/ThrowInvalidArgumentExceptionTest.php
Expand Up @@ -45,7 +45,7 @@ public function testIfNegativeWithDefaultMessage(): void
'The value must be greater than or equal to 0.'
);

\PhpThrow\ThrowInvalidArgumentException::ifNegative(-1);
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegative(-1);
}

/**
Expand All @@ -58,7 +58,7 @@ public function testIfNegativeWithZeroValue(): void
'The value must be greater than or equal to 0.'
);

\PhpThrow\ThrowInvalidArgumentException::ifNegative(0);
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegative(0);
}

/**
Expand All @@ -70,7 +70,7 @@ public function testIfNegativeWithCustomMessage(): void
$this->expectException(\PhpThrow\ThrowInvalidArgumentException::class);
$this->expectExceptionMessage('My custom error message.');

\PhpThrow\ThrowInvalidArgumentException::ifNegative(
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegative(
-1,
'My custom error message.'
);
Expand All @@ -81,7 +81,7 @@ public function testIfNegativeWithCustomMessage(): void
*/
public function testIfPositiveValueDoesNotThrowException(): void
{
\PhpThrow\ThrowInvalidArgumentException::ifNegative(1);
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegative(1);

$this->assertTrue(true);
}
Expand All @@ -99,7 +99,9 @@ public function testIfNegativeWithValueWithoutCustomMessage(): void
\sprintf('The value %d must be greater than or equal to 0.', $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue($value);
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
$value
);
}

/**
Expand All @@ -116,7 +118,7 @@ public function testIfNegativeWithValueWithCustomMsgNoPlaceholder(): void
\sprintf('%s. Provided %d', $message, $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue(
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
$value,
$message
);
Expand All @@ -136,7 +138,7 @@ public function testIfNegativeWithValueWithCustomMsgWithPlaceholder(): void
\sprintf($message, $value)
);

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue(
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
$value,
$message
);
Expand All @@ -149,7 +151,9 @@ public function testIfNegativeWithValueNotThrowException(): void
{
$value = 5;

\PhpThrow\ThrowInvalidArgumentException::ifNegativeWithValue($value);
\PhpThrow\ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
$value
);

$this->expectNotToPerformAssertions();
}
Expand Down

0 comments on commit 96725c4

Please sign in to comment.