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
4 changes: 2 additions & 2 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::comparePositiveNumericStrings($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::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($nanoseconds, (string) PHP_INT_MIN) < 0) { + if ($nanoseconds > 0 && self::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) >= 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($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::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($nanoseconds, (string) PHP_INT_MIN) < 0) { + if ($nanoseconds >= 0 && self::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($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::compareNegativeNumericStrings($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 "LessThan": @@ @@ return new self($nanoseconds); } $nanoseconds = \sprintf('%.0f', round($nanoseconds)); - if ($nanoseconds > 0 && self::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($nanoseconds, (string) PHP_INT_MIN) < 0) { + if ($nanoseconds > 0 && self::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($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::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds < 0 && self::compareNegativeNumericStrings($nanoseconds, (string) PHP_INT_MIN) < 0) { + if ($nanoseconds > 0 && self::comparePositiveNumericStrings($nanoseconds, (string) PHP_INT_MAX) > 0 || $nanoseconds <= 0 && self::compareNegativeNumericStrings($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 @@ -135,8 +135,8 @@
return $timeSpan;
}

private function __construct(
private int $nanoseconds,
public function __construct(
private int $nanoseconds = 0,
) {}

public function toNanoseconds(): int
Expand Down
21 changes: 21 additions & 0 deletions tests/TimeSpanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,27 @@ public function testItThrowsForDateTimeInterfaceDiffInterval(): void
TimeSpan::fromInterval($interval);
}

#[TestWith([0])]
#[TestWith([100])]
#[TestWith([-100])]
#[TestWith([PHP_INT_MAX])]
#[TestWith([PHP_INT_MIN])]
#[TestWith([9_223_372_036_854_775_000])]
#[TestWith([-9_223_372_036_854_775_000])]
public function testConstructor(int $nanoseconds): void
{
$span = new TimeSpan($nanoseconds);

self::assertSame($nanoseconds, $span->toNanoseconds());
}

public function testConstructorWithoutArgs(): void
{
$span = new TimeSpan();

self::assertSame(0, $span->toNanoseconds());
}

#[TestWith([0, 0])]
#[TestWith([0.0, 0])]
#[TestWith([100, 100])]
Expand Down