GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HtmlCompressMiddleware   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 58
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A handleResponse() 0 20 4
A __invoke() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use React\Http\Io\HttpBodyStream;
8
use React\Promise\PromiseInterface;
9
use function React\Promise\resolve;
10
use function RingCentral\Psr7\stream_for;
11
use WyriHaximus\HtmlCompress\Factory;
12
use WyriHaximus\HtmlCompress\Parser;
13
14
final class HtmlCompressMiddleware
15
{
16
    const MIME_TYPES = [
17
        'text/html',
18
        'text/xhtml',
19
    ];
20
21
    /**
22
     * @var Parser
23
     */
24
    private $compressor;
25
26
    /**
27
     * @param Parser $compressor
28
     */
29 5
    public function __construct(Parser $compressor = null)
30
    {
31 5
        if ($compressor === null) {
32 5
            $compressor = Factory::constructFastest();
33
        }
34
35 5
        $this->compressor = $compressor;
0 ignored issues
show
Documentation Bug introduced by
It seems like $compressor can also be of type object<WyriHaximus\HtmlC...tmlCompressorInterface>. However, the property $compressor is declared as type object<WyriHaximus\HtmlCompress\Parser>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36 5
    }
37
38 5
    public function __invoke(ServerRequestInterface $request, callable $next)
39
    {
40 5
        $response = $next($request);
41
42 5
        if (!($response instanceof PromiseInterface)) {
43 2
            return resolve($this->handleResponse($response));
44
        }
45
46
        return $response->then(function (ResponseInterface $response) {
47 3
            return $this->handleResponse($response);
48 3
        });
49
    }
50
51 5
    private function handleResponse(ResponseInterface $response)
52
    {
53 5
        if ($response->getBody() instanceof HttpBodyStream) {
54 1
            return $response;
55
        }
56
57 4
        if (!$response->hasHeader('content-type')) {
58 1
            return $response;
59
        }
60
61 3
        list($contentType) = \explode(';', $response->getHeaderLine('content-type'));
62 3
        if (!\in_array($contentType, self::MIME_TYPES, true)) {
63 1
            return $response;
64
        }
65
66 2
        $body = (string)$response->getBody();
67 2
        $compressedBody = $this->compressor->compress($body);
68
69 2
        return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', \strlen($compressedBody));
70
    }
71
}
72