Skip to content

Commit d4db7fa

Browse files
committed
Simplify accepted type logic
1 parent 3c7a2a0 commit d4db7fa

15 files changed

Lines changed: 226 additions & 235 deletions

src/AcceptTypes.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bakame\Tokei;
6+
7+
use DateInterval;
8+
use DateTimeInterface;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class AcceptTypes
14+
{
15+
public static function time(Time|Event|NativeEvent|DateTimeInterface $time): Time
16+
{
17+
return match (true) {
18+
$time instanceof NativeEvent => Event::fromNative($time)->at,
19+
$time instanceof Event => $time->at,
20+
$time instanceof DateTimeInterface => Time::fromDateTime($time),
21+
$time instanceof Time => $time,
22+
};
23+
}
24+
25+
public static function duration(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $duration): Duration
26+
{
27+
return match (true) {
28+
$duration instanceof NativeInterval,
29+
$duration instanceof NativeTask,
30+
$duration instanceof Task,
31+
$duration instanceof Interval => self::interval($duration)->duration,
32+
$duration instanceof DateInterval => Duration::fromDateInterval($duration),
33+
$duration instanceof Duration => $duration,
34+
};
35+
}
36+
37+
public static function interval(Interval|Task|NativeTask|NativeInterval $interval): Interval
38+
{
39+
return match (true) {
40+
$interval instanceof NativeInterval => Interval::fromNative($interval),
41+
$interval instanceof NativeTask => Task::fromNative($interval)->interval,
42+
$interval instanceof Task => $interval->interval,
43+
$interval instanceof Interval => $interval,
44+
};
45+
}
46+
}

src/Duration.php

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121

2222
final readonly class Duration implements JsonSerializable
2323
{
24-
public int $value;
24+
public int $microseconds;
2525
public int $sign;
2626

2727
/**
28-
* @param int $value expressed in microseconds
28+
* @param int $microseconds expressed in microseconds
2929
*
3030
* @throws InvalidDuration
3131
*/
32-
private function __construct(int $value)
32+
private function __construct(int $microseconds)
3333
{
34-
($value > PHP_INT_MIN + 1 && $value < PHP_INT_MAX) || throw InvalidDuration::dueToOverflow();
34+
($microseconds > PHP_INT_MIN + 1 && $microseconds < PHP_INT_MAX) || throw InvalidDuration::dueToOverflow();
3535

36-
$this->value = $value;
37-
$this->sign = $this->value <=> 0;
36+
$this->microseconds = $microseconds;
37+
$this->sign = $this->microseconds <=> 0;
3838
}
3939

4040
/**
@@ -172,27 +172,16 @@ public static function maxOf(self ...$durations): self
172172
return null !== $value ? $value : throw new ValueError('maxOf() expects at least one duration');
173173
}
174174

175-
private static function extractDuration(self|Interval|Task|NativeInterval|NativeTask $that): self
176-
{
177-
return match (true) {
178-
$that instanceof NativeInterval => Interval::fromNative($that)->duration,
179-
$that instanceof NativeTask => Task::fromNative($that)->interval->duration,
180-
$that instanceof Task => $that->interval->duration,
181-
$that instanceof Interval => $that->duration,
182-
$that instanceof self => $that,
183-
};
184-
}
185-
186175
/**
187176
* Compare this instance with another.
188177
*
189178
* @return int<-1, 1> If this duration is shorter, equal, or longer than the given duration.
190179
*/
191180
public static function compare(
192-
self|Interval|Task|NativeInterval|NativeTask $that,
193-
self|Interval|Task|NativeInterval|NativeTask $other
181+
Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $that,
182+
Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other
194183
): int {
195-
return self::extractDuration($that)->value <=> self::extractDuration($other)->value;
184+
return AcceptTypes::duration($that)->microseconds <=> AcceptTypes::duration($other)->microseconds;
196185
}
197186

198187
/**
@@ -210,7 +199,7 @@ public function format(DurationFormat $format = DurationFormat::Iso8601): string
210199
*/
211200
public function toDateInterval(?DateTimeInterface $relativeTo = null): DateInterval
212201
{
213-
$parsed = UnitTransformer::decompose($this->value);
202+
$parsed = UnitTransformer::decompose($this->microseconds);
214203
$interval = new DateInterval('PT0S');
215204
$interval->d = $parsed->daysCount;
216205
$interval->h = $parsed->hours % 24;
@@ -234,9 +223,9 @@ public function toDateInterval(?DateTimeInterface $relativeTo = null): DateInter
234223
/**
235224
* Returns the Duration as expressed in the specified Unit of time.
236225
*/
237-
public function total(Unit $unit): int|float
226+
public function in(Unit $unit): int|float
238227
{
239-
return UnitTransformer::fromMicroseconds($this->value, $unit);
228+
return UnitTransformer::fromMicroseconds($this->microseconds, $unit);
240229
}
241230

242231
/**
@@ -254,7 +243,7 @@ public function jsonSerialize(): string
254243
*/
255244
public function isZero(): bool
256245
{
257-
return 0 === $this->value;
246+
return 0 === $this->microseconds;
258247
}
259248

260249
/**
@@ -264,38 +253,38 @@ public function isZero(): bool
264253
*/
265254
public function negated(): self
266255
{
267-
return new self(-$this->value);
256+
return new self(-$this->microseconds);
268257
}
269258

270259
/**
271260
* @throws InvalidDuration
272261
*/
273262
public function abs(): self
274263
{
275-
return $this->value < 0 ? $this->negated() : $this;
264+
return $this->microseconds < 0 ? $this->negated() : $this;
276265
}
277266

278267
/**
279268
* Returns a new instance rounded to the specified unit using a rounding mode.
280269
*/
281270
public function roundTo(Unit $unit, SnapMode $mode = SnapMode::Nearest): self
282271
{
283-
$rounded = UnitTransformer::round($this->value, $unit, $mode);
272+
$rounded = UnitTransformer::round($this->microseconds, $unit, $mode);
284273

285-
return $this->value === $rounded ? $this : new self($rounded);
274+
return $this->microseconds === $rounded ? $this : new self($rounded);
286275
}
287276

288277
/**
289278
* @throws InvalidDuration
290279
*/
291-
public function sum(self|Interval|Task|NativeInterval|NativeTask ...$other): self
280+
public function sum(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask ...$other): self
292281
{
293-
$other = array_map(self::extractDuration(...), $other);
282+
$other = array_map(AcceptTypes::duration(...), $other);
294283
$other[] = $this;
295-
$value = array_sum(array_column($other, 'value'));
296-
is_int($value) || throw InvalidDuration::dueToOverflow(); /* @phpstan-ignore-line */
284+
$microseconds = array_sum(array_column($other, 'microseconds'));
285+
is_int($microseconds) || throw InvalidDuration::dueToOverflow(); /* @phpstan-ignore-line */
297286

298-
return $this->value === $value ? $this : new self($value);
287+
return $this->microseconds === $microseconds ? $this : new self($microseconds);
299288
}
300289

301290
/**
@@ -363,26 +352,26 @@ public function decrease(
363352
/**
364353
* Tells whether this instance is equal to the specified duration.
365354
*/
366-
public function equals(self $other): bool
355+
public function equals(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): bool
367356
{
368357
return 0 === self::compare($this, $other);
369358
}
370359

371-
public function isLongerThan(self $other): bool
360+
public function isLongerThan(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): bool
372361
{
373362
return 0 < self::compare($this, $other);
374363
}
375364

376-
public function isLongerThanOrEqual(self $other): bool
365+
public function isLongerThanOrEqual(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): bool
377366
{
378367
return 0 <= self::compare($this, $other);
379368
}
380-
public function isShorterThan(self $other): bool
369+
public function isShorterThan(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): bool
381370
{
382371
return 0 > self::compare($this, $other);
383372
}
384373

385-
public function isShorterThanOrEqual(self $other): bool
374+
public function isShorterThanOrEqual(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): bool
386375
{
387376
return 0 >= self::compare($this, $other);
388377
}
@@ -395,11 +384,11 @@ public function isShorterThanOrEqual(self $other): bool
395384
* @throws InvalidDuration
396385
*/
397386
public function clamp(
398-
self|Interval|Task|NativeInterval|NativeTask $min,
399-
self|Interval|Task|NativeInterval|NativeTask $max
387+
Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $min,
388+
Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $max
400389
): self {
401-
$max = self::extractDuration($max);
402-
$min = self::extractDuration($min);
390+
$max = AcceptTypes::duration($max);
391+
$min = AcceptTypes::duration($min);
403392

404393
$max->isLongerThanOrEqual($min) || throw new InvalidDuration('The maximum duration must be longer or equal to the minimum duration.');
405394

@@ -419,7 +408,7 @@ public function multipliedBy(int $factor): self
419408
{
420409
0 <= $factor || throw new InvalidDuration('factor must be a non negative integer.'); /* @phpstan-ignore-line */
421410

422-
$result = $this->value * $factor;
411+
$result = $this->microseconds * $factor;
423412

424413
is_int($result) || throw InvalidDuration::dueToOverflow(); /* @phpstan-ignore-line */
425414

@@ -439,15 +428,15 @@ public function dividedBy(int $factor): self
439428
{
440429
0 < $factor || throw new InvalidDuration('factor must be a positive integer.'); /* @phpstan-ignore-line */
441430

442-
return new self(intdiv($this->value, $factor));
431+
return new self(intdiv($this->microseconds, $factor));
443432
}
444433

445-
public function times(self|Interval|Task|NativeInterval|NativeTask $other): int
434+
public function countOf(Duration|DateInterval|Interval|Task|NativeInterval|NativeTask $other): int
446435
{
447-
$other = self::extractDuration($other);
436+
$other = AcceptTypes::duration($other);
448437

449438
return !$other->isZero()
450-
? intdiv($this->value, $other->value)
439+
? intdiv($this->microseconds, $other->microseconds)
451440
: throw new InvalidDuration('Cannot divide by zero duration.');
452441
}
453442

@@ -456,7 +445,7 @@ public function times(self|Interval|Task|NativeInterval|NativeTask $other): int
456445
*/
457446
public function __serialize(): array
458447
{
459-
return [['microseconds' => $this->value], []];
448+
return [['microseconds' => $this->microseconds], []];
460449
}
461450

462451
/**
@@ -468,7 +457,7 @@ public function __unserialize(array $data): void
468457
{
469458
[$properties] = $data;
470459
$duration = new self($properties['microseconds']);
471-
$this->value = $duration->value;
460+
$this->microseconds = $duration->microseconds;
472461
$this->sign = $duration->sign;
473462
}
474463
}

src/DurationFormat.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function fromIso8601(string $data): Duration
180180
*/
181181
private static function toTimer(Duration $duration): string
182182
{
183-
$value = $duration->value;
183+
$value = $duration->microseconds;
184184

185185
$abs = $value < 0 ? -$value : $value;
186186
$hours = UnitTransformer::whole($abs, Unit::Hour);
@@ -210,7 +210,7 @@ private static function toTimer(Duration $duration): string
210210
*/
211211
private static function toIso8601(Duration $duration): string
212212
{
213-
$value = $duration->value;
213+
$value = $duration->microseconds;
214214
$sign = -1 === $duration->sign ? '-' : '';
215215

216216
$abs = $value < 0 ? -$value : $value;
@@ -257,7 +257,7 @@ private static function toIso8601(Duration $duration): string
257257
*/
258258
private static function toCompact(Duration $duration): string
259259
{
260-
$parsed = UnitTransformer::decompose($duration->value);
260+
$parsed = UnitTransformer::decompose($duration->microseconds);
261261
$time = [];
262262
if (0 !== $parsed->weeksCount) {
263263
$time[] = $parsed->weeksCount.'w';

src/DurationTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testParseMicroseconds(): void
3232
$duration = Duration::of(weeks:5, days:6, hours: 2, minutes: 15, seconds: 42, microseconds: 123_456);
3333

3434
self::assertSame(1, $duration->sign);
35-
self::assertSame(3_550_542_123_456, $duration->total(Unit::Microsecond));
35+
self::assertSame(3_550_542_123_456, $duration->microseconds);
3636
self::assertSame('5w6d2h15m42s123456µs', $duration->format(DurationFormat::Compact));
3737
}
3838

@@ -112,7 +112,7 @@ public function testZeroMicroseconds(): void
112112

113113
self::assertSame('00:00:00', $duration->format(DurationFormat::Timer));
114114
self::assertSame(0, $duration->sign);
115-
self::assertSame(0, $duration->value);
115+
self::assertSame(0, $duration->microseconds);
116116
self::assertSame('0s', $duration->format(DurationFormat::Compact));
117117
self::assertTrue($duration->isZero());
118118
self::assertEquals($duration, Duration::zero());
@@ -228,7 +228,7 @@ public function test_truncate_to_precision(
228228
$expectedMicroseconds,
229229
$duration
230230
->roundTo($unit, SnapMode::Floor)
231-
->total(Unit::Microsecond),
231+
->microseconds,
232232
);
233233
}
234234

@@ -307,8 +307,8 @@ public function test_truncate_preserves_sign_consistency(): void
307307
$positive = Duration::of(microseconds:3_661_500_000);
308308
$negative = Duration::of(microseconds:3_661_500_000)->negated();
309309

310-
self::assertTrue($positive->roundTo(Unit::Minute, SnapMode::Floor)->total(Unit::Microsecond) > 0);
311-
self::assertTrue($negative->roundTo(Unit::Minute, SnapMode::Floor)->total(Unit::Microsecond) < 0);
310+
self::assertTrue($positive->roundTo(Unit::Minute, SnapMode::Floor)->microseconds > 0);
311+
self::assertTrue($negative->roundTo(Unit::Minute, SnapMode::Floor)->microseconds < 0);
312312
}
313313

314314
public function test_it_can_not_invert_php_int_max(): void
@@ -693,7 +693,7 @@ public function test_round_to(int $input, Unit $precision, int $expected): void
693693
? Duration::of(microseconds: -$input)->negated()
694694
: Duration::of(microseconds: $input);
695695

696-
self::assertSame($expected, $duration->roundTo($precision)->total(Unit::Microsecond));
696+
self::assertSame($expected, $duration->roundTo($precision)->microseconds);
697697
}
698698

699699
/**
@@ -841,7 +841,7 @@ public static function clampProvider(): array
841841
#[DataProvider('validIntervalsProvider')]
842842
public function testFromDateIntervalConvertsCorrectly(DateInterval $interval, int $expectedMicroseconds): void
843843
{
844-
self::assertSame($expectedMicroseconds, Duration::fromDateInterval($interval)->total(Unit::Microsecond));
844+
self::assertSame($expectedMicroseconds, Duration::fromDateInterval($interval)->microseconds);
845845
}
846846

847847
/**

src/Event.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ private function __construct(
2525
public static function at(Time|NativeEvent|DateTimeInterface|Event $time, Identifiers|HasIdentifiers|string $identifier = new Identifiers()): self
2626
{
2727
return new self(
28-
match (true) {
29-
$time instanceof DateTimeInterface => Time::fromDateTime($time),
30-
$time instanceof NativeEvent => Event::fromNative($time)->at,
31-
$time instanceof Event => $time->at,
32-
default => $time,
33-
},
28+
AcceptTypes::time($time),
3429
match (true) {
3530
$identifier instanceof HasIdentifiers => $identifier->identifiers,
3631
$identifier instanceof Identifiers => $identifier,
@@ -87,12 +82,7 @@ public function equals(Event $other): bool
8782

8883
public function occursOn(Time|NativeEvent|DateTimeInterface|Event $at): self
8984
{
90-
$at = match (true) {
91-
$at instanceof DateTimeInterface => Time::fromDateTime($at),
92-
$at instanceof NativeEvent => Event::fromNative($at)->at,
93-
$at instanceof Event => $at->at,
94-
default => $at,
95-
};
85+
$at = AcceptTypes::time($at);
9686

9787
return $at->equals($this->at) ? $this : new self($at, $this->identifiers);
9888
}

0 commit comments

Comments
 (0)