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 app/Devnull/Data/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(public string $scale = 'year', public array $buckets
*/
public static function fromArray(mixed $data): self
{
if (! is_array($data) || ! array_key_exists($data['scale'] ?? null, self::Windows) || ! is_array($data['buckets'] ?? null)) {
if (! is_array($data) || ! is_array($data['buckets'] ?? null)) {
return new self;
}

Expand All @@ -43,7 +43,7 @@ public static function fromArray(mixed $data): self
}
}

return new self($data['scale'], $buckets);
return new self(in_array($data['scale'] ?? null, array_keys(self::Windows), true) ? $data['scale'] : 'year', $buckets);
}

public function total(): int
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Devnull/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ function activityJson(string $scale, CarbonImmutable $from, string $step, array
expect(Activity::fromArray(activityJson('week', CarbonImmutable::parse('2026-10-09'), 'days', array_fill(0, 7, 0)))->isEmpty())->toBeTrue()
->and(Activity::fromArray(activityJson('year', CarbonImmutable::parse('2025-10-12'), 'days', array_fill(0, 7, 0)))->isEmpty())->toBeFalse();
});

it('draws a scale it does not know as a year rather than dropping the commits', function () {
$activity = Activity::fromArray(activityJson('quarter', CarbonImmutable::parse('2026-08-23'), 'days', [1, ...array_fill(0, 89, 0), 2]));

expect($activity->scale)->toBe('year')
->and($activity->cells())->toHaveCount(13)
->and($activity->total())->toBe(3);
});
Loading