Skip to content

Commit

Permalink
Improve fix for issue #134
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 6, 2023
1 parent b5380bf commit e53b263
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.2",
"phpunit/phpunit": "^10.4.2"
"phpunit/phpunit": "^10.4.2",
"symfony/var-dumper": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 8 additions & 6 deletions src/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,16 @@ public function intersections(): self

/** @var Period $current */
$isContained = $current->contains($period);
if ($isContained && $isPreviouslyContained && !$sequence->contains($current)) {
$sequence->push($current->intersect($period));
if (!$sequence->contains($current)) {
if ($isContained && $isPreviouslyContained) {
$sequence->push($current->intersect($period));

return $sequence;
}
return $sequence;
}

if ($current->overlaps($period) && !$sequence->contains($current)) {
$sequence->push($current->intersect($period));
if ($current->overlaps($period)) {
$sequence->push($current->intersect($period));
}
}

$isPreviouslyContained = $isContained;
Expand Down
12 changes: 12 additions & 0 deletions src/SequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,16 @@ public function testTotalTimeDuration(): void
self::assertNotEquals($period->timeDuration(), $sequence->totalTimeDuration());
}
}

public function testIssue134RemoveDuplicateOnIntersection(): void
{
$p1 = Period::fromDate('2023-01-01 00:00:00', '2023-01-03 00:00:00');
$p2 = Period::fromDate('2023-01-01 00:00:00', '2023-01-03 00:00:00');
$p3 = Period::fromDate('2023-01-01 00:00:00', '2023-01-03 00:00:00');
$p4 = Period::fromDate('2023-01-02 00:00:00', '2023-01-04 00:00:00');
$p5 = Period::fromDate('2023-01-02 00:00:00', '2023-01-04 00:00:00');

$sequence = new Sequence($p1, $p2, $p3, $p4, $p5);
self::assertCount(2, $sequence->intersections());
}
}

0 comments on commit e53b263

Please sign in to comment.