Skip to content

Commit

Permalink
added PHP 7.0 scalar and return type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 23, 2017
1 parent 886ddb2 commit 30051c1
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 130 deletions.
39 changes: 11 additions & 28 deletions src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,27 @@ public function __construct(IStorage $storage, $namespace = NULL)

/**
* Returns cache storage.
* @return IStorage
*/
public function getStorage()
public function getStorage(): IStorage
{
return $this->storage;
}


/**
* Returns cache namespace.
* @return string
*/
public function getNamespace()
public function getNamespace(): string
{
return (string) substr($this->namespace, 0, -1);
}


/**
* Returns new nested cache object.
* @param string
* @return static
*/
public function derive($namespace)
public function derive(string $namespace)
{
$derived = new static($this->storage, $this->namespace . $namespace);
return $derived;
Expand All @@ -84,10 +81,9 @@ public function derive($namespace)
/**
* Reads the specified item from the cache or generate it.
* @param mixed
* @param callable
* @return mixed
*/
public function load($key, $fallback = NULL)
public function load($key, callable $fallback = NULL)
{
$data = $this->storage->read($this->generateKey($key));
if ($data === NULL && $fallback) {
Expand All @@ -101,11 +97,8 @@ public function load($key, $fallback = NULL)

/**
* Reads multiple items from the cache.
* @param array
* @param callable
* @return array
*/
public function bulkLoad(array $keys, $fallback = NULL)
public function bulkLoad(array $keys, callable $fallback = NULL): array
{
if (count($keys) === 0) {
return [];
Expand Down Expand Up @@ -276,9 +269,8 @@ public function call($function)
/**
* Caches results of function/method calls.
* @param mixed
* @return \Closure
*/
public function wrap($function, array $dependencies = NULL)
public function wrap($function, array $dependencies = NULL): \Closure
{
return function () use ($function, $dependencies) {
$key = [$function, func_get_args()];
Expand Down Expand Up @@ -311,10 +303,8 @@ public function start($key)

/**
* Generates internal cache key.
* @param mixed
* @return string
*/
protected function generateKey($key)
protected function generateKey($key): string
{
return $this->namespace . md5(is_scalar($key) ? (string) $key : serialize($key));
}
Expand All @@ -325,10 +315,8 @@ protected function generateKey($key)

/**
* Checks CALLBACKS dependencies.
* @param array
* @return bool
*/
public static function checkCallbacks($callbacks)
public static function checkCallbacks(array $callbacks): bool
{
foreach ($callbacks as $callback) {
if (!array_shift($callback)(...$callback)) {
Expand All @@ -341,23 +329,18 @@ public static function checkCallbacks($callbacks)

/**
* Checks CONSTS dependency.
* @param string
* @param mixed
* @return bool
*/
private static function checkConst($const, $value)
private static function checkConst(string $const, $value): bool
{
return defined($const) && constant($const) === $value;
}


/**
* Checks FILES dependency.
* @param string
* @param int|NULL
* @return bool
* @param int|NULL $time
*/
private static function checkFile($file, $time)
private static function checkFile(string $file, $time): bool
{
return @filemtime($file) == $time; // @ - stat may fail
}
Expand Down
3 changes: 1 addition & 2 deletions src/Caching/IBulkReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ interface IBulkReader

/**
* Reads from cache in bulk.
* @param string
* @return array key => value pairs, missing items are omitted
*/
function bulkRead(array $keys);
function bulkRead(array $keys): array;

}
14 changes: 4 additions & 10 deletions src/Caching/IStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,30 @@ interface IStorage

/**
* Read from cache.
* @param string
* @return mixed
*/
function read($key);
function read(string $key);

/**
* Prevents item reading and writing. Lock is released by write() or remove().
* @param string
* @return void
*/
function lock($key);
function lock(string $key);

/**
* Writes item into the cache.
* @param string
* @param mixed
* @return void
*/
function write($key, $data, array $dependencies);
function write(string $key, $data, array $dependencies);

/**
* Removes item from the cache.
* @param string
* @return void
*/
function remove($key);
function remove(string $key);

/**
* Removes items from the cache by conditions.
* @param array conditions
* @return void
*/
function clean(array $conditions);
Expand Down
14 changes: 4 additions & 10 deletions src/Caching/Storages/DevNullStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,42 @@ class DevNullStorage implements Nette\Caching\IStorage

/**
* Read from cache.
* @param string
* @return mixed
*/
public function read($key)
public function read(string $key)
{
}


/**
* Prevents item reading and writing. Lock is released by write() or remove().
* @param string
* @return void
*/
public function lock($key)
public function lock(string $key)
{
}


/**
* Writes item into the cache.
* @param string
* @param mixed
* @return void
*/
public function write($key, $data, array $dependencies)
public function write(string $key, $data, array $dependencies)
{
}


/**
* Removes item from the cache.
* @param string
* @return void
*/
public function remove($key)
public function remove(string $key)
{
}


/**
* Removes items from the cache by conditions & garbage collector.
* @param array conditions
* @return void
*/
public function clean(array $conditions)
Expand Down
32 changes: 10 additions & 22 deletions src/Caching/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ public function __construct($dir, IJournal $journal = NULL)

/**
* Read from cache.
* @param string
* @return mixed
*/
public function read($key)
public function read(string $key)
{
$meta = $this->readMetaAndLock($this->getCacheFile($key), LOCK_SH);
if ($meta && $this->verify($meta)) {
Expand All @@ -100,10 +99,8 @@ public function read($key)

/**
* Verifies dependencies.
* @param array
* @return bool
*/
private function verify($meta)
private function verify(array $meta): bool
{
do {
if (!empty($meta[self::META_DELTA])) {
Expand Down Expand Up @@ -140,10 +137,9 @@ private function verify($meta)

/**
* Prevents item reading and writing. Lock is released by write() or remove().
* @param string
* @return void
*/
public function lock($key)
public function lock(string $key)
{
$cacheFile = $this->getCacheFile($key);
if ($this->useDirs && !is_dir($dir = dirname($cacheFile))) {
Expand All @@ -159,11 +155,9 @@ public function lock($key)

/**
* Writes item into the cache.
* @param string
* @param mixed
* @return void
*/
public function write($key, $data, array $dp)
public function write(string $key, $data, array $dp)
{
$meta = [
self::META_TIME => microtime(),
Expand Down Expand Up @@ -244,10 +238,9 @@ public function write($key, $data, array $dp)

/**
* Removes item from the cache.
* @param string
* @return void
*/
public function remove($key)
public function remove(string $key)
{
unset($this->locks[$key]);
$this->delete($this->getCacheFile($key));
Expand All @@ -256,7 +249,6 @@ public function remove($key)

/**
* Removes items from the cache by conditions & garbage collector.
* @param array conditions
* @return void
*/
public function clean(array $conditions)
Expand Down Expand Up @@ -315,7 +307,7 @@ public function clean(array $conditions)
* @param int lock mode
* @return array|NULL
*/
protected function readMetaAndLock($file, $lock)
protected function readMetaAndLock(string $file, int $lock)
{
$handle = @fopen($file, 'r+b'); // @ - file may not exist
if (!$handle) {
Expand All @@ -342,10 +334,9 @@ protected function readMetaAndLock($file, $lock)

/**
* Reads cache data from disk and closes cache file handle.
* @param array
* @return mixed
*/
protected function readData($meta)
protected function readData(array $meta)
{
$data = stream_get_contents($meta[self::HANDLE]);
flock($meta[self::HANDLE], LOCK_UN);
Expand All @@ -361,10 +352,8 @@ protected function readData($meta)

/**
* Returns file name.
* @param string
* @return string
*/
protected function getCacheFile($key)
protected function getCacheFile(string $key): string
{
$file = urlencode($key);
if ($this->useDirs && $a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR)
Expand All @@ -376,11 +365,10 @@ protected function getCacheFile($key)

/**
* Deletes and closes file.
* @param string
* @param resource
* @param resource $handle
* @return void
*/
private static function delete($file, $handle = NULL)
private static function delete(string $file, $handle = NULL)
{
if (@unlink($file)) { // @ - file may not already exist
if ($handle) {
Expand Down
5 changes: 1 addition & 4 deletions src/Caching/Storages/IJournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ interface IJournal

/**
* Writes entry information into the journal.
* @param string
* @param array
* @return void
*/
function write($key, array $dependencies);
function write(string $key, array $dependencies);


/**
* Cleans entries from journal.
* @param array
* @return array|NULL of removed items or NULL when performing a full cleanup
*/
function clean(array $conditions);
Expand Down

0 comments on commit 30051c1

Please sign in to comment.