Skip to content

feat: generics types support #9

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

Merged
merged 3 commits into from
Jan 18, 2022
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
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ parameters:
level: 7
paths:
- src
- tests
- tests
38 changes: 22 additions & 16 deletions src/Interval/DateTimeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@

use function count;

/**
* @template TPoint of DateTimeInterface
* @implements IntervalInterface<TPoint>
*/
final class DateTimeInterval implements IntervalInterface
{
/**
* @var DateTimeInterface
* @var TPoint
*/
private $low;

/**
* @var DateTimeInterface
* @var TPoint
*/
private $high;

/**
* DateTimeInterval constructor
* @param DateTimeInterface $low
* @param DateTimeInterface $high
* @param TPoint $low
* @param TPoint $high
*/
public function __construct($low, $high)
{
Expand All @@ -37,10 +41,12 @@ public function __construct($low, $high)
}

/**
* @param DateTimeInterface[] $interval
* @return DateTimeInterval
* @phpstan-ignore-next-line
* @psalm-template TPoint of DateTimeInterface
* @param TPoint[] $interval
* @return IntervalInterface<TPoint>
*/
public static function fromArray($interval): DateTimeInterval
public static function fromArray(array $interval): IntervalInterface
{
if (count($interval) !== 2) {
throw new InvalidArgumentException('Wrong interval array');
Expand All @@ -59,20 +65,20 @@ public function getHigh(): DateTimeInterface
}

/**
* @param DateTimeInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function equalTo($otherInterval): bool
public function equalTo(IntervalInterface $otherInterval): bool
{
return $this->getLow()->getTimestamp() === $otherInterval->getLow()->getTimestamp() &&
$this->getHigh()->getTimestamp() === $otherInterval->getHigh()->getTimestamp();
}

/**
* @param DateTimeInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function lessThan($otherInterval): bool
public function lessThan(IntervalInterface $otherInterval): bool
{
return $this->getLow()->getTimestamp() < $otherInterval->getLow()->getTimestamp() ||
(
Expand All @@ -82,19 +88,19 @@ public function lessThan($otherInterval): bool
}

/**
* @param DateTimeInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function intersect($otherInterval): bool
public function intersect(IntervalInterface $otherInterval): bool
{
return !($this->getHigh() < $otherInterval->getLow() || $otherInterval->getHigh() < $this->getLow());
}

/**
* @param DateTimeInterval $otherInterval
* @return DateTimeInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return IntervalInterface<TPoint>
*/
public function merge($otherInterval): DateTimeInterval
public function merge(IntervalInterface $otherInterval): IntervalInterface
{
return new DateTimeInterval(
min($this->getLow(), $otherInterval->getLow()),
Expand Down
61 changes: 44 additions & 17 deletions src/Interval/IntervalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,56 @@

namespace Danon\IntervalTree\Interval;

/**
* @template TPoint
*/
interface IntervalInterface
{
// @phpstan-ignore-next-line
/**
* @param TPoint $low
* @param TPoint $high
*/
public function __construct($low, $high);

// @phpstan-ignore-next-line
public static function fromArray($interval);

// @phpstan-ignore-next-line
/**
* @phpstan-ignore-next-line
* @psalm-template TPoint
* @param TPoint[] $interval
* @return IntervalInterface<TPoint>
*/
public static function fromArray(array $interval): IntervalInterface;

/**
* @return TPoint
*/
public function getLow();

// @phpstan-ignore-next-line
/**
* @return TPoint
*/
public function getHigh();

// @phpstan-ignore-next-line
public function equalTo($otherInterval): bool;

// @phpstan-ignore-next-line
public function lessThan($otherInterval): bool;

// @phpstan-ignore-next-line
public function intersect($otherInterval): bool;

// @phpstan-ignore-next-line
public function merge($otherInterval);
/**
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function equalTo(IntervalInterface $otherInterval): bool;

/**
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function lessThan(IntervalInterface $otherInterval): bool;

/**
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function intersect(IntervalInterface $otherInterval): bool;

/**
* @param IntervalInterface<TPoint> $otherInterval
* @return IntervalInterface<TPoint>
*/
public function merge(IntervalInterface $otherInterval): IntervalInterface;
}
42 changes: 24 additions & 18 deletions src/Interval/NumericInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

use function count;

/**
* @template TPoint of int|float
* @implements IntervalInterface<TPoint>
*/
final class NumericInterval implements IntervalInterface
{
/**
* @var int|float
* @var TPoint
*/
private $low;

/**
* @var int|float
* @var TPoint
*/
private $high;

/**
* NumericInterval constructor
* @param int|float $low
* @param int|float $high
* @param TPoint $low
* @param TPoint $high
*/
public function __construct($low, $high)
{
Expand All @@ -36,10 +40,12 @@ public function __construct($low, $high)
}

/**
* @param int[] $interval
* @return NumericInterval
* @phpstan-ignore-next-line
* @psalm-template TPoint of int|float
* @param TPoint[] $interval
* @return IntervalInterface<TPoint>
*/
public static function fromArray($interval): NumericInterval
public static function fromArray(array $interval): IntervalInterface
{
if (count($interval) !== 2) {
throw new InvalidArgumentException('Wrong interval array');
Expand All @@ -48,54 +54,54 @@ public static function fromArray($interval): NumericInterval
}

/**
* @return int|float
* @return TPoint
*/
public function getLow()
{
return $this->low;
}

/**
* @return int|float
* @return TPoint
*/
public function getHigh()
{
return $this->high;
}

/**
* @param NumericInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function equalTo($otherInterval): bool
public function equalTo(IntervalInterface $otherInterval): bool
{
return $this->getLow() === $otherInterval->getLow() && $this->getHigh() === $otherInterval->getHigh();
}

/**
* @param NumericInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function lessThan($otherInterval): bool
public function lessThan(IntervalInterface $otherInterval): bool
{
return $this->getLow() < $otherInterval->getLow() ||
($this->getLow() === $otherInterval->getLow() && $this->getHigh() < $otherInterval->getHigh());
}

/**
* @param NumericInterval $otherInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return bool
*/
public function intersect($otherInterval): bool
public function intersect(IntervalInterface $otherInterval): bool
{
return !($this->getHigh() < $otherInterval->getLow() || $otherInterval->getHigh() < $this->getLow());
}

/**
* @param NumericInterval $otherInterval
* @return NumericInterval
* @param IntervalInterface<TPoint> $otherInterval
* @return IntervalInterface<TPoint>
*/
public function merge($otherInterval): NumericInterval
public function merge(IntervalInterface $otherInterval): IntervalInterface
{
return new NumericInterval(
min($this->getLow(), $otherInterval->getLow()),
Expand Down
Loading