Completed
Pull Request — master (#332)
by Alexander M.
34:09
created

CacheItem   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 92
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isHit() 0 3 1
A getKey() 0 3 1
A __construct() 0 5 2
A get() 0 3 1
A set() 0 5 1
A expiresAt() 0 12 4
A expiresAfter() 0 16 5
A getExpiration() 0 3 1
1
<?php
2
3
namespace Doctrine\Common\Cache\Psr6;
4
5
use Psr\Cache\CacheItemInterface;
6
7
final class CacheItem implements CacheItemInterface
8
{
9
    /** @var string */
10
    private $key;
11
    /** @var mixed */
12
    private $value;
13
    /** @var bool */
14
    private $hit;
15
    /** @var \DateTimeInterface|null */
0 ignored issues
show
introduced by
Class \DateTimeInterface should not be referenced via a fully qualified name, but via a use statement.
Loading history...
16
    private $expiration;
17
18
    public function __construct(string $key, $data)
19
    {
20
        $this->key = $key;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21
        $this->value = false === $data ? null : $data;
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
22
        $this->hit = false !== $data;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Yoda comparisons are disallowed.
Loading history...
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function getKey(): string
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
29
    {
30
        return $this->key;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function get()
37
    {
38
        return $this->value;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function isHit(): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
45
    {
46
        return $this->hit;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function set($value): self
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
53
    {
54
        $this->value = $value;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function expiresAt($expiration): self
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
63
    {
64
        if (null !== $expiration && !$expiration instanceof \DateTimeInterface) {
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Class \DateTimeInterface should not be referenced via a fully qualified name, but via a use statement.
Loading history...
65
            throw new \TypeError(sprintf(
0 ignored issues
show
introduced by
Class \TypeError should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function sprintf() should not be referenced via a fallback global name, but via a use statement.
Loading history...
66
                'Expected $expiration to be an instance of DateTimeInterface or null, got %s',
67
                \is_object($expiration) ? \get_class($expiration) : \gettype($expiration)
0 ignored issues
show
introduced by
Function \is_object() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function \get_class() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function \gettype() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
68
            ));
69
        }
70
71
        $this->expiration = $expiration;
72
73
        return $this;
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
75
    /**
76
     * @inheritDoc
77
     */
78
    public function expiresAfter($time): self
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
79
    {
80
        if (null === $time) {
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
81
            $this->expiration = null;
82
        } elseif (is_numeric($time)) {
0 ignored issues
show
introduced by
Function is_numeric() should not be referenced via a fallback global name, but via a use statement.
Loading history...
83
            $this->expiration = new \DateTimeImmutable(sprintf('now +%d seconds', $time));
0 ignored issues
show
introduced by
Class \DateTimeImmutable should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function sprintf() should not be referenced via a fallback global name, but via a use statement.
Loading history...
84
        } elseif ($time instanceof \DateInterval) {
0 ignored issues
show
introduced by
Class \DateInterval should not be referenced via a fully qualified name, but via a use statement.
Loading history...
85
            $this->expiration = (new \DateTimeImmutable())->add($time);
0 ignored issues
show
introduced by
Class \DateTimeImmutable should not be referenced via a fully qualified name, but via a use statement.
Loading history...
86
        } else {
87
            throw new \TypeError(sprintf(
0 ignored issues
show
introduced by
Class \TypeError should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function sprintf() should not be referenced via a fallback global name, but via a use statement.
Loading history...
88
                'Expected $time to be either an integer, an instance of DateInterval or null, got %s',
89
                \is_object($time) ? \get_class($time) : \gettype($time)
0 ignored issues
show
introduced by
Function \is_object() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function \get_class() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Function \gettype() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
90
            ));
91
        }
92
93
        return $this;
94
    }
95
96
    public function getExpiration(): ?\DateTimeInterface
0 ignored issues
show
introduced by
Class \DateTimeInterface should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
97
    {
98
        return $this->expiration;
99
    }
100
}
101