Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
34 | class CacheItem implements CacheItemInterface |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * The default timeout. |
||
39 | * |
||
40 | * @var integer |
||
41 | */ |
||
42 | const DEFAULT_TIMEOUT = 1400; |
||
43 | |||
44 | /** |
||
45 | * The cache key. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $key; |
||
50 | |||
51 | /** |
||
52 | * The cached value. |
||
53 | * |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $value; |
||
57 | |||
58 | /** |
||
59 | * The UNIX timestamp with the expiration date. |
||
60 | * |
||
61 | * @var integer |
||
62 | */ |
||
63 | protected $expiry = 0; |
||
64 | |||
65 | /** |
||
66 | * Initializes the cache item with the key. |
||
67 | * |
||
68 | * @param mixed $key The cache key |
||
69 | */ |
||
70 | public function __construct($key) |
||
74 | |||
75 | /** |
||
76 | * Returns the key for the current cache item. |
||
77 | * |
||
78 | * The key is loaded by the Implementing Library, but should be available to |
||
79 | * the higher level callers when needed. |
||
80 | * |
||
81 | * @return string The key string for this cache item. |
||
82 | * @see \Psr\Cache\CacheItemInterface::getKey() |
||
83 | */ |
||
84 | public function getKey() |
||
88 | |||
89 | /** |
||
90 | * Sets the value represented by this cache item. |
||
91 | * |
||
92 | * The $value argument may be any item that can be serialized by PHP, |
||
93 | * although the method of serialization is left up to the Implementing |
||
94 | * Library. |
||
95 | * |
||
96 | * @param mixed $value The serializable value to be stored. |
||
97 | * |
||
98 | * @return static The invoked object. |
||
99 | * @see \Psr\Cache\CacheItemInterface::set() |
||
100 | */ |
||
101 | public function set($value) |
||
106 | |||
107 | /** |
||
108 | * Confirms if the cache item lookup resulted in a cache hit. |
||
109 | * |
||
110 | * Note: This method MUST NOT have a race condition between calling isHit() |
||
111 | * and calling get(). |
||
112 | * |
||
113 | * @return boolean TRUE if the request resulted in a cache hit. FALSE otherwise. |
||
114 | * @see \Psr\Cache\CacheItemInterface::isHit() |
||
115 | */ |
||
116 | public function isHit() |
||
120 | |||
121 | /** |
||
122 | * Retrieves the value of the item from the cache associated with this object's key. |
||
123 | * |
||
124 | * The value returned must be identical to the value originally stored by set(). |
||
125 | * |
||
126 | * If isHit() returns false, this method MUST return null. Note that null |
||
127 | * is a legitimate cached value, so the isHit() method SHOULD be used to |
||
128 | * differentiate between "null value was found" and "no value was found." |
||
129 | * |
||
130 | * @return mixed The value corresponding to this cache item's key, or null if not found. |
||
131 | * @see \Psr\Cache\CacheItemInterface::get() |
||
132 | */ |
||
133 | public function get() |
||
137 | |||
138 | /** |
||
139 | * Sets the expiration time for this cache item. |
||
140 | * |
||
141 | * @param \DateTimeInterface|null $expiration The point in time after which the item MUST be considered expired. If null is passed explicitly, a default value MAY be used. If none is set, the value should be stored permanently or for as long as the implementation allows. |
||
142 | * |
||
143 | * @return static The called object. |
||
144 | * @see \Psr\Cache\CacheItemInterface::expiresAt() |
||
145 | * @throws \InvalidArgumentException |
||
146 | */ |
||
147 | public function expiresAt($expiration) |
||
165 | |||
166 | /** |
||
167 | * Sets the expiration time for this cache item. |
||
168 | * |
||
169 | * @param int|\DateInterval|null $time The period of time from the present after which the item MUST be considered expired. An integer parameter is understood to be the time in seconds until expiration. If null is passed explicitly, a default value MAY be used. If none is set, the value should be stored permanently or for as long as the implementation allows. |
||
170 | * |
||
171 | * @return static The called object. |
||
172 | * @see \Psr\Cache\CacheItemInterface::expiresAfter() |
||
173 | * @throws \InvalidArgumentException |
||
174 | */ |
||
175 | public function expiresAfter($time) |
||
190 | } |
||
191 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.