AbstractCacheConstraintTrait::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Test\PHPUnit;
13
14
use PHPUnit\Runner\Version;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Runner\Version was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * This trait is used to have the same code and behavior between AbstractCacheConstraint and its legacy version.
19
 */
20
trait AbstractCacheConstraintTrait
21
{
22
    protected $header = 'X-Cache';
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param string $header Cache debug header; defaults to X-Cache-Debug
28
     */
29 29
    public function __construct($header = null)
30
    {
31 29
        if ($header) {
32 3
            $this->header = $header;
33
        }
34
35 29
        if (version_compare(Version::id(), '8.0.0', '<')) {
36
            parent::__construct();
37
        }
38 29
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param ResponseInterface $other The guzzle response object
44
     */
45 29
    public function matches($other): bool
46
    {
47 29
        if (!$other instanceof ResponseInterface) {
0 ignored issues
show
introduced by
$other is always a sub-type of Psr\Http\Message\ResponseInterface.
Loading history...
48
            throw new \RuntimeException(sprintf('Expected a GuzzleHttp\Psr7\Response but got %s', get_class($other)));
49
        }
50 29
        if (!$other->hasHeader($this->header)) {
51 1
            $message = sprintf(
52
                'Response has no "%s" header. Configure your caching proxy '
53 1
                .'to set the header with cache hit/miss status.',
54 1
                $this->header
55
            );
56 1
            if (200 !== $other->getStatusCode()) {
57
                $message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode());
58
            }
59
60 1
            $message .= "\nThe response headers are:\n\n";
61
62 1
            foreach ($other->getHeaders() as $name => $values) {
63
                foreach ($values as $value) {
64
                    $message .= $name.': '.$value."\n";
65
                }
66
            }
67
68 1
            $body = $other->getBody();
69 1
            $body->rewind();
70 1
            $message .= sprintf("\nThe response body is:\n\n %s", $body->getContents());
71
72 1
            throw new \RuntimeException($message);
73
        }
74
75 28
        return false !== strpos((string) $other->getHeaderLine($this->header), $this->getValue());
0 ignored issues
show
Bug introduced by
It seems like getValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        return false !== strpos((string) $other->getHeaderLine($this->header), $this->/** @scrutinizer ignore-call */ getValue());
Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function failureDescription($other): string
82
    {
83 2
        return sprintf(
84 2
            'response (with status code %s) %s',
85 2
            $other->getStatusCode(),
86 2
            $this->toString()
0 ignored issues
show
Bug introduced by
It seems like toString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            $this->/** @scrutinizer ignore-call */ 
87
                   toString()
Loading history...
87
        );
88
    }
89
}
90