Completed
Push — master ( e6a348...ceafb8 )
by Martin
12:34
created

CacheItem::expiresAt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 0
cts 0
cp 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
namespace Psr6NullCache;
3
4
use Psr\Cache\CacheItemInterface;
5
use DateTimeInterface;
6
use DateInterval;
7
use DateTime;
8
9
final class CacheItem implements CacheItemInterface
10
{
11
12
    /**
13
     *
14
     * @var string
15
     */
16
    private $key;
17
18
    /**
19
     *
20
     * @var mixed
21
     */
22
    private $value;
23
24
    /**
25
     *
26
     * @var boolean
27 5
     */
28
    private $isHit;
29 5
30 5
    /**
31 5
     *
32 5
     * @var null DateTimeInterface
33
     */
34
    private $expires;
35
36
    public function __construct($key, $value, $isHit, DateTimeInterface $expires = null)
37
    {
38
        $this->key = $key;
39
        $this->value = $value;
40
        $this->isHit = (bool) $isHit;
41
        $this->expires = $expires;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expires can also be of type object<DateTimeInterface>. However, the property $expires is declared as type null. 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...
42 2
    }
43
44 2
    /**
45
     * Returns the key for the current cache item.
46
     *
47
     * The key is loaded by the Implementing Library, but should be available to
48
     * the higher level callers when needed.
49
     *
50
     * @return string The key string for this cache item.
51
     */
52
    public function getKey()
53
    {
54
        return $this->key;
55
    }
56
57
    /**
58 3
     * Retrieves the value of the item from the cache associated with this object's key.
59
     *
60 3
     * The value returned must be identical to the value originally stored by set().
61
     *
62
     * If isHit() returns false, this method MUST return null. Note that null
63
     * is a legitimate cached value, so the isHit() method SHOULD be used to
64
     * differentiate between "null value was found" and "no value was found."
65
     *
66
     * @return mixed The value corresponding to this cache item's key, or null if not found.
67
     */
68
    public function get()
69
    {
70
        if ($this->isHit() !== true) {
71 2
            return null;
72
        }
73 2
        
74
        return $this->value;
75
    }
76
77
    public function setIsHit($mode = true)
78
    {
79
        $this->isHit = $mode;
80
    }
81
82
    /**
83
     * Confirms if the cache item lookup resulted in a cache hit.
84
     *
85
     * Note: This method MUST NOT have a race condition between calling isHit()
86
     * and calling get().
87
     *
88 1
     * @return bool True if the request resulted in a cache hit. False otherwise.
89
     */
90 1
    public function isHit()
91
    {
92 1
        return $this->isHit;
93
    }
94
95
    /**
96
     *
97
     * @return DateTimeInterface null
98
     */
99
    public function getExpires()
100
    {
101
        return $this->expires;
102
    }
103
104
    /**
105
     * Sets the value represented by this cache item.
106 1
     *
107
     * The $value argument may be any item that can be serialized by PHP,
108 1
     * although the method of serialization is left up to the Implementing
109
     * Library.
110
     *
111
     * @param mixed $value
112
     *            The serializable value to be stored.
113
     *            
114
     * @return static The invoked object.
115
     */
116
    public function set($value)
117
    {
118
        $this->value = $value;
119
        
120
        return $this;
121
    }
122
123 1
    /**
124
     * Sets the expiration time for this cache item.
125 1
     *
126
     * @param \DateTimeInterface $expires
127
     *            The point in time after which the item MUST be considered expired.
128
     *            If null is passed explicitly, a default value MAY be used. If none is set,
129
     *            the value should be stored permanently or for as long as the
130
     *            implementation allows.
131
     *            
132
     * @return static The called object.
133
     */
134
    public function expiresAt($expires)
135
    {
136
        if ($expires instanceof DateTimeInterface) {
137
            $this->expires = $expires;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expires of type object<DateTimeInterface> is incompatible with the declared type null of property $expires.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
        } else {
139
            $this->expires = null;
140
        }
141
        
142
        return $this;
143
    }
144
145
    /**
146
     * Sets the expiration time for this cache item.
147
     *
148
     * @param int|\DateInterval $time
149
     *            The period of time from the present after which the item MUST be considered
150
     *            expired. An integer parameter is understood to be the time in seconds until
151
     *            expiration. If null is passed explicitly, a default value MAY be used.
152
     *            If none is set, the value should be stored permanently or for as long as the
153
     *            implementation allows.
154
     *            
155
     * @return static The called object.
156
     */
157
    public function expiresAfter($time)
158
    {
159
        if ($time instanceof DateInterval) {
160
            $expires = new DateTime();
161
            $expires->add($time);
162
            
163
            $this->expires = $expires;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expires of type object<DateTime> is incompatible with the declared type null of property $expires.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
164
        } elseif (is_numeric($time)) {
165
            $expires = new DateTime('now +' . $time . ' seconds');
166
            
167
            $this->expires = $expires;
168
        } else {
169
            $this->expires = null;
170
        }
171
        
172
        return $this;
173
    }
174
}
175