Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/TimeSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

$nanoseconds = \sprintf('%.0f', round($nanoseconds));

if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0

Check warning on line 53 in src/TimeSpan.php

View workflow job for this annotation

GitHub Actions / check / infection

Escaped Mutant for Mutator "GreaterThan": @@ @@ return new self($nanoseconds); } $nanoseconds = \sprintf('%.0f', round($nanoseconds)); - if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { + if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) >= 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { throw new \OutOfBoundsException('The specified time span cannot be expressed as integer nanoseconds due to overflow.'); } return new self((int) $nanoseconds);

Check warning on line 53 in src/TimeSpan.php

View workflow job for this annotation

GitHub Actions / check / infection

Escaped Mutant for Mutator "GreaterThan": @@ @@ return new self($nanoseconds); } $nanoseconds = \sprintf('%.0f', round($nanoseconds)); - if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { + if ($nanoseconds >= 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { throw new \OutOfBoundsException('The specified time span cannot be expressed as integer nanoseconds due to overflow.'); } return new self((int) $nanoseconds);
|| $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0

Check warning on line 54 in src/TimeSpan.php

View workflow job for this annotation

GitHub Actions / check / infection

Escaped Mutant for Mutator "GreaterThan": @@ @@ return new self($nanoseconds); } $nanoseconds = \sprintf('%.0f', round($nanoseconds)); - if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { + if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) >= 0) { throw new \OutOfBoundsException('The specified time span cannot be expressed as integer nanoseconds due to overflow.'); } return new self((int) $nanoseconds);

Check warning on line 54 in src/TimeSpan.php

View workflow job for this annotation

GitHub Actions / check / infection

Escaped Mutant for Mutator "LessThan": @@ @@ return new self($nanoseconds); } $nanoseconds = \sprintf('%.0f', round($nanoseconds)); - if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { + if ($nanoseconds > 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds <= 0 && self::compareUnsignedNumericStrings($nanoseconds, (string) PHP_INT_MIN) > 0) { throw new \OutOfBoundsException('The specified time span cannot be expressed as integer nanoseconds due to overflow.'); } return new self((int) $nanoseconds);
) {
throw new \OutOfBoundsException('The specified time span cannot be expressed as integer nanoseconds due to overflow.');
}
Expand Down Expand Up @@ -287,6 +287,26 @@
{
return $this->nanoseconds >= 0;
}

public function add(self $timeSpan): self
{
return self::fromNanoseconds($this->nanoseconds + $timeSpan->nanoseconds);
}

public function sub(self $timeSpan): self
{
return self::fromNanoseconds($this->nanoseconds - $timeSpan->nanoseconds);
}

public function mul(int|float $times): self
{
return self::fromNanoseconds($this->nanoseconds * $times);
}

public function div(int|float $factor): self
{
return self::fromNanoseconds($this->nanoseconds / $factor);
}
private const array UNITS_MULT_MAP = [
'd' => self::MULT_DAYS,
'h' => self::MULT_HOURS,
Expand Down
105 changes: 105 additions & 0 deletions tests/TimeSpanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,109 @@ public function testIsPositiveOrZero(int $microseconds, bool $expected): void

self::assertSame($expected, $actual);
}

#[TestWith([2, 2, 4])]
#[TestWith([250, 250, 500])]
#[TestWith([-250, 250, 0])]
#[TestWith([-250, -250, -500])]
public function testAdd(int $firstDays, int $secondDays, int $sumDays): void
{
$firstSpan = TimeSpan::fromDays($firstDays);
$secondSpan = TimeSpan::fromDays($secondDays);

$sum = $firstSpan->add($secondSpan);

self::assertEquals($sumDays, $sum->toDays());
}

public function testAddOverflow(): void
{
self::expectException(\OutOfBoundsException::class);

$tooManyDays = 99_999;
$firstSpan = TimeSpan::fromDays($tooManyDays);
$secondSpan = TimeSpan::fromDays($tooManyDays);

$firstSpan->add($secondSpan);
}

#[TestWith([4, 2, 2])]
#[TestWith([250, 250, 0])]
#[TestWith([-250, 250, -500])]
#[TestWith([-250, -250, 0])]
public function testSub(int $firstDays, int $secondDays, int $diffDays): void
{
$firstSpan = TimeSpan::fromDays($firstDays);
$secondSpan = TimeSpan::fromDays($secondDays);

$diff = $firstSpan->sub($secondSpan);

self::assertEquals($diffDays, $diff->toDays());
}

public function testSubOverflow(): void
{
self::expectException(\OutOfBoundsException::class);

$tooManyDays = 99_999;
$zeroSpan = TimeSpan::fromDays(0);
$subSpan = TimeSpan::fromDays($tooManyDays);

$zeroSpan->sub($subSpan)
->sub($subSpan);
}

#[TestWith([2, 2, 4])]
#[TestWith([3, 3, 9])]
#[TestWith([5, 0, 0])]
#[TestWith([10, 0.5, 5])]
public function testMul(int $days, int|float $times, int $daysProduct): void
{
$span = TimeSpan::fromDays($days);

$product = $span->mul($times);

self::assertEquals($daysProduct, $product->toDays());
}

public function testMulOverflow(): void
{
self::expectException(\OutOfBoundsException::class);

$tooManyDays = 99_999;
$span = TimeSpan::fromDays($tooManyDays);

$span->mul(2);
}

#[TestWith([8_640_000_000_000, 1000, 8_640_000_000])]
#[TestWith([1_111_111_111_111, 3, 3_703_703_703_70])]
#[TestWith([1_111_111_111_111, 4, 2_777_777_777_78])]
#[TestWith([8_640_000_00, 0.1, 8_640_000_000])]
public function testDiv(int $nanoseconds, int|float $factor, int $nanosecondsQuotient): void
{
$span = TimeSpan::fromNanoseconds($nanoseconds);

$quotient = $span->div($factor);

self::assertEquals($nanosecondsQuotient, $quotient->toNanoseconds());
}

public function testDivOverflow(): void
{
self::expectException(\OutOfBoundsException::class);

$tooManyDays = 99_999;
$span = TimeSpan::fromDays($tooManyDays);

$span->div(0.1);
}

public function testDivByZero(): void
{
self::expectException(\DivisionByZeroError::class);

$span = TimeSpan::fromDays(5);
$span->div(0);
}
}