Completed
Pull Request — master (#365)
by Alessandro
12:11
created

AbstractCacheConstraintTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 56
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A matches() 0 20 4
A failureDescription() 0 8 1
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 Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * This trait is used to have the same code and behavior between AbstractCacheConstraint and its legacy version.
18
 */
19
trait AbstractCacheConstraintTrait
20
{
21
    protected $header = 'X-Cache';
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param string $header Cache debug header; defaults to X-Cache-Debug
27
     */
28
    public function __construct($header = null)
29
    {
30
        if ($header) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $header of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
31
            $this->header = $header;
32
        }
33
34
        parent::__construct();
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @param ResponseInterface $other The guzzle response object
41
     */
42
    public function matches($other)
43
    {
44
        if (!$other instanceof ResponseInterface) {
45
            throw new \RuntimeException(sprintf('Expected a GuzzleHttp\Psr7\Response but got %s', get_class($other)));
46
        }
47
        if (!$other->hasHeader($this->header)) {
48
            $message = sprintf(
49
                'Response has no "%s" header. Configure your caching proxy '
50
                .'to set the header with cache hit/miss status.',
51
                $this->header
52
            );
53
            if (200 !== $other->getStatusCode()) {
54
                $message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode());
55
            }
56
57
            throw new \RuntimeException($message);
58
        }
59
60
        return strpos((string) $other->getHeaderLine($this->header), $this->getValue()) !== false;
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function failureDescription($other)
67
    {
68
        return sprintf(
69
            'response (with status code %s) %s',
70
            $other->getStatusCode(),
71
            $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
        );
73
    }
74
}
75