diff --git a/src/WpOrg/Api/Cache.php b/src/WpOrg/Api/Cache.php index 7167178..d658fee 100644 --- a/src/WpOrg/Api/Cache.php +++ b/src/WpOrg/Api/Cache.php @@ -25,19 +25,12 @@ public function read(string $slug): ?bool return null; } - $content = $this->cache->read($key); + $content = (string) $this->cache->read($key); - // Should never happen because of the age check. - if ($content === false) { - return null; - } - - $lines = explode("\n", $content); - - return match ($lines[0]) { - 'closed' => true, - 'open' => false, - default => null, // Unexpected content. Treat as a cache miss. + return match (true) { + str_starts_with($content, 'closed'.PHP_EOL) => true, + str_starts_with($content, 'open'.PHP_EOL) => false, + default => null, // Unexpected content. Treat as a miss. }; } @@ -48,10 +41,13 @@ public function write(string $slug, bool $isClosed): void } $content = sprintf( - "%s\n%s\n%s\n", + "%s%s%s%s%s%s", $isClosed ? 'closed' : 'open', + PHP_EOL, $slug, + PHP_EOL, date(DateTimeInterface::RFC3339), + PHP_EOL ); $this->cache->write( diff --git a/tests/Unit/WpOrg/Api/CacheTest.php b/tests/Unit/WpOrg/Api/CacheTest.php index 6f64a6f..6ada1fd 100644 --- a/tests/Unit/WpOrg/Api/CacheTest.php +++ b/tests/Unit/WpOrg/Api/CacheTest.php @@ -31,7 +31,7 @@ function (string $slug, bool $expected, string $firstLine): void { $composerCache->expects() ->read() ->with("{$slug}.txt") - ->andReturn("{$firstLine}\n{$slug}\n2006-01-02T15:04:05+07:00\n"); + ->andReturn($firstLine.PHP_EOL.$slug.PHP_EOL.'2006-01-02T15:04:05+07:00'.PHP_EOL); $cache = new Cache($composerCache); @@ -89,7 +89,7 @@ function (string $slug, bool $isClosed, string $expected): void { $composerCache->shouldHaveReceived('write', [ "{$slug}.txt", - Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected."\n")), + Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected.PHP_EOL.$slug.PHP_EOL)), ]); })->with('slugs');