Skip to content

Commit

Permalink
Accept wider range of metadata as input
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMuller committed Jun 21, 2024
1 parent e78bdc6 commit ae9777e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function start(): self
}

/**
* @param array{int|string, int|float|string|null|bool|Stringable}|null $metadata
* @param array{int|string, mixed}|null $metadata
*/
public function checkpoint(string $label, ?array $metadata = null): self
{
Expand All @@ -46,7 +46,7 @@ public function checkpoint(string $label, ?array $metadata = null): self
/**
* @alias
* @see self::checkpoint()
* @param array{int|string, int|float|string|null|bool|Stringable}|null $metadata
* @param array{int|string, mixed}|null $metadata
*/
public function lap(string $label, ?array $metadata = null): self
{
Expand Down
12 changes: 9 additions & 3 deletions src/StopwatchCheckpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public string $timeSinceLastCheckpointFormatted;

/**
* @param array{int|string, int|float|string|null|bool|Stringable}|null $metadata
* @param array{int|string, mixed}|null $metadata
*/
public function __construct(
public string $label,
Expand Down Expand Up @@ -91,7 +91,13 @@ private function renderMetadata(): string
}

$contents = collect($this->metadata)
->implode(static fn (int|float|string|null|bool|Stringable $value, string|int $key): string => "<strong>{$key}:</strong> {$value}<br/>");
->implode(static function (mixed $value, string|int $key): string {
if (! is_scalar($value) && ! $value instanceof Stringable) {
$value = 'non-scalar value (' . gettype($value) . ')';
}

return "<strong>{$key}:</strong> {$value}<br/>";
});

return <<<HTML
<div style="padding: 5px 10px; margin: 5px 0 0; background-color: #fcfcfc; border: 1px solid rgb(243 244 246); border-radius: 5px; line-height: 1.2;">
Expand All @@ -104,7 +110,7 @@ private function renderMetadata(): string
* @return array{
* label: string,
* time: string,
* metadata: array{int|string, int|float|string|null|bool|Stringable}|null,
* metadata: array{int|string, mixed}|null,
* totalTimeElapsedMs: int,
* totalTimeElapsedFormatted: string,
* timeSinceLastCheckpointMs: int,
Expand Down
3 changes: 1 addition & 2 deletions src/StopwatchCheckpointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use Stringable;

/**
* @extends Collection<array-key, StopwatchCheckpoint>
Expand All @@ -14,7 +13,7 @@
final class StopwatchCheckpointCollection extends Collection
{
/**
* @param array{int|string, int|float|string|null|bool|Stringable}|null $metadata
* @param array{int|string, mixed}|null $metadata
*/
public function addCheckpoint(string $label, ?array $metadata, CarbonImmutable $stopwatchStartTime): self
{
Expand Down

0 comments on commit ae9777e

Please sign in to comment.