Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mutation coverage #485

Merged
merged 2 commits into from
Sep 3, 2024
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
198 changes: 101 additions & 97 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions config/infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"DecrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
"Psl\\DataStructure\\PriorityQueue::peek",
"Psl\\DateTime\\TemporalConvenienceMethodsTrait::since"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> Altering since() causes timeouts in the async component.

]
},
"FunctionCallRemoval": {
Expand All @@ -42,7 +43,8 @@
},
"IncrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
"Psl\\DataStructure\\PriorityQueue::peek",
"Psl\\DateTime\\TemporalConvenienceMethodsTrait::since"
]
},
"LogicalNot": {
Expand All @@ -58,6 +60,7 @@
},
"Throw_": {
"ignore": [
"Psl\\DateTime\\DateTime::__construct",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> It's impossible to test the InvalidArgumentExceptions that are being thrown. I'm wondering if it would make more sense to use the same checks from fromParts in the constructor directly.

The constructor does a range check, the fromParts does a calendar (intl) check.
Both result in different type of exceptions with different messages.

Would it make sense to always go for the fromParts() checks instead?

"Psl\\File\\ReadHandle::__construct",
"Psl\\File\\WriteHandle::__construct",
"Psl\\File\\ReadWriteHandle::__construct",
Expand Down
9 changes: 4 additions & 5 deletions src/Psl/DateTime/DateTimeConvenienceMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ public function plusMonths(int $months): static
return $this;
}

if (0 > $months) {
if ($months < 1) {
return $this->minusMonths(-$months);
}

$plus_years = Math\div($months, MONTHS_PER_YEAR);
$months_left = $months - ($plus_years * MONTHS_PER_YEAR);
$target_month = $this->getMonth() + $months_left;

if ($target_month > 12) {
if ($target_month > MONTHS_PER_YEAR) {
$plus_years++;
$target_month = $target_month - MONTHS_PER_YEAR;
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public function minusMonths(int $months): static
return $this;
}

if (0 > $months) {
if ($months < 1) {
return $this->plusMonths(-$months);
}

Expand Down Expand Up @@ -616,10 +616,9 @@ public function toString(null|DateStyle $date_style = null, null|TimeStyle $time
$timestamp = $this->getTimestamp();

/**
* @psalm-suppress InvalidOperand
* @psalm-suppress ImpureMethodCall
*/
return Internal\create_intl_date_formatter($date_style, $time_style, null, $timezone ?? $this->getTimezone(), $locale)
->format($timestamp->getSeconds() + ($timestamp->getNanoseconds() / NANOSECONDS_PER_SECOND));
->format($timestamp->getSeconds());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default pattern does not represent nanoseconds.
it is not possible (to my knowledge) to get a timestamp to contain nanoseconds > NANOSECONDS_PER_SECOND - 1.

I did not find a case to cover this calculation

}
}
29 changes: 13 additions & 16 deletions src/Psl/DateTime/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,27 +674,24 @@ public function toString(int $max_decimals = 3): string
$sec_sign = $this->seconds < 0 || $this->nanoseconds < 0 ? '-' : '';
$sec = Math\abs($this->seconds);

/** @var list<array{string, string}> $values */
$values = [
[((string) $this->hours), 'hour(s)'],
[((string) $this->minutes), 'minute(s)'],
[$sec_sign . ((string) $sec) . $decimal_part, 'second(s)'],
];

// $end is the sizeof($values), use static value for better performance.
$end = 3;
while ($end > 0 && $values[$end - 1][0] === '0') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutating the while's results in timeouts. I decided to go for a couple of simple if statements instead that results in the same behaviour.

--$end;
$containsHours = $this->hours !== 0;
$containsMinutes = $this->minutes !== 0;
$concatenatedSeconds = $sec_sign . ((string) $sec) . $decimal_part;
$containsSeconds = $concatenatedSeconds !== '0';

/** @var list<string> $output */
$output = [];
if ($containsHours) {
$output[] = ((string) $this->hours) . ' hour(s)';
}

$start = 0;
while ($start < $end && $values[$start][0] === '0') {
++$start;
if ($containsMinutes || ($containsHours && $containsSeconds)) {
$output[] = ((string) $this->minutes) . ' minute(s)';
}

$output = [];
for ($i = $start; $i < $end; ++$i) {
$output[] = $values[$i][0] . ' ' . $values[$i][1];
if ($containsSeconds) {
$output[] = $concatenatedSeconds . ' second(s)';
}

return ([] === $output) ? '0 second(s)' : Str\join($output, ', ');
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/Collection/MutableSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testArrayAccess(): void

static::assertTrue(isset($set['foo']));
static::assertSame('foo', $set['foo']);

unset($set['foo']);
static::assertFalse(isset($set['foo']));

Expand Down Expand Up @@ -191,6 +191,16 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
$set[false];
}

public function testFromArrayKeysConstructor()
{
$set = MutableSet::fromArrayKeys(['foo' => 1, 'bar' => 1, 'baz' => 1]);

static::assertCount(3, $set);
static::assertTrue($set->contains('foo'));
static::assertTrue($set->contains('bar'));
static::assertTrue($set->contains('baz'));
}

/**
* @template T of array-key
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ protected function createFromList(array $items): Set
{
return Set::fromArray($items);
}

public function testFromArrayKeysConstructor()
{
$set = Set::fromArrayKeys(['foo' => 1, 'bar' => 1, 'baz' => 1]);

static::assertCount(3, $set);
static::assertTrue($set->contains('foo'));
static::assertTrue($set->contains('bar'));
static::assertTrue($set->contains('baz'));
}
}
Loading