Skip to content

Commit 1655c0f

Browse files
committed
added PHP 7.0 scalar and return type hints
1 parent 886ddb2 commit 1655c0f

11 files changed

+56
-132
lines changed

src/Caching/Cache.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,27 @@ public function __construct(IStorage $storage, $namespace = NULL)
5151

5252
/**
5353
* Returns cache storage.
54-
* @return IStorage
5554
*/
56-
public function getStorage()
55+
public function getStorage(): IStorage
5756
{
5857
return $this->storage;
5958
}
6059

6160

6261
/**
6362
* Returns cache namespace.
64-
* @return string
6563
*/
66-
public function getNamespace()
64+
public function getNamespace(): string
6765
{
6866
return (string) substr($this->namespace, 0, -1);
6967
}
7068

7169

7270
/**
7371
* Returns new nested cache object.
74-
* @param string
7572
* @return static
7673
*/
77-
public function derive($namespace)
74+
public function derive(string $namespace)
7875
{
7976
$derived = new static($this->storage, $this->namespace . $namespace);
8077
return $derived;
@@ -84,10 +81,9 @@ public function derive($namespace)
8481
/**
8582
* Reads the specified item from the cache or generate it.
8683
* @param mixed
87-
* @param callable
8884
* @return mixed
8985
*/
90-
public function load($key, $fallback = NULL)
86+
public function load($key, callable $fallback = NULL)
9187
{
9288
$data = $this->storage->read($this->generateKey($key));
9389
if ($data === NULL && $fallback) {
@@ -101,11 +97,8 @@ public function load($key, $fallback = NULL)
10197

10298
/**
10399
* Reads multiple items from the cache.
104-
* @param array
105-
* @param callable
106-
* @return array
107100
*/
108-
public function bulkLoad(array $keys, $fallback = NULL)
101+
public function bulkLoad(array $keys, callable $fallback = NULL): array
109102
{
110103
if (count($keys) === 0) {
111104
return [];
@@ -276,9 +269,8 @@ public function call($function)
276269
/**
277270
* Caches results of function/method calls.
278271
* @param mixed
279-
* @return \Closure
280272
*/
281-
public function wrap($function, array $dependencies = NULL)
273+
public function wrap($function, array $dependencies = NULL): \Closure
282274
{
283275
return function () use ($function, $dependencies) {
284276
$key = [$function, func_get_args()];
@@ -311,10 +303,8 @@ public function start($key)
311303

312304
/**
313305
* Generates internal cache key.
314-
* @param mixed
315-
* @return string
316306
*/
317-
protected function generateKey($key)
307+
protected function generateKey($key): string
318308
{
319309
return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key));
320310
}
@@ -325,10 +315,8 @@ protected function generateKey($key)
325315

326316
/**
327317
* Checks CALLBACKS dependencies.
328-
* @param array
329-
* @return bool
330318
*/
331-
public static function checkCallbacks($callbacks)
319+
public static function checkCallbacks(array $callbacks): bool
332320
{
333321
foreach ($callbacks as $callback) {
334322
if (!array_shift($callback)(...$callback)) {
@@ -341,23 +329,18 @@ public static function checkCallbacks($callbacks)
341329

342330
/**
343331
* Checks CONSTS dependency.
344-
* @param string
345-
* @param mixed
346-
* @return bool
347332
*/
348-
private static function checkConst($const, $value)
333+
private static function checkConst(string $const, $value): bool
349334
{
350335
return defined($const) && constant($const) === $value;
351336
}
352337

353338

354339
/**
355340
* Checks FILES dependency.
356-
* @param string
357-
* @param int|NULL
358-
* @return bool
341+
* @param int|NULL $time
359342
*/
360-
private static function checkFile($file, $time)
343+
private static function checkFile(string $file, $time): bool
361344
{
362345
return @filemtime($file) == $time; // @ - stat may fail
363346
}

src/Caching/IBulkReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ interface IBulkReader
1818

1919
/**
2020
* Reads from cache in bulk.
21-
* @param string
2221
* @return array key => value pairs, missing items are omitted
2322
*/
24-
function bulkRead(array $keys);
23+
function bulkRead(array $keys): array;
2524

2625
}

src/Caching/IStorage.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,30 @@ interface IStorage
1818

1919
/**
2020
* Read from cache.
21-
* @param string
2221
* @return mixed
2322
*/
24-
function read($key);
23+
function read(string $key);
2524

2625
/**
2726
* Prevents item reading and writing. Lock is released by write() or remove().
28-
* @param string
2927
* @return void
3028
*/
31-
function lock($key);
29+
function lock(string $key);
3230

3331
/**
3432
* Writes item into the cache.
35-
* @param string
36-
* @param mixed
3733
* @return void
3834
*/
39-
function write($key, $data, array $dependencies);
35+
function write(string $key, $data, array $dependencies);
4036

4137
/**
4238
* Removes item from the cache.
43-
* @param string
4439
* @return void
4540
*/
46-
function remove($key);
41+
function remove(string $key);
4742

4843
/**
4944
* Removes items from the cache by conditions.
50-
* @param array conditions
5145
* @return void
5246
*/
5347
function clean(array $conditions);

src/Caching/Storages/DevNullStorage.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,42 @@ class DevNullStorage implements Nette\Caching\IStorage
2121

2222
/**
2323
* Read from cache.
24-
* @param string
2524
* @return mixed
2625
*/
27-
public function read($key)
26+
public function read(string $key)
2827
{
2928
}
3029

3130

3231
/**
3332
* Prevents item reading and writing. Lock is released by write() or remove().
34-
* @param string
3533
* @return void
3634
*/
37-
public function lock($key)
35+
public function lock(string $key)
3836
{
3937
}
4038

4139

4240
/**
4341
* Writes item into the cache.
44-
* @param string
45-
* @param mixed
4642
* @return void
4743
*/
48-
public function write($key, $data, array $dependencies)
44+
public function write(string $key, $data, array $dependencies)
4945
{
5046
}
5147

5248

5349
/**
5450
* Removes item from the cache.
55-
* @param string
5651
* @return void
5752
*/
58-
public function remove($key)
53+
public function remove(string $key)
5954
{
6055
}
6156

6257

6358
/**
6459
* Removes items from the cache by conditions & garbage collector.
65-
* @param array conditions
6660
* @return void
6761
*/
6862
public function clean(array $conditions)

src/Caching/Storages/FileStorage.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ public function __construct($dir, IJournal $journal = NULL)
8383

8484
/**
8585
* Read from cache.
86-
* @param string
8786
* @return mixed
8887
*/
89-
public function read($key)
88+
public function read(string $key)
9089
{
9190
$meta = $this->readMetaAndLock($this->getCacheFile($key), LOCK_SH);
9291
if ($meta && $this->verify($meta)) {
@@ -100,10 +99,8 @@ public function read($key)
10099

101100
/**
102101
* Verifies dependencies.
103-
* @param array
104-
* @return bool
105102
*/
106-
private function verify($meta)
103+
private function verify(array $meta): bool
107104
{
108105
do {
109106
if (!empty($meta[self::META_DELTA])) {
@@ -140,10 +137,9 @@ private function verify($meta)
140137

141138
/**
142139
* Prevents item reading and writing. Lock is released by write() or remove().
143-
* @param string
144140
* @return void
145141
*/
146-
public function lock($key)
142+
public function lock(string $key)
147143
{
148144
$cacheFile = $this->getCacheFile($key);
149145
if ($this->useDirs && !is_dir($dir = dirname($cacheFile))) {
@@ -159,11 +155,9 @@ public function lock($key)
159155

160156
/**
161157
* Writes item into the cache.
162-
* @param string
163-
* @param mixed
164158
* @return void
165159
*/
166-
public function write($key, $data, array $dp)
160+
public function write(string $key, $data, array $dp)
167161
{
168162
$meta = [
169163
self::META_TIME => microtime(),
@@ -244,10 +238,9 @@ public function write($key, $data, array $dp)
244238

245239
/**
246240
* Removes item from the cache.
247-
* @param string
248241
* @return void
249242
*/
250-
public function remove($key)
243+
public function remove(string $key)
251244
{
252245
unset($this->locks[$key]);
253246
$this->delete($this->getCacheFile($key));
@@ -256,7 +249,6 @@ public function remove($key)
256249

257250
/**
258251
* Removes items from the cache by conditions & garbage collector.
259-
* @param array conditions
260252
* @return void
261253
*/
262254
public function clean(array $conditions)
@@ -315,7 +307,7 @@ public function clean(array $conditions)
315307
* @param int lock mode
316308
* @return array|NULL
317309
*/
318-
protected function readMetaAndLock($file, $lock)
310+
protected function readMetaAndLock(string $file, int $lock)
319311
{
320312
$handle = @fopen($file, 'r+b'); // @ - file may not exist
321313
if (!$handle) {
@@ -342,10 +334,9 @@ protected function readMetaAndLock($file, $lock)
342334

343335
/**
344336
* Reads cache data from disk and closes cache file handle.
345-
* @param array
346337
* @return mixed
347338
*/
348-
protected function readData($meta)
339+
protected function readData(array $meta)
349340
{
350341
$data = stream_get_contents($meta[self::HANDLE]);
351342
flock($meta[self::HANDLE], LOCK_UN);
@@ -361,10 +352,8 @@ protected function readData($meta)
361352

362353
/**
363354
* Returns file name.
364-
* @param string
365-
* @return string
366355
*/
367-
protected function getCacheFile($key)
356+
protected function getCacheFile(string $key): string
368357
{
369358
$file = urlencode($key);
370359
if ($this->useDirs && $a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR)
@@ -376,11 +365,10 @@ protected function getCacheFile($key)
376365

377366
/**
378367
* Deletes and closes file.
379-
* @param string
380-
* @param resource
368+
* @param resource $handle
381369
* @return void
382370
*/
383-
private static function delete($file, $handle = NULL)
371+
private static function delete(string $file, $handle = NULL)
384372
{
385373
if (@unlink($file)) { // @ - file may not already exist
386374
if ($handle) {

src/Caching/Storages/IJournal.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ interface IJournal
1818

1919
/**
2020
* Writes entry information into the journal.
21-
* @param string
22-
* @param array
2321
* @return void
2422
*/
25-
function write($key, array $dependencies);
23+
function write(string $key, array $dependencies);
2624

2725

2826
/**
2927
* Cleans entries from journal.
30-
* @param array
3128
* @return array|NULL of removed items or NULL when performing a full cleanup
3229
*/
3330
function clean(array $conditions);

0 commit comments

Comments
 (0)