Skip to content

Commit 5191a6f

Browse files
committed
fix phpstan issues
1 parent 0d615ee commit 5191a6f

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/ResponseCompression.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
use Closure;
66
use Symfony\Component\HttpFoundation\BinaryFileResponse;
77
use Symfony\Component\HttpFoundation\StreamedResponse;
8+
use Symfony\Component\HttpFoundation\Response;
89

910
class ResponseCompression
1011
{
1112
/**
1213
* Handle an incoming request.
1314
*
1415
* @param \Illuminate\Http\Request $request
15-
* @return mixed
16+
* @param \Closure(\Illuminate\Http\Request): \Symfony\Component\HttpFoundation\Response $next
17+
* @return \Symfony\Component\HttpFoundation\Response
1618
*/
1719
public function handle($request, Closure $next)
1820
{
@@ -22,13 +24,14 @@ public function handle($request, Closure $next)
2224
if ($this->shouldCompressResponse($response) && $compressionAlgorithm !== null) {
2325
[$algo, $function] = $compressionAlgorithm;
2426

25-
$response->setContent(
26-
call_user_func(
27-
$function,
28-
$response->getContent(),
29-
config("response-compression.level.{$algo}", 9)
30-
)
27+
/** @var string $compressedContent */
28+
$compressedContent = call_user_func(
29+
$function,
30+
$response->getContent(),
31+
config("response-compression.level.{$algo}", 9)
3132
);
33+
34+
$response->setContent($compressedContent);
3235

3336
$responseHeaders = [
3437
'Content-Encoding' => $algo,
@@ -46,15 +49,26 @@ public function handle($request, Closure $next)
4649
/**
4750
* Determine if response should be compressed.
4851
*
49-
* @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response
52+
* @param \Symfony\Component\HttpFoundation\Response $response
5053
*/
5154
protected function shouldCompressResponse($response): bool
5255
{
53-
return ! $response instanceof BinaryFileResponse
54-
&& ! $response instanceof StreamedResponse
55-
&& ! $response->headers->has('Content-Encoding')
56-
&& config('response-compression.enable', true)
57-
&& strlen($response->getContent() ?: '') >= config('response-compression.threshold', 10000);
56+
if (
57+
$response instanceof BinaryFileResponse
58+
|| $response instanceof StreamedResponse
59+
|| !config('response-compression.enable', true)
60+
) {
61+
return false;
62+
}
63+
64+
if (
65+
! $response->headers->has('Content-Encoding')
66+
&& strlen($response->getContent() ?: '') >= config('response-compression.threshold', 10000)
67+
) {
68+
return true;
69+
}
70+
71+
return false;
5872
}
5973

6074
/**

0 commit comments

Comments
 (0)