Skip to content

Commit 7af3d89

Browse files
committed
use isset instead of in array, code style, fix typo
1 parent 5a59588 commit 7af3d89

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/CacheMiddleware.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CacheMiddleware
5656
*
5757
* @var array
5858
*/
59-
protected $safeMethods = ['GET', 'HEAD', 'OPTIONS', 'TRACE'];
59+
protected $safeMethods = ['GET' => true, 'HEAD' => true, 'OPTIONS' => true, 'TRACE' => true];
6060

6161
/**
6262
* @param CacheStrategyInterface|null $cacheStrategy
@@ -126,7 +126,7 @@ public function __invoke(callable $handler)
126126

127127
return $handler($request, $options)->then(
128128
function (ResponseInterface $response) use ($request) {
129-
if (!in_array($request->getMethod(), $this->safeMethods)) {
129+
if (!isset($this->safeMethods[$request->getMethod()])) {
130130
// Invalidate cache after a call of non-safe method on the same URI
131131
$response = $this->invalidateCache($request, $response);
132132
}
@@ -386,9 +386,7 @@ public static function getMiddleware(CacheStrategyInterface $cacheStorage = null
386386
*/
387387
private function invalidateCache(RequestInterface $request, ResponseInterface $response)
388388
{
389-
$this->cacheStorage->delete($request);
390-
391-
foreach (array_diff(array_keys($this->httpMethods), [$request->getMethod()]) as $method) {
389+
foreach (array_keys($this->httpMethods) as $method) {
392390
$this->cacheStorage->delete($request->withMethod($method));
393391
}
394392

tests/InvalidateCacheTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class InvalidateCacheTest extends TestCase
2222
/**
2323
* @var CacheMiddleware
2424
*/
25-
protected $middelware;
25+
protected $middleware;
2626

2727
protected function setUp(): void
2828
{
@@ -32,11 +32,11 @@ protected function setUp(): void
3232
]));
3333
});
3434

35-
$this->middelware = new CacheMiddleware(new PrivateCacheStrategy(
35+
$this->middleware = new CacheMiddleware(new PrivateCacheStrategy(
3636
new Psr6CacheStorage(new ArrayCachePool()),
3737
));
3838

39-
$stack->push($this->middelware, 'cache');
39+
$stack->push($this->middleware, 'cache');
4040

4141
$this->client = new Client(['handler' => $stack]);
4242
}
@@ -46,7 +46,7 @@ protected function setUp(): void
4646
*/
4747
public function testItInvalidatesForUnsafeHttpMethods($unsafeMethod)
4848
{
49-
$this->middelware->setHttpMethods([
49+
$this->middleware->setHttpMethods([
5050
'GET' => true,
5151
'HEAD' => true,
5252
]);

0 commit comments

Comments
 (0)