Skip to content

Commit

Permalink
Remove unecessary validation
Browse files Browse the repository at this point in the history
By requiring PHP 8.3 we no longer need to have two different
exceptions to handle.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Sep 24, 2024
1 parent da6201e commit 4012dd8
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 39 deletions.
15 changes: 0 additions & 15 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,3 @@ parameters:
paths:
- src
- test

ignoreErrors:
# This is only for PHP 8.2 compatibility
-
message: "#^PHPDoc tag @throws with type DateMalformedStringException\\|InvalidArgumentException is not subtype of Throwable$#"
count: 1
path: src/FrozenClock.php
-
message: "#^Class DateMalformedStringException not found\\.$#"
count: 1
path: test/FrozenClockTest.php
-
message: "#^Parameter \\#1 \\$exception of method PHPUnit\\\\Framework\\\\TestCase\\:\\:expectException\\(\\) expects class\\-string\\<Throwable\\>, string given\\.$#"
count: 1
path: test/FrozenClockTest.php
13 changes: 2 additions & 11 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use DateMalformedStringException;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;

final class FrozenClock implements Clock
{
Expand All @@ -29,19 +28,11 @@ public function setTo(DateTimeImmutable $now): void
*
* @param string $modifier @see https://www.php.net/manual/en/datetime.formats.php
*
* @throws InvalidArgumentException When an invalid format string is passed (PHP < 8.3).
* @throws DateMalformedStringException When an invalid date/time string is passed (PHP 8.3+).
* @throws DateMalformedStringException When an invalid date/time string is passed.
*/
public function adjustTime(string $modifier): void
{
$modifiedTime = @$this->now->modify($modifier);

// PHP < 8.3 won't throw exceptions on invalid modifiers
if ($modifiedTime === false) {
throw new InvalidArgumentException('The given modifier is invalid');
}

$this->now = $modifiedTime;
$this->now = $this->now->modify($modifier);
}

public function now(): DateTimeImmutable
Expand Down
13 changes: 0 additions & 13 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

use DateMalformedStringException;
use DateTimeImmutable;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -57,17 +55,6 @@ public function adjustTimeChangesTheObject(): void
}

#[Test]
#[RequiresPhp('< 8.3.0')]
public function adjustTimeThrowsForInvalidModifierInPhp82(): void
{
$clock = FrozenClock::fromUTC();

$this->expectException(InvalidArgumentException::class);
$clock->adjustTime('invalid');
}

#[Test]
#[RequiresPhp('>= 8.3.0')]
public function adjustTimeThrowsForInvalidModifier(): void
{
$clock = FrozenClock::fromUTC();
Expand Down

0 comments on commit 4012dd8

Please sign in to comment.