Skip to content

Commit 25623d8

Browse files
authored
Code optimisations for PHP8 (#45)
1 parent c811206 commit 25623d8

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/Psr6Store.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Psr6Store implements Psr6StoreInterface, ClearableInterface
6060
private string $hashAlgorithm;
6161

6262
/**
63-
* When creating a Psr6Store you can configure a number options.
63+
* When creating a Psr6Store you can configure a number of options.
6464
* See the README for a list of all available options and their description.
6565
*/
6666
public function __construct(array $options = [])
@@ -100,7 +100,7 @@ public function __construct(array $options = [])
100100
$this->options = $resolver->resolve($options);
101101
$this->cache = $this->options['cache'];
102102
$this->lockFactory = $this->options['lock_factory'];
103-
$this->hashAlgorithm = version_compare(PHP_VERSION, '8.1.0', '>=') ? 'xxh128' : 'sha256';
103+
$this->hashAlgorithm = \PHP_VERSION_ID >= 80100 ? 'xxh128' : 'sha256';
104104
}
105105

106106
public function lookup(Request $request): ?Response
@@ -227,7 +227,7 @@ public function unlock(Request $request): bool
227227

228228
try {
229229
$this->locks[$cacheKey]->release();
230-
} catch (LockReleasingException $e) {
230+
} catch (LockReleasingException) {
231231
return false;
232232
} finally {
233233
unset($this->locks[$cacheKey]);
@@ -260,7 +260,7 @@ public function cleanup(): void
260260
foreach ($this->locks as $lock) {
261261
$lock->release();
262262
}
263-
} catch (LockReleasingException $e) {
263+
} catch (LockReleasingException) {
264264
// noop
265265
} finally {
266266
$this->locks = [];
@@ -281,7 +281,7 @@ public function invalidateTags(array $tags): bool
281281

282282
try {
283283
return $this->cache->invalidateTags($tags);
284-
} catch (CacheInvalidArgumentException $e) {
284+
} catch (CacheInvalidArgumentException) {
285285
return false;
286286
}
287287
}
@@ -508,13 +508,13 @@ private function restoreResponse(array $cacheData): ?Response
508508

509509
// BC
510510
if (\is_string($value)) {
511-
$value = ['expires' => 0, 'contents' => $value];
511+
$value = ['contents' => $value];
512512
}
513513

514514
if ($this->isBinaryFileResponseContentDigest($cacheData['headers']['x-content-digest'][0])) {
515515
try {
516516
$file = new File($value['contents']);
517-
} catch (FileNotFoundException $e) {
517+
} catch (FileNotFoundException) {
518518
return null;
519519
}
520520

@@ -542,7 +542,7 @@ private function getDefaultLockStore(string $cacheDir): PersistingStoreInterface
542542
{
543543
try {
544544
return new SemaphoreStore();
545-
} catch (LockInvalidArgumentException $exception) {
545+
} catch (LockInvalidArgumentException) {
546546
return new FlockStore($cacheDir);
547547
}
548548
}

tests/Psr6StoreTest.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333

3434
class Psr6StoreTest extends TestCase
3535
{
36-
/**
37-
* @var Psr6Store
38-
*/
39-
private $store;
36+
private Psr6Store $store;
4037

4138
protected function setUp(): void
4239
{
@@ -334,7 +331,7 @@ public function testRegularCacheKey(): void
334331
{
335332
$request = Request::create('https://foobar.com/');
336333
$expected = 'md'.hash(
337-
version_compare(PHP_VERSION, '8.1.0', '>=')
334+
\PHP_VERSION_ID >= 80100
338335
? 'xxh128'
339336
: 'sha256',
340337
'foobar.com/'
@@ -381,7 +378,7 @@ public function testRegularLookup(): void
381378
$this->assertSame('whatever', $result->headers->get('Foobar'));
382379

383380
$this->assertSame(
384-
version_compare(PHP_VERSION, '8.1.0', '>=')
381+
\PHP_VERSION_ID >= 80100
385382
? 'endf8d09e93f874900a99b8775cc15b6c7' // xxh128
386383
: 'enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' // sha256
387384
, $result->headers->get('X-Content-Digest'));
@@ -535,7 +532,7 @@ public function testLookupWithMultipleVaryResponse(): void
535532
$result = $this->store->lookup($request);
536533
$this->assertNull($result);
537534

538-
// Should return null because header provided but non matching content
535+
// Should return null because header provided but non-matching content
539536
$request = Request::create('https://foobar.com/');
540537
$request->headers->set('Accept', 'application/xml');
541538
$result = $this->store->lookup($request);
@@ -862,20 +859,20 @@ public function testContentDigestExpiresCorrectly(array $responseHeaders, $expec
862859
->withConsecutive(
863860
[
864861
// content digest
865-
version_compare(PHP_VERSION, '8.1.0', '>=')
862+
\PHP_VERSION_ID >= 80100
866863
? 'en3c9e102628997f44ac87b0b131c6992d' // xxh128
867864
: 'enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2' // sha256
868865
],
869866
[
870867
// meta
871-
version_compare(PHP_VERSION, '8.1.0', '>=')
868+
\PHP_VERSION_ID >= 80100
872869
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
873870
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
874871
],
875872
[Psr6Store::COUNTER_KEY], // write counter
876873
[
877874
// meta again
878-
version_compare(PHP_VERSION, '8.1.0', '>=')
875+
\PHP_VERSION_ID >= 80100
879876
? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128
880877
: 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256
881878
]

0 commit comments

Comments
 (0)