@@ -144,43 +144,30 @@ public function deleteMatching(string $pattern)
144144 */
145145 public function increment (string $ key , int $ offset = 1 )
146146 {
147- $ key = static ::validateKey ($ key , $ this ->prefix );
148- $ data = $ this ->getItem ($ key );
147+ $ key = static ::validateKey ($ key , $ this ->prefix );
148+ $ tmp = $ this ->getItem ($ key );
149149
150- if ($ data === false ) {
151- $ data = [
152- 'data ' => 0 ,
153- 'ttl ' => 60 ,
154- ];
155- } elseif (! is_int ($ data ['data ' ])) {
150+ if ($ tmp === false ) {
151+ $ tmp = ['data ' => 0 , 'ttl ' => 60 ];
152+ }
153+
154+ ['data ' => $ value , 'ttl ' => $ ttl ] = $ tmp ;
155+
156+ if (! is_int ($ value )) {
156157 return false ;
157158 }
158159
159- $ newValue = $ data [ ' data ' ] + $ offset ;
160+ $ value += $ offset ;
160161
161- return $ this ->save ($ key , $ newValue , $ data [ ' ttl ' ] ) ? $ newValue : false ;
162+ return $ this ->save ($ key , $ value , $ ttl ) ? $ value : false ;
162163 }
163164
164165 /**
165166 * {@inheritDoc}
166167 */
167168 public function decrement (string $ key , int $ offset = 1 )
168169 {
169- $ key = static ::validateKey ($ key , $ this ->prefix );
170- $ data = $ this ->getItem ($ key );
171-
172- if ($ data === false ) {
173- $ data = [
174- 'data ' => 0 ,
175- 'ttl ' => 60 ,
176- ];
177- } elseif (! is_int ($ data ['data ' ])) {
178- return false ;
179- }
180-
181- $ newValue = $ data ['data ' ] - $ offset ;
182-
183- return $ this ->save ($ key , $ newValue , $ data ['ttl ' ]) ? $ newValue : false ;
170+ return $ this ->increment ($ key , -$ offset );
184171 }
185172
186173 /**
@@ -229,7 +216,8 @@ public function isSupported(): bool
229216 * Does the heavy lifting of actually retrieving the file and
230217 * verifying it's age.
231218 *
232- * @return array|bool|float|int|object|string|null
219+ * @return array<string, mixed>|false
220+ * @phpstan-return array{data: mixed, ttl: int, time: int}|false
233221 */
234222 protected function getItem (string $ filename )
235223 {
@@ -238,15 +226,21 @@ protected function getItem(string $filename)
238226 }
239227
240228 $ data = @unserialize (file_get_contents ($ this ->path . $ filename ));
241- if (! is_array ($ data ) || ! isset ($ data ['ttl ' ])) {
229+
230+ if (! is_array ($ data )) {
231+ return false ;
232+ }
233+
234+ if (! isset ($ data ['ttl ' ]) || ! is_int ($ data ['ttl ' ])) {
235+ return false ;
236+ }
237+
238+ if (! isset ($ data ['time ' ]) || ! is_int ($ data ['time ' ])) {
242239 return false ;
243240 }
244241
245242 if ($ data ['ttl ' ] > 0 && Time::now ()->getTimestamp () > $ data ['time ' ] + $ data ['ttl ' ]) {
246- // If the file is still there then try to remove it
247- if (is_file ($ this ->path . $ filename )) {
248- @unlink ($ this ->path . $ filename );
249- }
243+ @unlink ($ this ->path . $ filename );
250244
251245 return false ;
252246 }
0 commit comments